The data structure course may have a sorting algorithm to the exam:
Insert Sort Hill Sort Bubbling method Quick Row Select Sort Heap sort Merge sort
One insert Sort
#include <cstdio>#include<string>#include<cstring>#include<iostream>using namespacestd;voidPrintintA[],intNinti) {cout<<i <<":"; for(intj=0; j<8; J + +) {cout<<A[J] <<" "; } cout<<Endl;}voidInsertsort (intA[],intN) { for(intI=1; i<n; i++){ if(A[i] < a[i-1]){//if the first element is greater than the i-1 element, it is inserted directly. Less then, move the ordered table after inserting intj= I-1; intx = A[i];//Copy as Sentinel, which stores the elements to be sortedA[i] = a[i-1];//move one element at a succession while(x < a[j]) {//find the insertion position in an ordered tablea[j+1] =A[j]; J--;//element Move back} a[j+1] = x;//Insert to correct position} print (a,n,i); //Print the results of each trip sort } }intMain () {inta[8] = {3,1,5,7,2,4,9,6}; Insertsort (A,8); Print (A,8,8); return 0;}Insert Sort
Find a location for each element of the insertion process in turn
Two Hill sort
#include <cstdio>#include<string>#include<cstring>#include<iostream>using namespacestd;voidPrintintA[],intNinti) {cout<<i <<":"; for(intj=0; j<8; J + +) {cout<<A[J] <<" "; } cout<<Endl;}/** * Direct Insert sort general form * * @param int DK Shrink increment, if direct insert sort, dk=1 **/voidShellinsertsort (intA[],intNintDK) { for(intI= DK; i<n; ++i) {if(A[i] < A[I-DK])//if the first element is greater than the i-1 element, it is inserted directly. Less then, move the ordered table after inserting { intj = IDK; intx = A[i];//Copy as Sentinel, which stores the elements to be sortedA[i] = A[I-DK];//first move back one element while(x < a[j])//find the insertion position in an ordered table{a[j+DK] =A[j]; J-= DK;//element Move back} a[j+DK] = x;//Insert to correct position} print (A, n,i); }}/** * First by increment D (N/2,n for the number of orders to be sorted by Hill sort **/voidShellsort (intA[],intN) { intDK = n/2; while(DK >=1) {Shellinsertsort (A, n, DK); DK= dk/2; }}intMain () {inta[8] = {3,1,5,7,2,4,9,6}; //Shellinsertsort (a,8,1);//Direct Insert SortShellsort (A,8);//Hill Insert SortPrint (A,8,8);}Hill Sort
Hill sort shrinks comparison size until adjacent comparison
Special topics in sorting algorithms