正文

快速排序算法(C源码)2006-05-13 23:33:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/smile/14011.html

分享到:

快速排序算法(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;}

阅读(4322) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册