Common Classic Sorting algorithms

Source: Internet
Author: User
Tags benchmark

Common Classic Sorting algorithms



1. Hill sort

2. Two-point insertion method
3. Direct Insertion method
4. Direct sequencing with Sentinel
5. Bubble sort
6. Select Sort
7. Quick Sort
8. Heap Sequencing






I. Hill (Shell) Sort method (also known as the small increments of the house, which was presented by D.l.shell in 1959)


/* Shell Sort Method */
#include <stdio.h>


void sort (int v[],int n)
{
int gap,i,j,temp;
For (Gap=n/2;gap>0;gap/= 2)/* Set the step size of the sort, step gap halved each time, until reduced to 1 */
{
for (i=gap;i<n;i++)/* Navigate to each element */
{
for (J=i-gap; (j >= 0) && (v[j] > V[j+gap])//* Compare the size of two elements far apart from the gap, depending on the sort direction how to exchange */
{
TEMP=V[J];
V[J]=V[J+GAP];
V[j+gap]=temp;
}
}
}
}











Two. Two-point insertion method


/* Binary Insertion method */
void Halfinsertsort (int a[], int len)
{
int I, j,temp;
int low, high, mid;
for (I=1; i<len; i++)
{
temp = a[i];/* Save But former element */
Low = 0;
High = i-1;
while (low <= high)/* in A[low...high] binary find the position of an orderly insert */
{
Mid = (low + high)/2; /* Find the middle element */
if (A[mid] > Temp)/* If the intermediate element is larger than the previous element, the current element is inserted to the left of the intermediate element.
{
High = mid-1;
}
else/* If the intermediate element is smaller than the current element, but the former element is inserted to the right of the middle element */
{
low = mid+1;
}
}/* Finds the position of the current element, between low and high */
for (j=i-1; j>high; j--)/* element move back */
{
A[J+1] = A[j];
}
A[HIGH+1] = temp; /* Insert */
}
}





Three. Direct Insertion method


/* Direct Insert Method */


void Insertionsort (int input[],int len)
{
int i,j,temp;
for (i = 1; i < Len; i++)
{
temp = Input[i]; /* operation of the current element, first saved in other variables */
for (j = i-1;j>-1&&input[j] > temp; j--)/* Find the appropriate location starting from the previous element of the current element */
{
Input[j + 1] = Input[j]; /* Move the element while looking for one side */
INPUT[J] = temp;
}
}
}





Four. Direct sequencing with Sentinel


/**
* Direct Insert sort with Sentinel, the first element of the array is not used to store valid data
* Input[0] As Sentinel, you can avoid the decision INPUT[J], whether the array is out of bounds
* Because in the process of j--, when J decreases to 0 o'clock, it becomes input[0] and input[0]
* Compare yourself, it is obvious at this time that the number before position I is smaller than input[i]
* The number on position I does not need to move, directly into the next round of insert comparison.
*
*/
void Insertionsortwithpiquet (int input[],int len)
{
int i,j;
for (i = 2; i < Len; i++)/* guarantees that the stored data of the first element of the array input is invalid, starting with the second data and comparing it with the elements in front of it */
{
Input[0] = Input[i];
for (j = i-1; Input[j] > Input[0]; j--)
{
Input[j + 1] = Input[j];
INPUT[J] = input[0]; /* INPUT[J] has always been the largest of the ordered elements */
}
}
}





Five. Bubbling method


/* Bubble Sort Method */
void Bublesort (int a[],int N)
{
int i,j,k;
for (j=0;j<n;j++)/* Bubble method to sort n times */
{
If the for (i=0;i<n-j;i++)/* value is more than the larger element sinks, only the maximum value of the remaining elements can be sunk down.
{
if (a[i]>a[i+1])/* Sink the value to the bottom of the larger element */
{
K=a[i];
A[I]=A[I+1];
A[i+1]=k;
}
}
}
}





Six. Select the Sorting method





/* Algorithm principle: first with an element as the benchmark, start scanning in one Direction,
* such as from left to right scanning, with a[0] as the benchmark. Next from a[0] ... A[9]
* Find the smallest element and swap it with a[0]. The base position is then right
* Move one, repeat the above action, for example, with A[1] as the benchmark, find out
* A[1]~a[9], the smallest of which is exchanged with a[1]. Continue to the base position
* Sort ends when moving to the last element of the array (all elements to the left of the datum at this time
* are incremented and ordered, and the benchmark is the last element, so the order is completed.
*/
void Selectsort (int a[],int N)
{
int i,j,min,temp;
for (i=0;i<n;i++)
{
Min=i;
for (j=i+1;j<=n;j++)/* Data from J forward is all lined up, so start with J to find the smallest of the remaining elements */
{
if (A[min]>a[j])/* Put the smallest of the remaining elements in a[i] */
{
Temp=a[i];
A[I]=A[J];
A[j]=temp;
}
}
}
}





Seven. Quick Sort


/* Fast Sorting (quick sort). In this approach,
* n elements are divided into three segments (groups): Left segment,
* Right and middle section middle. Middle
* contains only one element. The elements in the left segment are less than equal
* In the middle element, each element in the right paragraph is greater than or equal to
* Segment element. So the meta in left and right
* The vegetarian can be sorted independently and does not have to be left and
* The sorted results of right are merged.
* Sort a[0:n-1 by using quick Sort method
* Select an element from a[0:n-1] as the middle,
* This element splits the remaining elements into two segments left for Fulcrum
* And right, so that the left element is less than
* equals the fulcrum, and the element in right is greater than or equal to the fulcrum
* Sort left recursively by using quick Sort method
* Sort right recursively by using quick Sort method
* Results obtained are left+middle+right
*/


void Quick_sort (int data[],int low,int High)
{
int mid;
if (Low{
Mid=partition (Data,low,high);
Quick_sort (data,low,mid-1); /* Recursive call */
Quick_sort (Data,mid+1,high);
}
}
/* Be careful to see how the following data is replaced,
* First select an intermediate value, which is the first element Data[low],
* Then from the far right of the element to find the element smaller than it, put
* The element is copied to its median value (Data[low]=data[high]),
* Then find the element larger than it from the far left of the element and put
* This element is copied to the position of the element just found above (Data[high]=data[low]),
* Finally, the newly vacated position is loaded into the median value (data[low]=data[0]),
* This will go to the right of mid, which is smaller than mid to the left,
* The last line, the return of the low is the position of the middle element, left and right respectively recursive can be ordered.
*/


int Partition (int *r,int i,int j)
{
int pivot=r[i];
while (I&LT;J)
{
while (I<j&&r[j]>=pivot)
j--;
if (I&LT;J)
R[I++]=R[J];

while (I<j&&r[i]<=pivot)
i++;
if (I&LT;J)
R[j--]=r[i];
}
R[i]=pivot;
return i;
}
void QuickSort (int *r,int low,int High)
{
int pivotpos;
if (Low{
Pivotpos=partition (R,low,high);
QuickSort (R,LOW,PIVOTPOS-1);
QuickSort (R,pivotpos+1,high);
}
}//quicksort







Eight. Heap sequencing


/**************************************************************
* Heap definition n elements of sequence {k1,k2,..., kn} When and only if the following relationships are met
* Called Heap:
* Ki<=k2i ki<=k2i+1 (i=1,2,..., N/2)
* OR
* Ki>=k2i ki>=k2i+1 (i=1,2,..., N/2)
* Heap Sorting ideas:
* Established on the basis of the tree-shape selection sort;
* After the backlog is built into a heap (initial heap generation), the first element of the sequence (the top element of the heap) must be the largest element in the sequence;
* Swap it with the last element of the sequence, reducing the length of the sequence by one;
* After the sequence is built into the heap (heap adjustment), the top element of the heap is still the largest element in the sequence, exchanging it with the last element of the sequence again and shortening the sequence length;
* Repeat this process until the sequence length is one, and the resulting sequence is the sorted result.
**************************************************************/
void Heapadjust (int data[],int s,int m)/* Arrange piles of form */
{
int J,RC;
Rc=data[s]; /* Save processing Element */
for (j=2*s;j<=m;j*=2)/* Process father element */
{
if (j<m && data[j]<data[j+1]) ++j; /* Take a larger child node */
if (Rc>data[j]) break;
DATA[S]=DATA[J]; /* Parent node larger child node large swap, ensure that the parent node is larger than all child nodes (parent node stored in front) */
S=j;
}
DATA[S]=RC; /* Equivalent to DATA[J]=RC */
}


void Heap_sort (int data[],int long_n)/* Heap sort function */
{
int i,temp;
for (i=long_n/2;i>0;--i)/* has not read the reason for such processing, I hope you will be very generous * *
{
Heapadjust (Data,i,long_n); /* After processing, data[i] is the maximum value of the second half of the array */
}
for (I=long_n;i>0;--i)
{
TEMP=DATA[1]; /* Put the root element (the largest of the remaining elements) to the end, and the next time the remaining number will be OK */
Data[1]=data[i];
Data[i]=temp;
Heapadjust (data,1,i-1);
}
}

Common Classic Sorting algorithms

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.