All Sorting Algorithms In c++: Best Sorting Algorithm Code in cpp
Here you can find all Implementation of sorting algorithms in c++ which sorts the array in Increasing Order (Ascending Order).
- Bubble sort:
- Insertion sort:
- Selection sort:
- Merge sort:
Read Also:All Sorting Algorithm In python
Bubble Sort:
Implementation:
// c++ implementation of Bubble sort
#include <bits/stdc++.h>
using namespace std;
// Bubble Sort function
void bubble_sort(int arr[], int n)
{
// iterate i from 0 to n-1
for (int i = 0; i < n; i++)
{
// variable for checking if swap is done or not
int swapped = 0;
// iterate j form 0 to n-2
for (int j = 0; j < n - 1; j++)
{
// compare two neighbors at j index
if (arr[j] > arr[j + 1])
{
// if first is greater than second then swap element
int temp;
// swapping
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
// swapping is done
swapped = 1;
}
}
// if swapping is not done then arr is sorted
// then return function to break for loop
if (swapped == 0)
{
return;
}
}
}
// Driver code:
int main()
{
int arr[] = {1, 5, 8, 9, 2, 0, 323, 4, 0, 65, 6, 3, 1, 23, 5};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
// printing sorted array
cout << "sorted array:" << endl;
for (int i = 0; i < n; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
return 0;
}
Output:
sorted array:
0 0 1 1 2 3 4 5 5 6 8 9 23 65 323
Insertion Sort:
Implementation:
// c++ implementation of insertion sort
#include <bits/stdc++.h>
using namespace std;
// insertion sort function
void insertion_sort(int arr[], int n)
{
// iterate i form 1 to n-1
for (int i = 1; i < n; i++)
{
// here arr[0...i-1] is sorted in every iteration
// key which we will inserting
int key = arr[i];
// point to last element of sorted array
int j = i - 1;
// shift elements greater than key
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j--;
}
// please key element to appropriate please
arr[j + 1] = key;
}
}
// Driver code:
int main()
{
int arr[] = {1, 5, 8, 9, 2, 65, 6, 3, 1, 23, 5};
int n = sizeof(arr) / sizeof(arr[0]);
insertion_sort(arr, n);
// printing sorted array
cout << "sorted array:" << endl;
for (int i = 0; i < n; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
return 0;
}
Output:
sorted array:
1 1 2 3 5 5 6 8 9 23 65
Selection Sort:
Implementation:
// c++ implementation of selection sort
#include <bits/stdc++.h>
using namespace std;
// selection sort function
void selection_sort(int arr[], int n)
{
// iterate i from 0 to n-2
for (int i = 0; i < n - 1; i++)
{
// consider arr[i...n] array
int min_index = i;
// find min element index
for (int j = i + 1; j < n; j++)
{
if (arr[min_index] > arr[j])
{
min_index = j;
}
}
// swapping min element with first element for array.
int temp;
temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}
// Driver code:
int main()
{
int arr[] = {1, 5, 8, 9, 2, 67895, 99999, 6, 3, 1, 23, 5};
int n = sizeof(arr) / sizeof(arr[0]);
selection_sort(arr, n);
// printing sorted array
cout << "sorted array:" << endl;
for (int i = 0; i < n; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
return 0;
}
Output:
sorted array:
1 1 2 3 5 5 6 8 9 23 67895 99999
Merge Sort:
Implementation:
//c++ program for merge sort
#include <bits/stdc++.h>
using namespace std;
// Merge to subarray of array
// first_array = array[start..mid]
// second_array = array[mid+1..end]
void Merge(vector<int> &array, int start, int mid, int end)
{
// creating to temp subarray
vector<int> first_array;
vector<int> second_array;
// copy first_array
for (int i = start; i <= mid; i++)
{
first_array.push_back(array[i]);
}
// copy second_array
for (int i = mid + 1; i <= end; i++)
{
second_array.push_back(array[i]);
}
// index reference
int i = 0; // for first_array
int j = 0; // for second_array
int main_i = start; // for main array
int size_1 = first_array.size(); // size of first_array
int size_2 = second_array.size(); // size of second_array
// merge subarray first_array and second_array into array[start..end]
while (i < size_1 && j < size_2)
{
if (first_array[i] <= second_array[j])
{
array[main_i] = first_array[i];
i++;
}
else
{
array[main_i] = second_array[j];
j++;
}
main_i++;
}
// copy remaining element of first_array
while (i < size_1)
{
array[main_i] = first_array[i];
i++;
main_i++;
}
// copy remaining element of second_array
while (j < size_2)
{
array[main_i] = second_array[j];
j++;
main_i++;
}
}
// main merge Sort function
void merge_sort(vector<int> &array, int start, int end)
{
if (start >= end)
{
return;
}
// finding mid point
int mid = start + (end - start) / 2;
// divide
merge_sort(array, start, mid);
merge_sort(array, mid + 1, end);
// conquer
Merge(array, start, mid, end);
}
// driver code
int main()
{
// declaring array
vector<int> array = {1, 12, 5, 4, 78, 9, 5, 6, 3, 2, 1, 4};
int n = array.size();
merge_sort(array, 0, n - 1);
// array is sorted
cout << "sorted array:" << endl;
for (int i = 0; i < n; i++)
{
cout << array[i] << ' ';
}
cout << endl;
}
Output:
sorted array:
1 1 2 3 4 4 5 5 6 9 12 78
Read Also:Merge Sort Algorithm
Image of code:
0 Comments
If you have any doubt let me know.