- 论坛徽章:
- 0
|
#include<stdio.h>
#include<stdlib.h>
int partition(int l[], int low, int high)
{
int pivotkey = l[low];
while(low < high)
{
while((low < high) && (l[high] >= pivotkey))
high--;
l[low] = l[high];
while((low < high) && (l[low] <= pivotkey))
low++;
l[high] = l[low];
}
l[low] = pivotkey;
return low;
}
void qsort(int l[], int low, int high)
{
int pivotloc = 0;
if(low < high)
{
pivotloc = partition(l, low, high);
qsort(l, low, pivotloc - 1);
qsort(l, pivotloc + 1, high);
}
}
void print(int l[], int len)
{
int i = 0;
while(i < len - 1)
{
printf("%d,", l);
i++;
}
printf("%d\n", l[len - 1]);
}
int selectmax(int *l, int len)
{
int times = 0,i,j;
for(i=0; i< len; i++)
{
for(j =0; j < len ; j++)
{
if((i != j) && (l > l[j])) times++;
if(times == len -1) return i;
}
times = 0;
}
return -1;
}
void selectsort(int l[], int len)
{
int j = 0, i, tmp;
for(i = 0; i < len ;i++)
{
j = selectmax(l + i, len - i);
if(j < 0) exit(0);
tmp = l;
l = l[j + i];
l[j + i] = tmp;
}
}
int main(int argc, char **argv)
{
int l[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
int len = sizeof(l)/4;
selectsort(l, len);
print(l, len);
qsort(l, 0, len -1);
print(l, len);
return 0;
} |
|