  ### 5 sorting algorithms collection

##### 1. Bubble sort:
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

``````
int main()
{
int n=6;
int a[n] = {6,5,4,3,2,1};
int temp;

for(int i = 0; i < n; i++)
{
for( int j = i+1; j < n; j++ )
{
if( a[i] > a[j] )
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

for( int i = 0; i < n; i++ )
{
cout << a[i] << " ";
}
return 0;
}
``````
Worst and Average Case Time Complexity: O(n*n). Worst case occurs when array is reverse sorted.
Best Case Time Complexity: O(n). Best case occurs when array is already sorted.
Auxiliary Space: O(1)
Boundary Cases: Bubble sort takes minimum time (Order of n) when elements are already sorted.
Sorting In Place: Yes
Stable: Yes
Uses: Due to its simplicity, bubble sort is often used to introduce the concept of a sorting algorithm
##### 2. Insertion sort:
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.

Algorithm
To sort an array of size n in ascending order:
1: Iterate from arr to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.

``````
int main()
{
int n=6;
int a[n] = {6,2,4,3,5,1};
int key, j;

for( int i = 0; i < n; i++ )
{
key = a[i];
j = i-1;

while( j >= 0 && a[j] > key )
{
a[j+1] = a[j];
j--;
}
a[j+1] = key;
}

for( int i = 0; i < n; i++ )
{
cout << a[i] << " ";
}
return 0;
}
``````
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Boundary Cases: Insertion sort takes maximum time to sort if elements are sorted in reverse order. And it takes minimum time (Order of n) when elements are already sorted.
Algorithmic Paradigm: Incremental Approach
Sorting In Place: Yes
Stable: Yes
Online: Yes
Uses: Insertion sort is used when number of elements is small. It can also be useful when input array is almost sorted, only few elements are misplaced in complete big array.
##### 3. Selection sort:
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.

In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.

``````
int main()
{
int n=6;
int a[n] = {6,2,4,3,5,1};
int sIndex;
int temp;

for( int i = 0; i < n; i++ )
{
sIndex = i;
for( int j = i; j < n; j++ )
{
if( a[sIndex] > a[j] )
{
sIndex = j;
}
}

temp = a[i];
a[i] = a[sIndex];
a[sIndex] = temp;
}

for( int i = 0; i < n; i++ )
{
cout << a[i] << " ";
}
return 0;
}
``````
Time Complexity: O(n2) as there are two nested loops.
Auxiliary Space: O(1)
The good thing about selection sort is it never makes more than O(n) swaps and can be useful when memory write is a costly operation.
Stability: The default implementation is not stable. However it can be made stable. Please see stable selection sort for details.
In Place: Yes, it does not require extra space.

##### 4. Merge sort:
Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves. The mergeArr() function is used for merging two halves. The mergeArr(a, l, m, r) is a key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.

``````
void mergeArr(int a[], 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] = a[l+i];
}

for( int i = 0; i < n2; i++ )
{
R[i] = a[m+1+i];
}

int i=0, j=0;
int k=l;
while( i < n1 && j < n2 )
{
if( L[i] <= R[j] )
{
a[k] = L[i];
i++;
} else
{
a[k] = R[j];
j++;
}
k++;
}

while( i < n1 )
{
a[k] = L[i];
i++;
k++;
}

while( j < n2 )
{
a[k] = R[j];
j++;
k++;
}
}

void mergeSort(int a[], int l, int r)
{
if( l >= r )
{
return;
}

int m = (r-l)/2 + l;

mergeSort(a, l, m);
mergeSort(a, m+1, r);

mergeArr(a, l, m, r);
}

void printArr(int a[], int n)
{
for( int i = 0; i < n; i++ )
{
cout << a[i] << " ";
}
cout << endl;
}

int main()
{
int n = 7;
int a[n] = {6,7,5,4,3,2,1};

mergeSort(a, 0, n-1);
printArr(a, n);

return 0;
}
``````
Time Complexity: Time complexity of Merge Sort is ?(nLogn) in all 3 cases (worst, average and best) as merge sort always divides the array into two halves and takes linear time to merge two halves.
Auxiliary Space: O(n)
Algorithmic Paradigm: Divide and Conquer
Sorting In Place: No in a typical implementation
Stable: Yes
Applications of Merge Sort: Merge Sort is useful for sorting linked lists in O(nLogn) time. In the case of linked lists, the case is different mainly due to the difference in memory allocation of arrays and linked lists. Unlike arrays, linked list nodes may not be adjacent in memory. Unlike an array, in the linked list, we can insert items in the middle in O(1) extra space and O(1) time. Therefore, the merge operation of merge sort can be implemented without extra space for linked lists. In arrays, we can do random access as elements are contiguous in memory. Let us say we have an integer (4-byte) array A and let the address of A be x then to access A[i], we can directly access the memory at (x + i*4). Unlike arrays, we can not do random access in the linked list. Quick Sort requires a lot of this kind of access. In a linked list to access i th index, we have to travel each and every node from the head to i th node as we do not have a continuous block of memory. Therefore, the overhead increases for quicksort. Merge sort accesses data sequentially and the need of random access is low
Drawbacks of Merge Sort:
1. Slower comparative to the other sort algorithms for smaller tasks.
2. Merge sort algorithm requires an additional memory space of 0(n) for the temporary array
3. It goes through the whole process even if the array is sorted

##### 5. Quick sort:
Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.
1. Always pick first element as pivot
2. Always pick last element as pivot (implemented below)
3. Pick a random element as pivot
4. Pick median as pivot
The key process in quickSort is makePartition(). Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.
Partition Algorithm: The logic is simple, we start from the leftmost element and keep track of index of smaller (or equal to) elements as i. While traversing, if we find a smaller element, we swap current element with arr[i]. Otherwise we ignore current element.

``````
void elementSwap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}

int makePartition(int a[], int s, int e)
{
int pevot = a[e];
int pIndex = s;

for( int i = s; i < e; i++ )
{
if( a[i] <= pevot )
{
elementSwap(&a[i], &a[pIndex]);
pIndex++;
}
}

elementSwap(&a[e], &a[pIndex]);
return pIndex;
}

void quickSort(int a[], int s, int e)
{
if( s >= e )
{
return;
}
int p = makePartition(a, s, e);
quickSort(a, s, p-1);
quickSort(a, p+1, e);
}

void printArr(int a[], int n)
{
for( int i = 0; i < n; i++ )
{
cout << a[i] << " ";
}
cout << endl;
}

int main()
{
int n = 6;
int a[n] = {6,5,4,3,2,1};

quickSort(a, 0, n-1);
printArr(a, n);

return 0;
}
``````
Time Complexity:
1. Best case: ?(nLogn)
2. Average case: O(nLogn)
3. Worst caseL O(n^2)
Although the worst case time complexity of QuickSort is O(n2) which is more than many other sorting algorithms like Merge Sort and Heap Sort, QuickSort is faster in practice, because its inner loop can be efficiently implemented on most architectures, and in most real-world data. QuickSort can be implemented in different ways by changing the choice of pivot, so that the worst case rarely occurs for a given type of data. However, merge sort is generally considered better when data is huge and stored in external storage.
Stable: The default implementation is not stable. However any sorting algorithm can be made stable by considering indexes as comparison parameter.
In place: As per the broad definition of in-place algorithm it qualifies as an in-place sorting algorithm as it uses extra space only for storing recursive function calls but not for manipulating the input.

Why Quick Sort is preferred over MergeSort for sorting Arrays
Quick Sort in its general form is an in-place sort (i.e. it does not require any extra storage) whereas merge sort requires O(N) extra storage, N denoting the array size which may be quite expensive. Allocating and de-allocating the extra space used for merge sort increases the running time of the algorithm. Comparing average complexity we find that both type of sorts have O(NlogN) average complexity but the constants differ. For arrays, merge sort loses due to the use of extra O(N) storage space.
Most practical implementations of Quick Sort use randomized version. The randomized version has expected time complexity of O(nLogn). The worst case is possible in randomized version also, but worst case does not occur for a particular pattern (like sorted array) and randomized Quick Sort works well in practice.
Quick Sort is also a cache friendly sorting algorithm as it has good locality of reference when used for arrays.
Quick Sort is also tail recursive, therefore tail call optimizations is done.

Collected from:
https://www.geeksforgeeks.org/bubble-sort/
https://www.geeksforgeeks.org/insertion-sort/
https://www.geeksforgeeks.org/selection-sort/
https://www.geeksforgeeks.org/merge-sort/
https://www.geeksforgeeks.org/quick-sort/