小
小唐
Unregistered / Unconfirmed
GUEST, unregistred user!
以下是书上的例程,有些地方不懂,我在程序中加了我的理解和疑问,请给予指点
#include <stdio.h>
#include <stdlib.h>
void quick_sort(int array[], int first, int last)
{
int temp, low, high, list_separator;
low = first;
high = last;
list_separator = array[(first + last) / 2];
//得到数组中间的位置的值
do
{
while (array[low] < list_separator) //当list_separator左边的数小于list_separator
low++;
//循环找到list_separator左边的不小于list_separator的数的位置
while (array[high] > list_separator) //当list_separator右边的数大于list_separator
high--;
//循环找到list_separator右边的不大于list_separator的数的位置
if (low <= high) //为什么要if,觉得这个条件肯定会成立
{
temp = array[low];
//这句和下面两句将数组元素值交换
array[low++] = array[high];
array[high--] = temp;
}
}
while (low <= high);
//觉得这个条件将永远成立,会造成无限循环
if (first < high) //觉得这个条件绝对成立,没必要if
quick_sort(array, first, high);
if (low < last) //觉得这个条件绝对成立,没必要if
quick_sort(array, low, last);
}
void main(void)
{
int values[100], i;
for (i = 0;
i < 100;
i++)
values = rand() % 100;
quick_sort(values, 0, 99);
for (i = 0;
i < 100;
i++)
printf("%d ", values);
}
#include <stdio.h>
#include <stdlib.h>
void quick_sort(int array[], int first, int last)
{
int temp, low, high, list_separator;
low = first;
high = last;
list_separator = array[(first + last) / 2];
//得到数组中间的位置的值
do
{
while (array[low] < list_separator) //当list_separator左边的数小于list_separator
low++;
//循环找到list_separator左边的不小于list_separator的数的位置
while (array[high] > list_separator) //当list_separator右边的数大于list_separator
high--;
//循环找到list_separator右边的不大于list_separator的数的位置
if (low <= high) //为什么要if,觉得这个条件肯定会成立
{
temp = array[low];
//这句和下面两句将数组元素值交换
array[low++] = array[high];
array[high--] = temp;
}
}
while (low <= high);
//觉得这个条件将永远成立,会造成无限循环
if (first < high) //觉得这个条件绝对成立,没必要if
quick_sort(array, first, high);
if (low < last) //觉得这个条件绝对成立,没必要if
quick_sort(array, low, last);
}
void main(void)
{
int values[100], i;
for (i = 0;
i < 100;
i++)
values = rand() % 100;
quick_sort(values, 0, 99);
for (i = 0;
i < 100;
i++)
printf("%d ", values);
}