快速排序(基于顺序容器vector;基于数组array)
来源:互联网 发布:利口酒 知乎 编辑:程序博客网 时间:2024/06/05 07:04
代码1:基于顺序容器vector
1 /* 2 * FILE: quick_sort_vector.cpp 3 * --------------------------- 4 * DATE: 20170818 5 * 快速排序,基于顺序容器vector 6 */ 7 8 #include <iostream> 9 #include <vector> 10 11 /* 12 * Implementation notes: partition 13 * ------------------------------- 14 * This function rearrange the elements of the vector 15 */ 16 17 int partition(std::vector<int> &v, int begin, int end) 18 { 19 int pivot = v[begin]; 20 int left = begin + 1; 21 int right = end; 22 while(true) 23 { 24 while(left < right && v[right] >= pivot) 25 right--; 26 while(left < right && v[left] < pivot) 27 left++; 28 if(left == right) 29 break; 30 std::swap(v[left], v[right]); 31 } 32 if(v[left] >= pivot) 33 return begin; 34 v[begin] = v[left]; 35 v[left] = pivot; 36 return left; 37 } 38 39 /* 40 * Implementation notes: quickSort 41 * ------------------------------- 42 * This function sorts the elements in the vector between 43 * index position begin and end, inclusive 44 */ 45 void quickSort(std::vector<int> &v, int begin, int end) 46 { 47 if(begin >= end) 48 return; 49 int boundary = partition(v, begin, end); 50 quickSort(v, begin, boundary - 1); 51 quickSort(v, boundary + 1, end); 52 } 53 54 /* 55 * Implemention notes: sort 56 * ------------------------ 57 * This function sorts the elements of the vector into 58 * increasing numerical order using the QuickSort algorithm 59 */ 60 void sort(std::vector<int> &v) 61 { 62 quickSort(v, 0, v.size() - 1); 63 } 64 65 void display(std::vector<int> &v) 66 { 67 for(std::vector<int>::iterator iter= v.begin(); iter != v.end(); iter++ ) 68 std::cout<< *iter << " "; 69 std::cout<< std::endl; 70 } 71 72 int main(int argc, char *argv[]) 73 { 74 int a[] ={3,4,2,1,7,5,8,9,0,6}; 75 std::vector<int> vec(a, a+10); 76 sort(vec); 77 display(vec); 78 return 0; 79 }
代码2:基于数组array
1 /* 2 * FILE: quick_sort.cpp 3 * -------------------- 4 * DATE: 20170818 5 * 6 * 7 */ 8 9 #include <iostream> 10 11 void quickSort(int a[], int begin, int end); 12 13 int main(int argc, char *argv[]) 14 { 15 int a[] = {3,4,2,1,7,5,8,9,0,6}; 16 int length = sizeof(a) / sizeof(int); 17 18 quickSort(a, 0, length-1); // 快速排序 19 20 for(int i = 0; i < length; i++) 21 std::cout<< a[i] << " "; 22 std::cout<< std::endl; 23 return 0; 24 } 25 26 /* 快速排序 */ 27 void quickSort(int a[], int begin, int end) 28 { 29 if(begin >= end) 30 return; 31 int left = begin, right = end; 32 int pivot_value = a[left]; 33 while(left < right) 34 { 35 if(a[left+1] < pivot_value) 36 { 37 a[left] = a[left+1]; 38 left++; 39 } 40 else if(a[left+1] >= pivot_value) 41 { 42 std::swap(a[left+1], a[right]); // std::swap() 43 right--; 44 } 45 } 46 a[left] = pivot_value; 47 quickSort(a, begin, left-1); // 递归 48 quickSort(a, left+1, end); 49 }
阅读全文
0 0
- 快速排序(基于顺序容器vector;基于数组array)
- 归并排序(基于顺序容器vector_递归;基于数组array_递归_非递归)
- 顺序容器(vector、deque、list、forward_list、array 、string)
- 图解Java快速排序(基于数组)
- leetcode- 215. Kth Largest Element in an Array(基于快速排序思想)
- STL顺序容器数组之vector
- 基于Vector实现C++中的适配器stack(顺序栈)
- 基于c++浅谈容器(vector和list)
- 快速排序算法-容器存储vector
- (基于Java)算法之快速排序
- 快速选择排序(quickselect)--基于quicksort
- 基于Vector实现适配器stack(顺序栈)
- C编写的具有模板功能的Vector - 基于动态顺序数组
- 快速排序+基于c#实现
- 基于单链表的快速排序
- 基于Java的快速排序
- 模板容器类的实现一(基于动态数组)
- ReviewForJob——快速排序(基于插入排序)+快速选择(快速排序变体)
- 输出学生信息
- java学习——java基础(三)之JDK介绍
- 编译原理实验手册
- Linux开发环境搭建03---Ubuntu14.04在VMware12上设置
- 头文件的包含规则(尖括号和引号的区别)
- 快速排序(基于顺序容器vector;基于数组array)
- USACO-2017-FEB-金组 Why Did the Cow Cross the Road
- MapReduce 分析 Youtube 数据
- minetest源码解析八:ClientEnvironment
- 打印长方框
- DOS短文件名规则
- Struts2创建项目4--ognl
- final修饰符 10条易错问题总结
- C语言MD5源码及实例