常见排序之快速排序

Posted muzixiaofeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常见排序之快速排序相关的知识,希望对你有一定的参考价值。

快速排序:

#include<stdio.h>

int Array_Sort[] = {3,1,2,0,4,6,5,9,7,8};

int FindPos(int *Array,int low,int high)

{

      int val = Array[low];

      while(low<high)

      {

           while(low<high && val <= Array[high]) 此处必须先从high开始

           {

                 --high;

           }

           Array[low] = Array[high];

           while(low<high && val >= Array[low])

           {

                 ++low;

           }

           Array[high] = Array[low];

      }

      Array[low] = val;

      return low;

}

void Quick_Sort(int *Array,int low,int high)

{

      int pos;

      if(low<high)

      {

           pos = FindPos(Array,low,high);

           Quick_Sort(Array,low,pos-1);

           Quick_Sort(Array,pos+1,high);

      }

}

int main(void)

{

      int i = 0;

      Quick_Sort(Array_Sort,0,9);

      for(i = 0; i < 10 ; i++)

      {

           printf("%d ",Array_Sort[i]);

      }

return 0;

}

 

 

注:以上所有程序都经过作者多次验证,如有错误,请多指正。

以上是关于常见排序之快速排序的主要内容,如果未能解决你的问题,请参考以下文章