C Language Implementation Insert sort, bubble sort, choose sort, quick sort, heap sort, merge sort, Hill sort example, need friend can refer to next
Implement the following sort
Insert sort O (n^2)
Bubble sort O (n^2)
Select Sort O (n^2)
Quick Sort O (n log n)
Heap sort O (n log n)
Merge sort O (n log n)
Hill sort O (n^1.25)
1. Insert sort O (n^2)
In general, the insertion sort is implemented using In-place on the array. The specific algorithm is described as follows:
⒈ starting with the first element, the element can be thought to have been sorted
⒉ takes the next element and scans the sequence of elements that have been sorted from backward forward
⒊ if the element (sorted) is greater than the new element, move the element to the next position
⒋ Repeat step 3 until the sorted element is found to be less than or equal to the position of the new element
⒌ inserting new elements into the next position
⒍ Repeat steps 2~5
If the cost of the comparison operation is larger than the exchange operation, a binary lookup method can be used to reduce the number of comparison operations. The algorithm can be thought of as a variant of the insertion sort, called a binary lookup sort.
Copy CodeThe code is as follows:
void Insert_sort (int* array,unsignedint N) {
int i,j;
int temp;
for (i=1;i<n;i++) {
temp=* (Array+i);
For (j=i;j>0&&* (array+j-1) >temp;j--) {
* (ARRAY+J) =* (array+j-1);
}
* (ARRAY+J) =temp;
}
}
2. Bubble sort O (n^2)
The bubbling sorting algorithm works as follows:
Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
Repeat the above steps for all elements, except for the last one.
Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
Copy CodeThe code is as follows:
#include <stdio.h>
#defineSIZE8
void Bublle_sort (int a[],int n) {//n number of elements in array a
int i,j,temp;
for (j=0;j<n-1;j++)
for (i=0;i<n-1-j;i++)
if (A[i]>a[i+1]) {//array element size sorted in ascending order
Temp=a[i];
A[I]=A[I+1];
A[i+1]=temp;
}
}
int main () {
int number[size]={95,45,15,78,84,51,24,12};
int i;
Bublle_sort (number,size);
for (i=0;i<size;i++) {
printf ("%d", number[i]);
}
printf ("\ n");
}
3. Select Sort O (n^2)
Copy CodeThe code is as follows:
void Select_sort (int * A, int n) {
Register int i, j, Min, T;
for (i =0; i < n-1; i + +) {
min = i; Find Minimum value
for (j = i +1; j < N; j + +)
if (A[min] > A[j])
min = j; Exchange
if (min! = i) {
t = a[min];
A[min] = A[i];
A[i] = t;
}
}
}
4. Quick Sort O (n log n)
Copy CodeThe code is as follows:
void QuickSort (int a[],int numsize) {//a is a shaped array, numsize is the number of elements
int i=0,j=numsize-1;
int val=a[0];//specified reference value val size
if (numsize>1) {//Ensure that the array is at least 2 long, otherwise you do not need to sort
while (i<j{//Loop End condition
for (; j>i;j--)//search for elements smaller than Val from the back, find and fill in a[i] and jump out of the loop
if (a[j]<val) {
A[i]=a[j];break;
}
for (; i<j;i++)//search for elements larger than Val after the trip, find and fill in a[j] and jump out of the loop
if (a[i]>val) {
A[j]=a[i];break;
}
}
a[i]=val;//Place the number saved in Val into A[i]
QuickSort (a,i);//recursion, order of number of first I
QuickSort (a+i+1,numsize-1-i);//For i+1 to numsize this numsize-1-i number sort
}
}
5. Heap sort O (n log n)
n A sequence of keywords kl,k2,...,kn called (heap), when and only if the sequence satisfies the following properties (for short, heap properties):
(1) ki<=k (2i) and Ki<=k (2i+1) (1≤i≤n), of course, this is the small Gan, Dagen is replaced by the >= number. K (i) corresponds to a non-leaf node of a two-fork tree, and K (2i) is the left Dial hand node, and K (2i+1) is the right child node.
If the vector stored in this sequence is R[1..N] as a storage structure of a complete binary tree, then the heap is essentially a complete binary tree satisfying the following properties: The keywords of any non-leaf node in the tree are not greater than (or not less than) the keywords of their children (if any) nodes.
Copy CodeThe code is as follows:
Array is the heap array to be adjusted, I is the position of the array element to be adjusted, Nlength is the length of the array
The function is to construct a large heap based on an array of arrays
void Heapadjust (int array[], int i, int nlength)
{
int nchild;
int ntemp;
for (ntemp = array[i]; 2 * i + 1 < nlength; i = nchild)
{
Location of child nodes =2* (parent node position) + 1
Nchild = 2 * i + 1;
Get the larger nodes in the sub-nodes.
if (Nchild < nLength-1 && array[nchild + 1] > Array[nchild])
++nchild;
If the larger child node is larger than the parent node, move the larger child node up, replacing its parent node.
if (Ntemp < Array[nchild])
{
Array[i] = Array[nchild];
Array[nchild]= ntemp;
}
Else
Otherwise exit the loop
Break
}
}
Heap Sorting algorithm
void heapsort (int array[],int length)
{
int tmp;
Adjusts the first half of the sequence of elements, after which it is the largest element of a sequence
Length/2-1 is the first non-leaf node, where "/" is divisible
for (int i = floor (length-1)/2; I >= 0; i.)
Heapadjust (array, I, length);
Adjusts the sequence from the last element, shrinking the adjustment until the first element
for (int i = length-1; i > 0; i.)
{
Swap the first element with the last element of the current one,
The element that guarantees the current last position is the largest in this sequence now.
Swap (&array[0], &array[i]);
TMP = Array[i];
Array[i] = array[0];
ARRAY[0] = tmp;
Continuously reduce the range of the adjustment heap to ensure that the first element is the maximum value of the current sequence at each adjustment
Heapadjust (array, 0, i);
}
}
6. Merge sort O (n log n)
The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If two ordered tables are combined into an ordered table, they are called two-way merging.
Copy CodeThe code is as follows:
Merge operations
void Merge (int sourcearr[], int targetarr[], int startIndex, int midindex, int endIndex)
{
int I, j, K;
for (i = midindex+1, j = startIndex; StartIndex <= midindex && i <= EndIndex; j + +)
{
if (Sourcearr[startindex] < sourcearr[i])
{
TARGETARR[J] = sourcearr[startindex++];
}
Else
{
TARGETARR[J] = sourcearr[i++];
}
}
if (StartIndex <= midindex)
{
for (k = 0; k <= midindex-startindex; k++)
{
TARGETARR[J+K] = sourcearr[startindex+k];
}
}
if (i <= endIndex)
{
for (k = 0; k <= endindex-i; k++)
{
TARGETARR[J+K] = sourcearr[i+k];
}
}
}
Internal use recursion, spatial complexity of N+LOGN
void mergesort (int sourcearr[], int targetarr[], int startIndex, int endIndex)
{
int midindex;
int temparr[100]; The size here changes according to requirements
if (StartIndex = = EndIndex)
{
Targetarr[startindex] = Sourcearr[startindex];
}
Else
{
Midindex = (StartIndex + endIndex)/2;
MergeSort (Sourcearr, Temparr, StartIndex, Midindex);
MergeSort (Sourcearr, Temparr, midindex+1, EndIndex);
Merge (Temparr, Targetarr,startindex, Midindex, EndIndex);
}
}
Call
int _tmain (int argc, _tchar* argv[])
{
int a[8]={50,10,20,30,70,40,80,60};
int b[8];
MergeSort (A, B, 0, 7);
for (int i = 0; i < sizeof (a)/sizeof (*A); i++)
cout << B[i] << ";
cout << Endl;
System ("pause");
return 0;
}
7. Hill sort O (n^1.25)
First, take an integer less than n D1 as the first increment, dividing all the records of the file into D1 groups. All records with a multiple of D1 are placed in the same group. The direct insert sort is performed in each group first, then the second increment d2<d1 repeats the above groupings and sorts until the increment dt=1 (DT<DT-L<...<D2<D1) is taken, that is, all the records are placed in the same group for direct insert sorting.
Copy CodeThe code is as follows:
void Shellsort (int a[], int n) {
int d, I, J, temp;
for (d = n/2;d >= 1;d = D/2) {
for (i = D; i < n;i++) {
temp = A[i];
for (j = i-d; (j >= 0) && (a[j] > Temp); j = j-d) {
A[j + d] = a[j];
}
A[j + d] = temp;
}
}
}
Sorting algorithm C implementation