C-Language sorting algorithm

Source: Internet
Author: User

Summarize the common sorting algorithm in C, although most languages already provide sorting algorithms, such as the C function library provides the qsort sorting function (internal for the fast sort implementation), but understanding the idea of sorting algorithm far more than the practical value. Here I summarize the commonly used sorting algorithm, and implemented in C language. The writing order of these algorithms is also related, for example, the hill sort is an improvement to the insertion algorithm, and the fast sorting is an improvement to the bubbling sort, and the fast sorting and merging sorts are implemented recursively.
Note: The implementation of each method provides the same formal parameter list as much as possible. This does not involve the implementation of algorithms such as heap sequencing, box sorting, and so on.

Insert Sort
Algorithm Summary: Insert sort by traversing to the nth element when the N-1 element in front of it is already sorted, then find the previous N-1 element to place the nth element in the appropriate position, so that it goes through the elements of the sequence.
Code:
voidInsertsort (intArray[],intLength
{
intKey
for(intI=1; i<length; i++)
{
key = Array[i];
for(intj=i-1; j>=0&& array[j] > key; j--)
{
array[j+1] = Array[j];
}
array[j+1] = key;
}
}
Hill sort

Algorithm Summary: The shell sort is a modification of the insertion sort, it gets a sequence based on an increment for each sort sort, inserts the sequence of this subsequence, and then increments the number of elements of the subsequence by shrinking incrementally until the 1 increment is the same as the original order sequence. It is only necessary to do a small amount of comparison and movement to complete the sequencing of the sequence.
Code:
voidShellsort (intArray[],intLength
{
intKey
intIncrement
for(Increment = length/2; Increment>0; Increment/=2)
{
for(intI=increment; i<length; i++)
{
key = Array[i];
for(intj = i-increment; j>=0&& array[j] > key; J-= increment)
{
Array[j+increment] = Array[j];
}
Array[j+increment]=key;
}
}

}

Bubble sort
Algorithm Summary: The bubble sort is completed by N-1, the first sub-order from the 1th number to the number of n-i, if the number of I is greater than the next number (then ascending, small, descending) will be exchanged two of times.
Code:
voidBubblesort (intArray[],intLength
{
intFlag =0;
for(intI=0; i<length-1; i++)
{
for(intj=0; j<length-1-I.; J + +)
{
if(array[j]>array[j+1])
{
Flag =1;
ARRAY[J] = Array[j] + array[j+1];
array[j+1] = Array[j]-array[j+1];
ARRAY[J] = array[j]-array[j+1];
}
}
if(Flag = =0) Break;
}
}

Quick Sort
Quick Sort (Quicksort) is an improvement to the bubbling sort. By sorting the sorted data into separate two parts, one part of all data is smaller than the other part of the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.
Code:
intSort (intArray[],intFirstintLast
{
intPivot = Array[first];
intTemp
if(Last-first <=0)return-1;

while(First! = last)
{
while(Array[last] >= pivot && last! = first) last--;
temp = Array[first];
Array[first] = Array[last];
Array[last] = temp;
while(Array[first] <= pivot && last! = first) first++;
temp = Array[first];
Array[first] = Array[last];
Array[last] = temp;
}
returnLast
}

voidQuickSort (intArray[],intLength
{
inttemp = Sort (array,0, length-1);
if(temp = =-1)return;
QuickSort (array,temp+1);
QuickSort (&array[temp+1],length-temp-1);
}

Merge sort
Algorithm Summary: Merge sorting method is to combine two (or more than two) ordered tables into a new ordered table, that is, to sort the sequence into a number of sub-sequences, each sub-sequence is ordered. Then the ordered subsequence is combined into a whole ordered sequence.
Code:
voidMerge (intA[],intLowintMidintHigh
{
intI,j,k;
int*p =Newint[mid-low+1],*q =New int[High-mid];

for(i =0; I < Mid-Low +1; ++i) P[i] = A[i+low];

for(i =0; I < high-mid;++i) Q[i] = a[mid+1+i];

i = j =0, k = low;
while((i <= Mid-Low) && (J <= high-mid-1))
{

if(P[i] <= q[j]) a[k++] = p[i++];
ElseA[k++]= q[j++];
}

if(i > Mid-Low) { for(; J <= high-mid-1; ++j) a[k++] = Q[j]; }
Else
{ for(; I <= mid-low;++i)  a[k++] = P[i]; }

delete [] P;
delete [] Q;
}

voidMergeSort (intA[],intLeftintRight
{
if(Left < right)
{
inti = (left + right)/2;
MergeSort (A,left,i);
MergeSort (a,i+1, right);
Merge (A,left,i,right);
}
}

C-Language sorting algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.