菜鸟学C语言三:快速排序与调用C语言库函数qsort()

快速排序算法

快速排序是对冒泡排序的一种改进,根据就是冒泡排序的特点待排序列越有序,排序效率越高。算法思想基于分治法的:在待排序表中L[1......n]任取一个元素pivot作为基准,通过一趟排序将待排序表划分为独立的两部分L[1.....k-1]和L[k+1....n],使得前部分所有元素都小于pivot,后半部分所有的元素大于或等于pivot,则pivot放在了最后的位置上,这个过程就是一趟快速排序。而后在两个子表上分别递归的重复上过程,直到每部分内只有一个元素或为空为止。

void QuickSort(ElemType A[],int low,int high){
       if(low<high){
                             int pivotpos=Partition(A,low,high);//划分
                             QuickSort(A,low,pivotpos-1);//依次对两个子表进行递归排序
                             QuickSort(A,pivotpos+1,high);
                              
       }
}<pre name="code" class="html">int Partition(ElemType A[],int low,int high){
     ElemType pivot=A[low];//从当前表中第一个元素设为枢轴值,对表进行划分,这是最简单的选取枢轴方式。
     while(low<high){      //跳出循环条件
          while(low<highh&&A[high]>=pivot) --high;
          A[low]=A[high];  //将比枢轴元素小的移动到左端
          while(low<high&&A[low]<=pivot)  ++low;
          A[high]=A[low];  //将比枢轴元素大的移动到右端
       }
       A[low]=pivot;       //枢轴元素放到最终位置
       return low;         //返回存放枢轴的最终位置
}

 
</pre><br />二  C语言库函数qsort()调用,实现快速排序<p></p><p>qsort()函数原型的说明,此函数包含在<stdlib.h></p><pre name="code" class="html"><pre name="code" class="html">void qsort(void *base, size_t nmemb, size_t size, int(*compare) (const void *, const void *));
/*     qsort(base, nbr_elements, width_bytes, compare_function);  */
/*        void *base;                                             */
/*        size_t nbr_elements, width_bytes;                       */
/*        int (*compare_function)(const void *, const void *);    */
/*                                                                */
/* Sorts an array starting at base, of length nbr_elements, each  */
/* element of size width_bytes, ordered via compare_function,     */
/* which is called as  (*compare_function)(ptr_to_element1,       */
/* ptr_to_element2) and returns < 0 if element1 < element2,       */
/* 0 if element1 = element2, > 0 if element1 > element2. 
</pre><pre name="code" class="html">base指向数组中待排部分的首元素(即可以是对整个数组,也可以对数组中部分元素进行快速排序)
nmemb要排序元素的数量
size每个数组元素的大小,按字节
compare指向比较函数的指针

</pre><pre name="code" class="html">调用qsort()进行快排时,自己得写个compare函数告诉qsort两个数组元素哪个更小。<div style="margin: 0px; padding: 0px;">当给定两个指向数组元素的指针p和q时,比较函数必须返回一个整数,如果*p小于*q,那么返回的数为负数;如果*p等于*q,那么返回0.如果*p大于*q,返回正数.按照这种结果返回,qsort对数组默认是升序排序.如果想降序,只需要将上述结果返回值*-1。</div><div>
</div>

简单测试:

	   #include <stdlib.h>


int cmp(const void*, const void*);

int main() {

    int num[12] = { 1, 4, -9, 4, 8, 9, 3, 12, 10, 8,14,15 };
    qsort(num,12,sizeof(int),cmp);
    int i = 0;
    for(;i<12;i++){
        printf("%d\t",num[i]);
    }
    return 0;
}

int cmp(const void * a, const void * b) {
    return *(int *) a - *(int *) b;
}

三   C语言标准库函数sort()使用

顺带瞧下sort函数:下面片段来自http://www.cplusplus.me/265.html

sort(array,array+n)函数默认是从小到大的,如果降序也是需要写比较函数sort(array,array+n,cmp)

普通排序,升序排列

int main()
{
 int a[10]={7,3,4,6,5,1,2,9,8,0};
 sort(a,a+10);
 for(int i=0;i<10;i++)
 pritnf("%d  ",a[i]);
 return 0;
}
OUTPUT:0 1 2 3 4 5 6 7 8 9
普通排序,降序

#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(int a,int b)
{
 return a>b;
}
int main()
{
 int a[10]={7,3,4,6,5,1,2,9,8,0};
 sort(a,a+10,cmp);
 for(int i=0;i<10;i++)
 cout<<a[i]<<" ";
 return 0;
}
OUTPUT:9 8 7 6 5 4 3 2 1 0



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值