博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
快速排序 迭代实现
阅读量:2352 次
发布时间:2019-05-10

本文共 2334 字,大约阅读时间需要 7 分钟。

文章来源:http://blog.csdn.net/pkuyjxu/article/details/6918295

快速排序(QUICKSORT)是一个非常流行并且高效的排序算法,具有o(nlogn)的平均运行时间,

这个算法优于合并排序(MERGESORT)的一点就是它是在原位排序,即对于被排序的元素不需要辅助

的存储空间。在描述这个排序算法之前,需要一下的划分算法,它是(QUICKSORT)的基础。

 

划分算法

 

  原理:如果元素A[j]既不大于A[low...j-1]中的元素,也不小于A[j+1...high]的元素,则称其处于一

   个适当的位置或正确的位置。

  观察结论:用x(x∈A)作为主元素划分数组A后,x将处于一个正确的位置。

 

我们感兴趣的是在没有辅助数组的情况下进行划分,有几种方法可以达到这个目的,这里选取两种来实现。

 

1.从左往右搜索

 

  1. int split(int data[],int low,int high)  
  2. {  
  3.     int i=low;  
  4.     int x=data[low];  
  5.     int tmp;  
  6.     for(int j=low+1;j<=high;j++)  
  7.     {  
  8.         if(data[j]>x)  
  9.         {   i++;  
  10.             if(i!=j)   
  11.             {  
  12.                 tmp=data[j];  
  13.                 data[j]=data[i];  
  14.                 data[i]=tmp;  
  15.             }  
  16.         }  
  17.     }  
  18.     tmp=data[low];  
  19.     data[low]=data[i];  
  20.     data[i]=tmp;  
  21.     return i;  
  22. }  
 

 

2.从两边往中间搜索

 

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. int quicksort(int data[],int s,int t)  
  2. {  
  3.   int i=s,j=t;  
  4.   int tmp;  
  5.   if(s<t)  
  6.   {  
  7.     tmp=data[s];  
  8.     while(i!=j)  
  9.     {  
  10.       while(j>i&&data[j]<=tmp) j--;  
  11.       data[i]=data[j];  
  12.       while(i<j&&data[i]>=tmp) i++;  
  13.       data[j]=data[i];             
  14.     }  
  15.     data[i]=tmp;  
  16.     return i;  
  17.   }  
  18. }  
 

 

排序算法

 

1.递归形式

 

  1. void quicksort(int data[],int low,int high)  
  2. {  
  3.     if(low<high)  
  4.     {  
  5.         int w=split(data,low,high);  
  6.         quicksort(data,low,w-1);  
  7.         quicksort(data,w+1,high);  
  8.     }  
  9. }  
 

 

2.迭代形式

递归转化为迭代算法的关键是传递每次划分的low和high,所以定义一个结构体st来存储每次划分的low和high,

top初始为-1,用来记录长度。划分一次st长度++,进入划分一次st长度--,直到top=-1。

 

  1. struct node  
  2. int low,high;  
  3. }st[10000];  
  4. void quicksort2(int data[],int s,int t)  
  5. {  
  6.     int top=-1,low,high;  
  7.     top++;  
  8.     st[top].low=s;st[top].high=t;  
  9.     while(top>-1)  
  10.     {  
  11.         low=st[top].low;high=st[top].high;  
  12.         top--;  
  13.         int w;  
  14.         if(low<high)  
  15.         {  
  16.             w=split(data,low,high);//或者split2  
  17.             st[++top].low=low;st[top].high=w-1;  
  18.             st[++top].low=w+1;st[top].high=high;  
  19.         }  
  20.     }  
  21. }  
 

 

 

最后给出一段测试代码,很好用!

  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #define MAXSIZE 10000 //所有数组中元素的个数最多为MAXSIZE个  
  4. int isASCENDING(ELEMET_TYPE heap[],int n)  
  5. {  
  6.      
  7.     int i;  
  8.     for(i=n-1;i>0;i--)  
  9.         if(heap[i-1]<heap[i])  
  10.             return 0;//只要有一个结点比其后的结点小,则该数组肯定不是降序序列   
  11.     return 1;  
  12. }  
  13. void test()  
  14. {  
  15.     ELEMET_TYPE test_elem[10000];  
  16.     for(int test_Turn=0;test_Turn<100;test_Turn++)  
  17.     {  
  18.         printf("第%d轮:",test_Turn+1);   
  19.         int test_num=rand()%5000+5000;  
  20.         for(int i=0;i<test_num;i++)  
  21.             test_elem[i]=rand();  
  22.         QUICKSORT(test_elem,test_num);  
  23.         if(!isASCENDING(test_elem,test_num))  
  24.         {  
  25.             printf("错误/n");  
  26.             system("pause");  
  27.             exit(0);   
  28.         }  
  29.         printf("正确/n");   
  30.     }     
  31.     printf("测试通过/n");   
  32.     system("pause");  
  33. }  
  34. void QUICKSORT(ELEMET_TYPE data[],int n)  
  35. {      
  36.     quicksort(data,0,n-1);     
  37. }  
  38. int main()//主函数   
  39. {  
  40.           
  41.     test();   
  42.     return 0;  
  43.       

你可能感兴趣的文章
责任型模式
查看>>
单例模式
查看>>
观察者模式
查看>>
中介者模式
查看>>
享元模式
查看>>
构造型模式
查看>>
生成器模式
查看>>
GitHub 删除代码库 详解
查看>>
Android Studio Rest Client工具详解
查看>>
IntelliJ IDEA 配置MySQL数据库 详解
查看>>
Android Studio 常用快捷键详解
查看>>
Cornerstone无法上传 .so 文件的问题
查看>>
Objective-C #pragma mark 详解
查看>>
Object-C 单例模式
查看>>
IOS 命名规范
查看>>
禁用sublime自动更新提示
查看>>
iOS Pch文件 详解
查看>>
Objective-C 获取控件 详解
查看>>
Objective-C 事件处理 详解
查看>>
IOS UIView 详解
查看>>