栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > C++面试题库

快速排序(东软喜欢考类似的算法填空题,又如堆排序的算法等)

快速排序(东软喜欢考类似的算法填空题,又如堆排序的算法等)

#include “stdafx.h”
#define N 10
int part(int list[], int low, int high) {  // 一趟排序,返回分割点位置
 int tmp = list[low];
 while(low<high) {
  while(low<high && list[high]>=tmp) –high;
  list[low] = list[high];
  while(low<high && list[low]<=tmp)  ++low;
  list[high] = list[low];
 }
 list[low] = tmp;
 return low;
}
void QSort(int list[], int low, int high) { // 应用递归进行快速排序
 if(low<high) {
  int mid = part(list, low, high);
  QSort(list, low, mid-1);
  QSort(list, mid+1, high);
 }
}
void show(int list[], int n) {    // 输出列表中元素
 for(int i=0; i<n; i++)
  printf(“%d “, list[i]);
 printf(“n”);
}
int main(int argc, char* argv[]) {
 int list[N] = {23, 65, 26, 1, 6, 89, 3, 12, 33, 8};
 show(list, N);      // 输出排序前序列
 QSort(list, 0, N-1);     // 快速排序
 show(list, N);      // 输出排序后序列
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/265507.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号