Bubble Sort
#include <iostream>
using namespace std;
// Function to perform bubble sort
void bubbleSort(int arr[], int n) {
bool swapped;
for (int i = 0; i < n - 1; ++i) {
swapped = false;
// Traverse the array from 0 to n-i-1
for (int j = 0; j < n - i - 1; ++j) {
// Swap if the element found is greater than the next element
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// If no two elements were swapped in the inner loop, then break
if (!swapped) break;
}
}
// Function to print an array
void printArray(int arr[], int size) {
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl; //endl is used for new line
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int* arr = new int[n]; // Dynamically allocate array
cout << "Enter the elements: ";
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
bubbleSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
delete[] arr; // Free the dynamically allocated array
return 0;
}
Explanation:
1. Input and Output: The program first asks for the
number of elements and then takes the elements as input
from the user. After sorting, it prints the sorted array.
2. Bubble Sort Function:
o It uses a nested loop to compare adjacent elements.
o If elements are out of order, it swaps them.
o The outer loop ensures that with each iteration, the
largest unsorted element "bubbles up" to its correct
position.
o An optimization (swapped flag) is included to stop
early if no swaps were made during a pass, which
indicates that the array is already sorted.
3. Dynamic Memory Allocation: new and delete[] are used
to handle the array, allowing it to be of dynamic size
based on user input.
Selection Sort
#include <iostream>
using namespace std;
// Function to perform selection sort
void selectionSort(int array[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i; // Assume the current index is the minimum
for (int j = i + 1; j < n; j++) {
if (array[j] < array[minIndex]) {
minIndex = j; // Update minIndex if a smaller element is
found
}
}
// Swap the found minimum element with the first element
if (minIndex != i) {
swap(array[i], array[minIndex]);
}
}
}
// Function to print the array
void printArray(int array[], int n) {
for (int i = 0; i < n; i++) {
cout << array[i] << " ";
}
cout << endl;
}
// Main function
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int *array = new int[n]; // Dynamic array allocation
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> array[i];
}
cout << "Original array: ";
printArray(array, n);
selectionSort(array, n);
cout << "Sorted array: ";
printArray(array, n);
delete[] array; // Free dynamically allocated memory
return 0;
}
SHELL SORT
#include <iostream>
using namespace std;
// Function to perform Shell Sort
void shellSort(int arr[], int n) {
// Start with a large gap, then reduce the gap
for (int gap = n / 2; gap > 0; gap /= 2) {
// Do a gapped insertion sort for this gap size
for (int i = gap; i < n; i++) {
// Save the current element
int temp = arr[i];
int j;
// Shift earlier gap-sorted elements up until the correct location
for arr[i] is found
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
// Put temp (the original arr[i]) in its correct location
arr[j] = temp;
}
}
}
// Function to print the array
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
shellSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}