Factors In Deciding A Good Sorting Algorithm

Top-Tier Sorting Algorithms





#include <bits/stdc++.h>

using namespace std;

void swap( int *xp, int *yp)

{

int temp = *xp;

*xp = *yp;

*yp = temp;

}

void selectionSort( int arr[], int n)

{

int i, j, min_idx;

for (i = 0; i < n-1; i++)

{

min_idx = i;

for (j = i+1; j < n; j++)

if (arr[j] < arr[min_idx])

min_idx = j;

swap(&arr[min_idx], &arr[i]);

}

}

void printArray( int arr[], int size)

{

int i;

for (i=0; i < size; i++)

cout << arr[i] << " " ;

cout << endl;

}

int main()

{

int arr[] = {64, 25, 12, 22, 11};

int n = sizeof (arr)/ sizeof (arr[0]);

selectionSort(arr, n);

cout << "Sorted array:

" ;

printArray(arr, n);

return 0;

}

filter_none

edit

close

play_arrow

link

brightness_4

code <?php function selection_sort(& $arr , $n ) { for ( $i = 0; $i < $n ; $i ++) { $low = $i ; for ( $j = $i + 1; $j < $n ; $j ++) { if ( $arr [ $j ] < $arr [ $low ]) { $low = $j ; } } if ( $arr [ $i ] > $arr [ $low ]) { $tmp = $arr [ $i ]; $arr [ $i ] = $arr [ $low ]; $arr [ $low ] = $tmp ; } } } $arr = array (64, 25, 12, 22, 11); $len = count ( $arr ); selection_sort( $arr , $len ); echo "Sorted array :

" ; for ( $i = 0; $i < $len ; $i ++) echo $arr [ $i ] . " " ; ?> chevron_right

filter_none



Output:

Sorted array: 11 12 22 25 64









Insertion Sort - Go through every component in the exhibit. If the current element is smaller than the element to it's left, swap it left (until it's bigger than the element to it's left).

Time: O(N^2).

Space: O(1).

Stable: Yes.

Swaps: O(N^2). If the data is already sorted:

O(N). Insertion sort is excellent if there's an already sorted list, and a bit of data is added to the end of it (and then you need to resort the whole list).





#include <bits/stdc++.h>

using namespace std;

void insertionSort( int arr[], int n)

{

int i, key, j;

for (i = 1; i < n; i++)

{

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key)

{

arr[j + 1] = arr[j];

j = j - 1;

}

arr[j + 1] = key;

}

}

void printArray( int arr[], int n)

{

int i;

for (i = 0; i < n; i++)

cout << arr[i] << " " ;

cout << endl;

}

int main()

{

int arr[] = { 12, 11, 13, 5, 6 };

int n = sizeof (arr) / sizeof (arr[0]);

insertionSort(arr, n);

printArray(arr, n);

return 0;

}





filter_none

edit

close

play_arrow

link

brightness_4

code <?php function insertionSort(& $arr , $n ) { for ( $i = 1; $i < $n ; $i ++) { $key = $arr [ $i ]; $j = $i -1; while ( $j >= 0 && $arr [ $j ] > $key ) { $arr [ $j + 1] = $arr [ $j ]; $j = $j - 1; } $arr [ $j + 1] = $key ; } } function printArray(& $arr , $n ) { for ( $i = 0; $i < $n ; $i ++) echo $arr [ $i ]. " " ; echo "

" ; } $arr = array (12, 11, 13, 5, 6); $n = sizeof( $arr ); insertionSort( $arr , $n ); printArray( $arr , $n ); ?> chevron_right

filter_none



5 6 11 12 13

#include<stdlib.h>

#include<stdio.h>

void merge( int arr[], int l, int m, int r)

{

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

for (i = 0; i < n1; i++)

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1+ j];

i = 0;

j = 0;

k = l;

while (i < n1 && j < n2)

{

if (L[i] <= R[j])

{

arr[k] = L[i];

i++;

}

else

{

arr[k] = R[j];

j++;

}

k++;

}

while (i < n1)

{

arr[k] = L[i];

i++;

k++;

}

while (j < n2)

{

arr[k] = R[j];

j++;

k++;

}

}

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);

}

}

void printArray( int A[], int size)

{

int i;

for (i=0; i < size; i++)

printf ( "%d " , A[i]);

printf ( "

" );

}

int main()

{

int arr[] = {12, 11, 13, 5, 6, 7};

int arr_size = sizeof (arr)/ sizeof (arr[0]);

printf ( "Given array is

" );

printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf ( "

Sorted array is

" );

printArray(arr, arr_size);

return 0;

}





filter_none

edit

close

play_arrow

link

brightness_4

code def mergeSort(arr): if len (arr) > 1 : mid = len (arr) / / 2 L = arr[:mid] R = arr[mid:] mergeSort(L) mergeSort(R) i = j = k = 0 while i < len (L) and j < len (R): if L[i] < R[j]: arr[k] = L[i] i + = 1 else : arr[k] = R[j] j + = 1 k + = 1 while i < len (L): arr[k] = L[i] i + = 1 k + = 1 while j < len (R): arr[k] = R[j] j + = 1 k + = 1 def printList(arr): for i in range ( len (arr)): print (arr[i],end = " " ) print () if __name__ = = '__main__' : arr = [ 12 , 11 , 13 , 5 , 6 , 7 ] print ( "Given array is" , end = "

" ) printList(arr) mergeSort(arr) print ( "Sorted array is: " , end = "

" ) printList(arr) chevron_right

filter_none



Given array is 12 11 13 5 6 7 Sorted array is 5 6 7 11 12 13

#include <bits/stdc++.h>

using namespace std;

void swap( int * a, int * b)

{

int t = *a;

*a = *b;

*b = t;

}

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);

}

}

void printArray( int arr[], int size)

{

int i;

for (i = 0; i < size; i++)

cout << arr[i] << " " ;

cout << endl;

}

int main()

{

int arr[] = {10, 7, 8, 9, 1, 5};

int n = sizeof (arr) / sizeof (arr[0]);

quickSort(arr, 0, n - 1);

cout << "Sorted array:

" ;

printArray(arr, n);

return 0;

}





filter_none

edit

close

play_arrow

link

brightness_4

code using System; class GFG { static int partition( int []arr, int low, int high) { int pivot = arr[high]; int i = (low - 1); for ( int j = low; j < high; j++) { if (arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp1 = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp1; return i+1; } static 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); } } static void printArray( int []arr, int n) { for ( int i = 0; i < n; ++i) Console.Write(arr[i] + " " ); Console.WriteLine(); } public static void Main() { int []arr = {10, 7, 8, 9, 1, 5}; int n = arr.Length; quickSort(arr, 0, n-1); Console.WriteLine( "sorted array " ); printArray(arr, n); } } chevron_right

filter_none



Sorted array: 1 5 7 8 9 10

A trick to choose the best algorithm:





You can follow me on Quora and ask any doubt related to sorting or any programming topic. Here's the link: https://www.quora.com/profile/Mayank-Mewar-3













Now and then we have to sort our information. Possibly we have a rundown of names or a rundown of numbers, and we need them to be all together. There is a wide range of sorting techniques, yet some are superior to other people.(Furthermore, in a meeting, you might be asked which is ideal.). In any case, we should see when to utilize each sorting technique.- How quick is an algorithm? We care about the average and worst-case scenarios. O(N) is godly, O(NlogN) is the standard, O(N^2) is bad, usually.- How much extra space does a sorting algorithm need? (Set up implies no additional room is required, which is great.)- Will the order of equivalent information/data be preserved? For example, in [2, 4(a), 1, 5, 4(b), 3] if the first 4 (labeled as 4(a)) ends up before the second 4 (labeled as (4b)) in the sorted array, like [1, 2, 3, 4(a), 4(b), 5], then the sorting algorithm is stable.- There's a cost related to swapping two components in a cluster/list. In the case of swapping is costly, at that point we might need to pick a calculation that does as not many swaps as would be prudent.- Sometimes, the information isn't arbitrarily sorted out, which influences how a calculation performs. For instance, possibly we have sorted list and include a couple of numbers toward its finish. A few calculations improve on, for the most part, arranged information than others.- Sometimes we have to sort data that are so huge that it won't fit into RAM , meaning we need to use a special algorithm that will work on data stored on a hard driveThere are a ton of sorting methods, however since we're just taking a gander at this for an inquiry question, we'll look at the four best ones.- The least difficult arranging calculation: Start at the primary component of an exhibit. Search through every one of the components left in the exhibit, and monitor which one is the smallest. If the current number is greater than the present minimum value then swap them.Time: O(N^2).Space: O(1).Stable: No.Swaps: O(N). In the event that information is now arranged:O(N^2).selection sort is straightforward, yet just helpful if swaps are costly.Merge sort divides an array into 2 parts, forming two subarrays. It cuts those subarrays in half and repeats the process until the subarrays are one element in size. Merge sort then merges the subarrays by selectively choosing the smallest elements to put in first, and builds the subarrays back up in size until the full array is recreated, in sorted form.Time: O(NlogN).Space: O(N).Stable: Yes.If the data is already sorted: O(NlogN).Merge sort is great, and the technique is good for sorting data that can't fit in RAM().Choose an element as the "pivot" element, and go through the array, moving all elements smaller than the pivot to the left, and all the larger elements to the right. Then, for each "left" and "right" subarray, do the same process of picking a pivot and moving elements around().Time: O(N^2) in the worst case, but usually acts like O(NlogN).Space: O(1).Stable: If the given data is already sorted: O(N^2) depending on the pivot's position.Quicksort is generally quicker than merge sort, so ordinarily, it's utilized. But it does have a worst case of O(N^2) so don't choose it if we absolutely need O(NlogN) to be guaranteed.Output:(P.S. In reality, most quick sort calculations randomize the dataset before arranging it, to help maintain a strategic distance from the worst-case scenarios.)