前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >快速排序

快速排序

作者头像
ternturing
发布2018-09-12 17:34:15
4030
发布2018-09-12 17:34:15
举报
文章被收录于专栏:炉边夜话炉边夜话

//快速排序,主要思想是通过一趟排序将待排序的记录分割成相邻的两个区域, //其中一个区域中的关键字均比另一区域中记录的关键字要小,在分别对这两个 //区域进行排序,以达到整个序列有序。一般情况是O(logn),最坏情况是O(n)

#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 10 int QSorting(int *result ,int first,int end) {      int key = result[first];      while(first < end)      {           while(first < end&&result[end] >= key)           end--;           if(first != end)                 result[first++] = result[end];           while(first < end && result[first] <= key)                first++;           if(first != end)                result[end--] = result[first];       }       result[first] = key;       return first; }

void Qsort(int *result,int first,int end) {      if(first < end )      {              int middle = QSorting(result,first,end);              Qsort(result,first,middle-1);              Qsort(result,middle+1,end);      } }

int main() {      int value[N] = {18,94,61,87,34,31,78,56,14,17};      int i = 0;      int sum = 0;      Qsort(value,0,N-1);      for(i = 0 ;i < N;i++)      {           sum = sum + value[i];           printf("%d/n",value[i]);       }       printf("the result:%d/n",value[N/2]-sum/N);       return 0; }

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2006年06月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档