现在的位置: 首页 > 综合 > 正文

c语言实现快速排序

2013年10月21日 ⁄ 综合 ⁄ 共 484字 ⁄ 字号 评论关闭
 
对整型数组的快速排序
使用的是递归
 
 
#include<stdio.h>
int Partition(int a[],int low,int high)
{
  int pivotkey = a[low];
  while(low<high)
  {
    if(low<high && a[high]>=pivotkey) --high;
    a[low]=a[high];
    if(low<high && a[low]<=pivotkey) ++low;
    a[high]=a[low];
   }
  a[low]=pivotkey;
  return low;
}
 
void Quick_Sort(int a[],int low,int high)
{
  if(low<high) 
  {
    int position = Partition(a,low,high);
    Quick_Sort(a,low,poisition-1);
    Quick_Sort(a,poisition+1,high);
  }
}
 
void main()
{
  int a[4]={45,56,23,5};
  Quick_Sort(a,0,3);
}

抱歉!评论已关闭.