/*快速排序*/
/*算法思想*/
/*1. 判断参数*/
/*2. 以第一个元素为基准,找后面的元素,把小的放左边,把大的放右边*/
/*3. 以大小两边的第一个元素为基准,作2这样的操作*/
#include <stdio.h>
int Location(int array[], int low, int high)
{
int temp = array[low];
while(low < high)
{
while((low < high) && (array[high] > temp))
{
high--;
}
array[low] = array[high];
while((low < high) && (array[low] < temp))
{
low++;
}
array[high] = array[low];
}
array[low] = temp;
return low;
}
void Quick_Sort(int array[], int low, int high)
{
int position = 0;
if(low < high)
{
position = Location(array, low, high);
Quick_Sort(array, low, position-1);
Quick_Sort(array, position+1, high);
}
}
void TraverseArray(int array[], int len)
{
int i = 0;
for(i = 0; i < len; i++)
{
printf("%4d", array[i]);
}
printf("\n\n");
}
int main(int argc, char *argv[])
{
int array[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
TraverseArray(array, sizeof(array)/sizeof(array[0]));
Quick_Sort(array, 0, sizeof(array)/sizeof(array[0])-1);
TraverseArray(array, sizeof(array)/sizeof(array[0]));
return 0;
}
/*算法思想*/
/*1. 判断参数*/
/*2. 以第一个元素为基准,找后面的元素,把小的放左边,把大的放右边*/
/*3. 以大小两边的第一个元素为基准,作2这样的操作*/
#include <stdio.h>
int Location(int array[], int low, int high)
{
int temp = array[low];
while(low < high)
{
while((low < high) && (array[high] > temp))
{
high--;
}
array[low] = array[high];
while((low < high) && (array[low] < temp))
{
low++;
}
array[high] = array[low];
}
array[low] = temp;
return low;
}
void Quick_Sort(int array[], int low, int high)
{
int position = 0;
if(low < high)
{
position = Location(array, low, high);
Quick_Sort(array, low, position-1);
Quick_Sort(array, position+1, high);
}
}
void TraverseArray(int array[], int len)
{
int i = 0;
for(i = 0; i < len; i++)
{
printf("%4d", array[i]);
}
printf("\n\n");
}
int main(int argc, char *argv[])
{
int array[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
TraverseArray(array, sizeof(array)/sizeof(array[0]));
Quick_Sort(array, 0, sizeof(array)/sizeof(array[0])-1);
TraverseArray(array, sizeof(array)/sizeof(array[0]));
return 0;
}