#include <stdio.h>
#include <stdlib.h>
//输入若干数字,从小到大排序
void quick_sort(int array[], int left, int right); //从小到大排序
int main(void)
{
int i = 0;
int num = 0;
int count = 1;
int *ptr;
ptr = (int *)malloc(sizeof(int));
printf("Please input numbers(double '-1' to end):\n");
do
{
scanf("%d", &num);
ptr = (int *)realloc(ptr, count * sizeof(int));
if(ptr == NULL)
{
perror("realloc");
exit(-1);
}
ptr[count-1] = num;
count++;
}while(ptr[count-2] != -1 || num != -1);
quick_sort(ptr, 0, count - 3);
for(i = 0; i < count - 2; i++)
{
printf("%d ", ptr[i]);
}
putchar('\n');
free(ptr);
return 0;
}
void quick_sort(int array[], int left, int right) //从小到大排序
{
int temp, pivot;
int i = left, j = right;
pivot = array[(left + right) / 2];
while(i <= j)
{
while(array[i] < pivot)
{
i++;
}
while(array[j] > pivot)
{
j--;
}
if(i <= j)
{
temp = array[j];
array[j] = array[i];
array[i] = temp;
i++;
j--;
}
}
if(left < j)
{
quick_sort(array, left, j);
}
if(i < right)
{
quick_sort(array, i, right);
}
}
|