快排:
#include <stdio.h>
int qsort(int* a,int low,int high)
{
int temp = a[low];
while(low<high)
{
while(low < high && temp < a[high])
--high;
a[low] = a[high];
while(low < high && temp > a[low])
++low;
a[high] = a[low];
}
a[low] = temp;
return low;
}
void sort(int* a,int low,int high)
{
int temp;
if(low<high)
{
temp = qsort(a,low,high);
qsort(a,low,temp-1);
qsort(a,temp+1,high);
}
}
int main()
{
int list[] = {4,5,6,3,7,1};
sort(list,0,5);
char i;
for(i=0;i<6;i++)
printf("%3d",list[i]);
printf("\n");
return 0;
}
冒泡:
void sort(int *a,int len)//参数1,表示数组,参数2表示数组长度
{
int i,j,t;
for(i=0;i<len-1;i++)
{
for(j=0;j<len-1-i;j++)
{
if(a[j]<a[j+1])
{
t = a[j];
a[j] = a [j+1];
a[j+1] = t;
}
}
}
}
都是自己平时写的,全部可以调试通过,快排要是看不懂加我Q,285296372~
2019-07-17 22:50:56