/*快速排序*/
/*1. 判断参数*/
/*2. 以第一个元素为基准,找后面的元素,把小的放左边,把大的放右边,进行互换*/
/*3. 以大小两边的第一个元素为基准,作2这样的操作*/
#include <stdio.h>
int Location(int array[], int low, int high)
{
int pivot = array[low];
int left = low;
int right = high;
int tempValue;
while(left < right)
{
while((left < right) && (array[right] >= pivot))
{
right--;
}
while((left < right) && (array[left] <= pivot))
{
left++;
}
if(left < right)
{
tempValue = array[right];
array[right] = array[left];
array[left] = tempValue;
}
}
array[low] = array[right];
array[right] = pivot;
return right;
}
void Quick_Sort(int array[], int low, int high)
{
int position = -1;
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};
int array[] = {0, 9, 1, 2, 8, 5, 3, 6, 7, 4};
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 pivot = array[low];
int left = low;
int right = high;
int tempValue;
while(left < right)
{
while((left < right) && (array[right] >= pivot))
{
right--;
}
while((left < right) && (array[left] <= pivot))
{
left++;
}
if(left < right)
{
tempValue = array[right];
array[right] = array[left];
array[left] = tempValue;
}
}
array[low] = array[right];
array[right] = pivot;
return right;
}
void Quick_Sort(int array[], int low, int high)
{
int position = -1;
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};
int array[] = {0, 9, 1, 2, 8, 5, 3, 6, 7, 4};
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;
}