Special topics in sorting algorithms

Source: Internet
Author: User

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&LT;&LT;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&LT;&LT;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

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.