// 快速排序.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"#include<stdio.h>#include<windows.h>void quick_sort(int[],int,int,int);
void main()
{
int data[20];
int size = 0, i;
//要求输入数据直到数据为0
printf("\nPlease enter number to sort(enter 0 when end):\n");
printf("Number:");
do
{
scanf("%d",&data[size]);
} while (data[size++] != 0);
for (i = 0; i < 60; i++) printf("-");
printf("\n");
quick_sort(data,0,--size-1,size-1);
for (i = 0; i < 60; i++) printf("-");
printf("\nSorting:");
for (i = 0; i < size; i++)
printf("%d ",data[i]);
printf("\n");
system("pause");
}
void quick_sort(int data[], int left, int right, int size)
{//left与right分别表示要排序数据两端
int lbase, rbase, temp, i;
if (left < right)
{
lbase = left + 1;
while (data[lbase] < data[left]) lbase++;
rbase = right;
while (data[rbase] < data[left]) rbase--;
while (lbase < rbase)//若lbase小于rbase,则两数据对调
{
temp = data[lbase];data[lbase] = data[rbase];data[rbase] = temp;
lbase++;
while (data[lbase] < data[left]) lbase++;
rbase--;
while (data[rbase]>data[left]) rbase--;
}
temp = data[left];//此时lbase大于rbase,则rbase的数据与第一组对调data[left] = data[rbase];data[rbase] = temp;
printf("Access:");
for (i = 0; i < size; i++)
printf("%d ",data[i]);
printf("\n");
quick_sort(data,left,rbase-1,size);
quick_sort(data,rbase+1,right,size);
}
}