Summary of Eight Sorting Algorithms

I have always been vague about the sorting algorithm, and I have an interview recently, so I found some information, and I sorted out a lot on the Internet. I excerpted an article for future review. The original text is transferred from <a href='http://blog.csdn.net /yexinghai/article/details/4649923'>Summary of Eight Sorting Algorithms</a>
1. Direct Insertion Sorting

Principle : Divide the array into two areas: an unordered area and an ordered area, and then continuously sort the first one in the unordered area Elements are inserted into the ordered area in order of size, and finally all elements in the unordered area are moved to the ordered area to complete the sorting.

Key Point: Set up a sentinel for temporary storage and for judging array boundaries.

Implementation:

Void InsertSort(Node L[],int length)

{

Int i,j;//The ordered area and the unordered area pointer respectively

for(i=1;i<length;i++)//Gradually expand the ordered area

{

j=i+1;

if(L[j]<L[i])

{

L[0]=L[j];//Store the element to be sorted

While(L[0]<L[i])// Find the insertion position in the ordered area while moving the element

{

L[i+1]=L[i];//move

i--;//find

}

L[i+1]=L[0];/ /Insert the element

}

i=j-1;//Restore the ordered area pointer

}

}

2. Hill sort

Principle: Also known as incremental reduction sort. First, the sequence is divided into several groups with the same number of elements by increments, and the direct insertion sorting method is used for sorting, and then the increment is continuously reduced until it is 1, and finally the sorting is completed by direct insertion sorting.

Important: Incremental selection and sorting end up with sorting in increments of 1.

Implementation:

Void shellSort(Node L[],int d)

{

While(d>=1)//Until the increment shrinks to 1

{

Shell(L,d);

d=d/2;//Shrink the increment

}

}

Void Shell(Node L[],int d)

{

Int i,j;

For(i=d+1;i<length;i++)

{

if(L[i]<L[id])

{

L[0]= L[i];

j=id;

While(j>0&&L[j]>L[0])

{

L[j+d]=L[j];//move

j=jd;//find

}

L[j +d]=L[0];

}

}

}

Exchange sort

1. Bubble sort

Principle: Divide the sequence into disordered and ordered regions, and continuously complete the sorting by exchanging larger elements to the end of the disordered region.

Key points: Design exchange judgment conditions, and end the loop in a sorted sequence early.

Implementation:

Void BubbleSort(Node L[])

{

Int i ,j;

Bool ischanged;//Design out of the condition

For(j=n;j<0;j--)

{

ischanged =false;

For(i=0;i <j;i++)

{

If(L[i]>L[i+1])//If heavier elements are found, move backward

{

Int temp=L[i];

L[i]=L[i+1 ];

L[i+1]=temp;

Ischanged =true;

}

}

If(!ischanged)//If there is no movement, it means that the sequence is already in order, and jumps out of

Break directly;

}

}

2. Quick sort

Principle : keep looking for a sequence The midpoint of , and then recursively sort the sequences around the midpoint until all sequences are sorted, using the idea of ​​divide and conquer.

Key points: recursion, divide and conquer

Implementation :




selection sort

1. Direct selection sort

Principle: Divide the sequence into disordered and ordered regions, find the minimum value in the disordered region and exchange the first element of the disordered region, expand the ordered region by one, and the loop finally completes all sorting.

Point:

Implementation:

Void SelectSort(Node L[])

{

Int i,j,k;//Ordered area, unordered area, unordered area minimum element pointer

For(i=0;i<length;i++)

{

k=i;

For(j=i+1;j<length;j++)

{

If(L[j]<L[k])

k=j;

}

If(k!=i)//If the smallest element is found , then move to the ordered area

{

Int temp=L[k];

L[k]=L[i];

L[i]=L[temp];

}



}

}

2. Heap sort

Principle : use big root heap or small The idea of ​​root heap, first build the heap, then exchange the heap head and the heap tail, and the ordered area after the heap tail.

Key points: build heap, swap, adjust heap

Implementation :

Void HeapSort(Node L[])

{

BuildingHeap(L);//Build heap (big root heap)

For(int i=n;i>0;i--)// exchange

{

Int temp=L[i];

L[i]=L[0];

L[0]=temp;

Heapify(L,0,i);//Adjust heap

}

}




Void BuildingHeap(Node L[])

{ For (i=length/2 -1;i>0;i--)

Heapify(L,i,length);

} Merge

Sort

Principle : Divide the original sequence into two ordered sequences, and then use the merge algorithm to merge, After merging, it is an ordered sequence.

Key Point: Merge, Divide and Conquer

Implementation :

Void MergeSort(Node L[],int m,int n)

{

Int k;

If(m<n)

{

K=(m+n)/2;

MergeSort(L,m,k );

MergeSort(L,k+1,n);

Merge(L,m,k,n);

}

}




Radix sort

Principle : Divide the number into n keywords according to the number of digits, and sort one keyword at a time , and then sort the next keyword for the sorted sequence, and loop until all the keywords are used, and the sorting is completed.

Key points: the selection of keywords, the collection of element assignments.

accomplish:

Void RadixSort(Node L[],length,maxradix)

{

Int m,n,k,lsp;

k=1;m=1;

Int temp[10][length-1];

Empty(temp); //Empty the temp Space

While(k<maxradix) //traverse all keywords

{

For(int i=0;i<length;i++) //allocation process

{

If(L[i]<m)

Temp[0][n]=L [i];

Else

Lsp=(L[i]/m)%10; //determine the keyword

Temp[lsp][n]=L[i];

n++;

}

CollectElement(L,Temp); //collect

n =0;

m=m*10;

k++;

}

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326691953&siteId=291194637