Sorting Algorithms: Farrukh Zeshan Ahmad
Sorting Algorithms: Farrukh Zeshan Ahmad
Insertion Sort
Insertion Sort
void insertion_sort (int arr[], int length)
{
int j, temp;
for (int i = i; i < length; i++)
{
j = i;
while (j > 0 && arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp; j--;
}
}
}
Insertion sort sorts the elements in place
Merg Sort
Merg Sort
Merg Sort
Algorithm
selection-sort
insertion-sort
heap-sort
merge-sort
Time
Notes
O(n2)
slow
in-place
for small data sets (< 1K)
O(n2)
slow
in-place
for small data sets (< 1K)
O(n log n)
fast
in-place
for large data sets (1K 1M)
O(n log n)
fast
sequential data access
for huge data sets (> 1M)