快速排序算法(C源码)
#include <stdio.h>
#include <stdlib.h>
#define MAXLISTSIZE 100
typedef int ElementType;
//声明函数
int partition(ElementType a[], int low, int high);
int quicksort(ElementType a[], int low, int high);
void output(ElementType a[]);
void input(ElementType a[]);
int Listsize;
void main()
{
ElementType a[MAXLISTSIZE];
printf("Enter Number\n");
input(a);
printf("The primary number.\n");
output(a);
printf("Quick sort...\n");
quicksort(a,0,Listsize-1);
printf("\nThe Post Number.\n");
output(a);
}
int partition(ElementType arr[], int low, int high);
{
ElementType p = arr[low];
ElementType temp;
while(low < high)
{
while(low < high && arr[high] >= p) high--;
EXCHAGE(arr[low],arr[high]);
while(low < high && arr[low] <= p) low++;
EXCHAGE(arr[low],arr[high]);
}
return low;
}
void quicksort(ElementType arr[],int low, int high)
{
int loc;
if(low < high)
{
loc = partition(arr, low, high);
quicksort(arr, low, loc-1);
quicksort(arr, loc+1, high);
}
void output(ElementType arr[])
{
int i;
for( i = 0; i < Listsize; i++)
{
printf("%d", arr[i]);
}
}
void input(ElementType arr[])
{
int i = 0;
int temp = 0;
for(i = 0; ; i++)
{
scanf("%d",&temp);
if(temp == -1)
{
break;
}
arr[i] = temp;
}
Listsize = i;
}
评论