MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / insertionsort

Function insertionsort

CPP/sorting/intro_sort.cpp:8–30  ·  view source on GitHub ↗

Function to perform insertion sort on subarray `a[low…high]`

Source from the content-addressed store, hash-verified

6
7// Function to perform insertion sort on subarray `a[low…high]`
8void insertionsort(int a[], int low, int high)
9{
10 // start from the second element in the subarray
11 // (the element at index `low` is already sorted)
12 for (int i = low + 1; i <= high; i++)
13 {
14 int value = a[i];
15 int j = i;
16
17 // find index `j` within the sorted subset a[0…i-1]
18 // where element a[i] belongs
19 while (j > low && a[j - 1] > value)
20 {
21 a[j] = a[j - 1];
22 j--;
23 }
24
25 // Note that the subarray `a[j…i-1]` is shifted to
26 // the right by one position, i.e., `a[j+1…i]`
27
28 a[j] = value;
29 }
30}
31
32// Function to partition the array using Lomuto partition scheme
33int partition(int a[], int low, int high)

Callers 1

introsortFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected