# C + + nine sort specific implementation code

Time：2021-3-6

The content of this article will be updated according to the needs of bloggers. I hope you will pay more attention to it.

Direct insertion sort

``````
void InsertSort(int r[])
{
int n = sizeof(r) / sizeof(r[0]);
for(int i = 1; i < n; ++i)
{
for(int j = i - 1; j >= 0; --j)
{
if(r[j+1] < r[j])
{
int s = r[j+1];
r[j+1] = r[j];
r[j] = s;
}
}
}
}
``````

Binary Insertion Sort

``````void BinInsertSort(int r[])
{
int n = sizeof(r) / sizeof(r[0]);
for(int i = 1; i < n; ++i)
{
int s = r[i];
int low = 0;
int high = i - 1;

while(low <= high)
{
Int mid = (low + high) / 2; // the mid position is the number to find
if(s < r[mid])
high = mid - 1;
else
low = mid + 1;
}

For (int j = I - 1; J > = high + 1; -- J) // high + 1, i.e. mid, the number of execution moves backward until the number of mid moves backward
r[j+1] = r[j];

R [high + 1] = s; // the mid position stores itself
}
}``````

Shell Sort

``````
void ShellSort(int r[])
{
int n = sizeof(r) / sizeof(r[0]);
int step = n / 2;

while(step >= 1)
{
for(int i = step; i < n; ++i)
{
for(int j = i - step; j >= 0; j -= step)
{
if(r[j+step] < r[j])
{
int s = r[j+step];
r[j+step] = r[j];
r[j] = s;
}
}
}
step /= 2;
}
}
``````

Direct selection sort

``````
void SelectSort(int r[])
{
int n = sizeof(r) / sizeof(r[0]);
for(int i = 0; i < n - 1; ++i)
{
int samll = i;
for(int j = i + 1; j < n; ++j)
{
if(r[small] > r[j])
samll = j;
}
if(small != i)
{
int s = r[i];
r[i] = r[small];
r[small] = s;
}
}
}
``````

Heap sort

``````Void heapadjust (int r [] int i; int j) // adjust heap
{
int child = 2 * i;
Int s = R [i]; // S stores node data temporarily
while(child <= j)
{
If (child < J & & R [child + 1] > R [child]) // compare 2 subtrees
++child;
If (s > = R [child]) // compare node with big subtree
break;
R [child / 2] = R [child]; // if the big subtree is bigger than the node, swap
Child = 2 * child; // continue to search the subtree
}
R [child / 2] = s; // the number of nodes is the largest
}

Void HEAPSORT (int r [] // build heap
{
int n = sizeof(r) / sizeof(r[0]);
For (int i = n / 2-1; I > = 0; -- I) // only subscripts before N / 2-1 have subtrees
{
Heapadjust (R, I, N - 1); // construct the big top heap, the nodes are larger than the subtree, and the last root node is the largest number
}
for(int i = n - 1; i > 0; --i)
{
//Swap the current top element with the current tail element, that is, move the maximum number to the end
int s = r[0];
r[0] = r[i];
r[i] = s;
Heapadjust (R, 0, I - 1); // continue to adjust the remaining elements, and finally change the order from small to large
}
}``````

Bubble sort

``````
void BubbleSort(int r[])
{
int n = sizeof(r) / sizeof(r[0]);
for(int i = 0; i < n - 1; ++i)
{
for(int j = 0; j < n - 1 - i; ++j)
{
if(r[j] > r[j+1])
{
int s = r[j];
r[j] = r[j+1];
r[j+1] = s;
}
}
}
}
``````

Quick sort

``````int Partition(int r[], int low, int high)
{
int pivotkey = r[low];
int i = low;
int j = high;

while(i < j)
{
While (I < J & & R [J] > pivot key) // find the first number smaller than pivot key from J
--j;
if(i < j)
{
r[i] = r[j];
++i;
}

While (I < J & & R [i] < pivot key) // look from I to find the first number larger than pivot key
++i;
if(i < j)
{
r[j] = r[i];
--j;
}
}
R [J] = pivot key; // finish the final exchange
Return J; // returns the demarcation point. The number in front is smaller than pivot key, and the number in the back is larger than pivot key
}

Void quicksort (int r [] int low, int high) // pass array, 0 and length - 1
{
if(low < high)
{
int pivot = Partition(r, low, high);
Quicksort (R, low, pivot - 1); // recursion, the first half of the Quicksort continues
Quicksort (R, pivot + 1; high); // recursion, and quicksort continues in the second half
}
}``````

Merge sort

``````void copyArray(int source[], int dest[], int len, int first)
{
int i;
int j = first;
for(i = 0; i < len; ++i)
{
dest[j] = source[i];
++j;
}
}

void merge(int a[], int left, int right)
{
int begin1 = left;
int mid = (left + right) / 2;
int begin2 = mid + 1;
int newArrayLen = right - left + 1;
int *b = (int*)malloc(newArrayLen * sizeof(int));
int k = 0;

While (begin1 < = mid & & begin2 < = right) // find out the smaller number in the two groups and put it into space B in order
{
if(a[begin1] <= a[begin2])
b[k++] = a[begin1++];
else
b[k++] = a[begin2++];
}

//Put the rest in space B
while(begin1 <= mid)
b[k++] = a[begin1++];
while(begin2 <= right)
b[k++] = a[begin2++];

Copyarray (B, a, newarraylen, left); // write the number of space B back to the original array
free(b);
}

Void mergesort (int r [] int left, int right) // pass array, 0 and length - 1
{
int i;
//At least two elements are sorted
if(left < right)
{
i = (left + right) / 2;
Mergesort (R, left, I); // recursion in the first half
Mergesort (R, I + 1, right); // recursion in the second half
Merge (R, left, right); // the comparison order of 10 numbers is [0,1] [0,2] [3,4] [0,4] [5,6] [5,7] [8,9] [5,9] [0,9]
}
}``````

Bucket sorting

``````void insert(list<int> &bucket, int val)
{
auto iter = bucket.begin();
while(iter != bucket.end() && val >= *iter)
++iter;
//Insert will insert data before ITER, which can stabilize sorting
bucket.insert(iter, val);
}

void BuckdeSort(vector<int> &arr)
{
int len = arr.size();
if(len <= 1)
return;
int min = arr[0], max = min;
For (int i = 1; I < len; + + I) // find the minimum and maximum
{
if(min > arr[i])
min = arr[i];
if(max < arr[i])
max = arr[i];
}

Int k = 10; // the size of the bucket

//If [0,9] has 10 numbers, then (9-0) / K + 1 = 1 bucket
Int bucketsnum = (max - min) / K + 1; // number of buckets

vector<list<int>> buckets(bucketsNum);
for(int i = 0; i < len; ++i)
{
int value = arr[i];
//(value min) / K is in which bucket
Insert (buckets [(value min) / k], value); // put the data into each bucket and sort it
}

int index = 0;
for(int i = 0; i < bucketsNum; ++i)
{
if(buckets[i].size())
{
for(auto &value : buckets[i])
arr[index++] = value;
}
}
}``````

So far, this article about the specific implementation code of C + + nine sorts of sorting is introduced here. For more related C + + sorting content, please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!