Quick Sort C Realization

 The article "Quick Sort C Implementation" was originally written by me. It was published in my Baidu space in 2012. I didn't expect that by coincidence, this question was accidentally selected by Alibaba and changed into a fill-in-the-blank question. The written test questions of Alibaba's national school recruitment back then were a coincidence, which is a sigh! Now that the blog has moved, I will re-keep this article as it is and move it from Baidu Space to a new blog on CSDN.


#include <stdio.h>


/*output*/
void show(int [],int);


void quickSort(int array[],int left,int right)
{

   if(left>right)

          return;


 /* Take the leftmost value as pivot(base)*/
     int i=left,j=right,pivot=array[left];
  

    while(i<j)
    {
         while( (i<j) && (pivot <= array[j]) )
            j--;
         if(i<j)
            array[i++]=array[j];
  
  while( (i<j) && (array[i] <= pivot) )
             i++;
         if(i<j)
             array[j--]=array[i];
 }

     array[j]=pivot; /* can also be array[i]=piovt. Because at this time i=j*/

    quickSort(array,left,i-1);
    quickSort(array,i+1,right);
}


void main()
{
 /*Test Data*/
 //int array[]={4,3,9,0,8,5,7,1,6,2};
 int array[]={9,8,7,6,5,4,3,2,1,0,123,22,34,22,56,76,345,221};

 int LEN=sizeof(array)/sizeof(int);

 printf("Original array:\n");
 show(array,LEN);
 printf("\n-----\n");

 quickSort(array,0,LEN-1);

 printf("\nSort result:\n");
 show(array,LEN);
}

/*Print*/
void show(int a[],int len)
{
 int i;
 for(i=0;i<len;i++)
  printf("%d ",a[i]);
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325383306&siteId=291194637