c语言:快速排序

时间:2023-03-08 17:46:05

练手代码(分治实现):

input:

int input[] = {12,6,3,9,10,6,2};

output:

=======================
len = 7
input[0]=2
input[1]=3
input[2]=6
input[3]=6
input[4]=9
input[5]=10
input[6]=12

这里强烈推荐一款web端代码编译网站,不用换个机器想写点code还必须安装编译器。

http://codepad.org/ 

界面如下:

c语言:快速排序

int partition(int input[],int low,int high){
int pivot = input[low];
while(low < high){
  while(low < high && input[high] >= pivot) high--;
  input[low] = input[high];
   while(low < high && input[low] <= pivot) low++;
  input[high] = input[low];
}//low=high时跳出
input[low] = pivot;
return low;
} int quicksort(int input[],int inplen,int low,int high){//快速排序
int k=;
if(low < high){
k = partition(input,low,high);
quicksort(input,inplen,low,k-);
quicksort(input,inplen,k+,high);
}
return ;
} int main(){
printf("=======================\n");
int input[] = {,,,,,,};
int input1[] = {,,};
int len = sizeof(input)/sizeof(int);
printf("len = %d\n",len);
quicksort(input,len,,len-);
int i=;
for(;i<len;i++){
printf("input[%d]=%d\n",i,input[i]);
}
return ;
}