电脑配置不行,就只安装了个visual c++ 2010 express,按经典教材写了个快速排序,有点生疏,还不错。
int Partition(int data[],int low,int high)
{
int pivotkey=data[low];
data[0]=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]=data[0];
return low;
}
void qsort(int data[],int low,int high)
{
int pivotloc=0;
if(low<high)
{
pivotloc=Partition(data,low,high);
qsort(data,low,pivotloc-1);
qsort(data,pivotloc+1,high);
}
}
void quicksort(int data[],int N)
{
qsort(data,1,N);
}
//test
int main(void)
{
int i=1,arr[]={0,49,38,65,97,76,13,27,49};
quicksort(arr,8);
for(;i<9;i++)
printf("%4d",arr[i]);
return 0;
}
时间: 2024-10-11 00:48:43