Sorting Algorithms in C++
Selection Sort
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
int minIndex = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[minIndex]) minIndex = j;
}
swap(arr[i], arr[minIndex]);
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = 5;
selectionSort(arr, n);
cout << "Sorted array: ";
for (int i=0; i<n; i++) cout << arr[i] << " ";
return 0;
}
/* Output:
Sorted array: 11 12 22 25 64
*/
Bubble Sort
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) swap(arr[j], arr[j+1]);
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22};
int n = 5;
bubbleSort(arr, n);
cout << "Sorted array: ";
for (int i=0; i<n; i++) cout << arr[i] << " ";
return 0;
}
/* Output:
Sorted array: 12 22 25 34 64
*/
Insertion Sort
#include <iostream>
using namespace std;
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i-1;
while (j >= 0 && arr[j] > key) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = 5;
insertionSort(arr, n);
cout << "Sorted array: ";
for (int i=0; i<n; i++) cout << arr[i] << " ";
return 0;
}
/* Output:
Sorted array: 5 6 11 12 13
*/
Recursive Bubble Sort
#include <iostream>
using namespace std;
void recursiveBubble(int arr[], int n) {
if (n == 1) return;
for (int i = 0; i < n-1; i++) {
if (arr[i] > arr[i+1]) swap(arr[i], arr[i+1]);
}
recursiveBubble(arr, n-1);
}
int main() {
int arr[] = {5, 1, 4, 2, 8};
int n = 5;
recursiveBubble(arr, n);
cout << "Sorted array: ";
for (int i=0; i<n; i++) cout << arr[i] << " ";
return 0;
}
/* Output:
Sorted array: 1 2 4 5 8
*/
Recursive Insertion Sort
#include <iostream>
using namespace std;
void recursiveInsertion(int arr[], int n) {
if (n <= 1) return;
recursiveInsertion(arr, n-1);
int last = arr[n-1];
int j = n-2;
while (j >= 0 && arr[j] > last) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = last;
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = 5;
recursiveInsertion(arr, n);
cout << "Sorted array: ";
for (int i=0; i<n; i++) cout << arr[i] << " ";
return 0;
}
/* Output:
Sorted array: 5 6 11 12 13
*/
Merge Sort
#include <iostream>
using namespace std;
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) L[i] = arr[l + i];
for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) arr[k++] = L[i++];
else arr[k++] = R[j++];
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}
void mergeSort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r-l)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = 6;
mergeSort(arr, 0, n-1);
cout << "Sorted array: ";
for (int i=0; i<n; i++) cout << arr[i] << " ";
return 0;
}
/* Output:
Sorted array: 5 6 7 11 12 13
*/
Quick Sort
#include <iostream>
using namespace std;
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high-1; j++) {
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i+1], arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = 6;
quickSort(arr, 0, n-1);
cout << "Sorted array: ";
for (int i=0; i<n; i++) cout << arr[i] << " ";
return 0;
}
/* Output:
Sorted array: 1 5 7 8 9 10
*/