标签:span c++实现 clu mes iostream turn int col vector
一、插入排序 (Insertion Sort)
void insertion_sort(vector<int>& number){ int length = number.size(); if(length <= 1) return; for(int index = 1; index < length; ++index){ int temp = number[index]; int compare = index; for(; compare > 0 && temp < number[compare-1]; compare--) number[compare] = number[compare-1]; number[compare] = temp; } }
二、 快速排序(Quick Sort)
void quick_sort(vector<int>& number, int begin, int end){ if(begin >= end) return; int mid = (begin+end)/2, pivot = number[mid]; swap(number[mid], number[end]); int temp = begin - 1; for(int i = begin; i < end; i++) if(number[i] <= pivot) swap(number[++temp], number[i]); swap(number[++temp], number[end]); quick_sort(number, begin, temp-1); quick_sort(number, temp+1, end); }
示例代码:
#include <iostream> #include <vector> using namespace std; void insertion_sort(vector<int>&); void quick_sort(vector<int>&, int, int); int main() { vector<int> numbers = {34,8,64,51,21,1,23,45,90,47,35,70,134,152,251,540}; // 1 8 21 23 34 35 45 47 51 64 70 90 134 152 251 540 // insertion_sort(numbers); // quick_sort(numbers, 0, numbers.size()-1);
for(int num : numbers) cout << num << ‘ ‘ ; return 0; }
标签:span c++实现 clu mes iostream turn int col vector
原文地址:https://www.cnblogs.com/heifengli/p/11329465.html