Interview Programming questions (sort)

Source: Internet
Author: User

Sort by: quick-arrange, heap-sort

/*Direct Insert Sort * Set Sentinel [0]*/voidInsertsort (intData[],intlength) {     for(inti =2; I <= length; i++)    {        if(Data[i] < data[i-1]) {data[0] =Data[i]; Data[i]= data[i-1]; intJ;  for(J= I-2; data[0] < DATA[J]; j--) Data[j+1] =Data[j]; Data[j+1] = data[0]; }    }}/*Direct Insert Sort * Do not set Sentinel*/voidInsertSort2 (intData[],intlength) {     for(intI=1; i<length; i++)    {        if(data[i]<data[i-1])        {            intTMP =Data[i]; intJ;  for(j=i-1; j>=0&& Data[j] > tmp;j--) Data[j+1] =Data[j]; Data[j+1] =tmp; }    }}/*Hill Sort with insert implementation*/voidShellsort (intData[],intN) {     for(intD = n/2; D >=1; D/=2)//Step Size    {         for(inti = D; i<n; i++)        {            if(Data[i] < data[i-d]) {intTMP =Data[i]; intJ;  for(j = i-d; j>=0&& data[j] > tmp; j-=d) Data[j+D] =Data[j]; Data[j+D] =tmp; }        }    }}/*Hill sort with swap implementation*/voidShellSort2 (intData[],intN) {     for(intD = n/2; D >=1; D/=2)//Step Size    {         for(inti = D; i<n; i++)        {             for(intj = i-d; j>=0&& Data[j] > Data[j+d]; j-=d) Swap (Data[j],data[j+d]); }    }}/*Bubble Sort*/voidBubble_sort (intData[],intN) {     for(inti =0; i<n;i++)    {         for(intj =0; J < n-i-1; ++j) {if(data[j+1] <Data[j]) swap (data[j],data[j+1]); }    }}/*Qsort*/intPartition (intData[],intLenintLowintHigh ) {    intPivotKey =Data[low];  while(Low <High ) {         while(LowHigh ;        Swap (Data[low],data[high]);  while(LowLow ;    Swap (Data[low],data[high]); }    returnLow ;}intPartition2 (intData[],intLenintLowintHigh ) {    intTMP =Data[low]; intPivotKey =Data[low];  while(low<High ) {         while(Low < High && Data[high] >= pivotkey)--High ; Data[low]=Data[high];  while(Low < High && Data[low] <= pivotkey) + +Low ; Data[high]=Data[low]; } Data[low]=tmp; returnLow ;}voidQsort (intData[],intLenintLowintHigh ) {    if(Low <High ) {        intPivotloc =Partition2 (Data,len,low,high); Qsort (Data,len,low,pivotloc-1); Qsort (Data,len,pivotloc+1, high); }}/*Heap Sort*/voidHeapadjust (intData[],intStartintend) {    intTMP =Data[start];  for(intj=2*start+1; j<=end;j*=2)    {        if(J<end && data[j]<data[j+1]) ++J; if(tmp > DATA[J]) Break; Data[start]=Data[j]; Start=J; } Data[start]=tmp;}voidHeapsort (intData[],intLen) {    //Build a heap     for(inti=len/2-1; i>=0; i--)//0 start the last non-terminal node is N/2 down-1//1 Start the last non-endpoint for N/2 down roundingHeapadjust (data,i,len-1);  for(inti=len-1;i>0; i--) {Swap (data[0],data[i]); Heapadjust (data,0, I-1); }}

Merge sort

Comparison of the spatial complexity of stable time complexity with each sort algorithm

Interview Programming questions (sort)

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.