Basic sorting algorithm

Source: Internet
Author: User

Seven basic sorting algorithms (all internally sorted):
Direct Insert Sort
Hill sort
Bubble sort
Simple selection sorting
Quick Sort
Heap Sort
Two-way merge sort

Sorting algorithm Stability: After sorting, the relative order between elements with the same key code remains constant, then the ordering method is called stable; otherwise unstable.

Direct Insert Sort:

void Insertsort (inta[],int N) {//index start at1,a[0] is temp one int i,j;for (i=2; i<=n;i++) {        if(a[i]<a[I1]){a[0]=aI;            a[i]=a[I1];for (j=i-2; a[j]>a[0];j--) {            a[j+1]=a[j];}a[j+1]=a[0];}    }}

Direct Insert sort is a stable sort, time complexity O (n^2), spatial complexity is O (1)

Hill Sort:

The elements are divided into subsets by increments, and the subsets are continuously sorted by insertion.

void Shellsort (inta[],int N) {//index start at1,a[0] is temp one int d,i,j,k;for (d=n/2;d >=1;d>>=1) {for (i=d+1; i<=n;i++) {//Insertsort        if(a[i]<a[I-d]) {a[0]=aI;           a[i]=a[I-d];for (j=i-2*d; j>0&&a[0]<a[j];j-=d) {               a[j+d]=a[j];}a[j+d]=a[0];}    }    }}

If a[i]>a[i-d] is always true, then the time is O (Nlogn), but in the worst case it is O (n^2). Space complexity is O (1)
Hill sort is an unstable sort of method

Bubble Sort:

Adjacent elements if reversed 22 are exchanged until all positions are determined.

void BubbleSort(int a[],int// index start at 1, a[0] is temp one     int i,j,k;    for(i=1;i<=n;i++){        for(j=1;j<=n-i;j++){        if(a[j]>a[j+1]) {            a[j]=a[j]^a[j+1];  a[j+1]=a[j]^a[j+1];   a[j]=a[j]^a[j+1];        }    }    }}

This is a stable sorting method, time complexity: O (n^2)

Quick sort:

Select an axis value so that the value of the left element is less than it, and the value of the element on the right is greater than it. Repeat the appeal process for the resulting partition. The algorithm is an improvement on the bubbling sort.

intPartion (intA[],intStartintEnd) {intI=start,j=end;intTemp=a[start]; while(I&LT;J) { while(I<j && a[j]>=temp) j--; A[I]=A[J];//I is more         while(I<j && a[i]<=temp) i++; A[j]=a[i];//J is more} a[i]=temp;//At end, I=j    returni;}voidQsort (intA[],intStartintEnd) {if(Start<end) {intD=partion (A,start,end);        Qsort (A,START,D); Qsort (a,d+1, end); }}

Fast sorting is not a stable sorting algorithm. On average, the time complexity of Qsort is O (nlogn)

Simple selection Sort:

Thought: The first trip will be sorted record R[I......N] The smallest element and R[i] Exchange

void SelectSort(int a[],int n){    for(int i=1;i<n;i++){       int dex=i;       for(int j=i+1;j<=n;j++){           if(a[dex]>a[j]) dex=j;   //useindexand find min one       }       if(dex!=i)  {           a[dex]=a[dex]^a[i];   a[i]=a[dex]^a[i];   a[dex]=a[dex]^a[i];         }    }}
Heap Sort:

The heap is divided into Dagen and small Gan. The parent node is larger or smaller than the left or right child.
The nature of the maintenance heap:

Heap sequencing idea: Build the heap first, build the heap from the bottom up. The root node is then fetched and output, and the last element is placed on the root node to maintain the heap. Repeat the above procedure.

void Sift (inta[],int s,int N) {int i=s,j=2*s;     while(j<=n) {        //if(J<n &&a[j]>a[j+1]) j=j+1; Small heap get big --and small//if(a[i]<=a[j]) Break;        if(J<n &&a[j]<a[j+1]) j=j+1; Big heap Get small --and big        if(a[i]>=a[j]) Break;        Else{Swap (a[I],a[j]);K= (; J=2*j;}}}void heapsort (inta[],int s,int N) {for (int i=n/2; i>=1;i--)  Sift (A,i,n); Build a heap bottom -upShowa, N);for (int i=n; i>1;i--) {Swapa[1],a[i]);Sift (a,1. io1);}}

The time complexity of heap sequencing is O (NLOGN), which is an unstable sorting algorithm

Two-way merge sort:

The first is the ordering of adjacent elements, recursively, comparing the sequence of adjacent subsets, and finally finishing the sorting.

Const intn=1e3;intB[n];voidMergeintA[],intSdex,intMdex,intEdex) {inti=sdex,j=mdex+1, K=sdex; while(I<=mdex&&j<=edex) {if(A[i]<a[j]) b[k++]=a[i++];ElseB[k++]=a[j++]; } while(i!=mdex+1) b[k++]=a[i++]; while(j!=edex+1) b[k++]=a[j++]; for(i=sdex;i<=edex;i++) a[i]=b[i];}voidMergeSort (intA[],intSdex,intEdex) {intMdex;if(Sdex<edex) {mdex= (Sdex+edex)/2;        MergeSort (A,sdex,mdex); MergeSort (a,mdex+1, Edex);    Merge (A,sdex,mdex,edex); }}

Basic 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.