#include<vector>
using namespace std;#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file#include"catch.hpp"
vector<int>&insertSort(vector<int>&v){int len = v.size();if(len ==0|| len ==1)return v;for(int i =1; i<len;++i){int insertNum = v[i];//寻找插入的位�?int j = i;while(j>0&& insertNum<v[j -1]){
v[j]= v[j -1];
j--;}
v[j]= insertNum;}return v;}SCENARIO("直接插入排序","[vector]"){GIVEN("some vectors with some items"){
std::vector<int> v1{};WHEN("size of vector is 0"){
vector<int> sortedRes1{};THEN("test insertSort"){REQUIRE(v1.size()==0);REQUIRE(insertSort(v1)== sortedRes1);}}
std::vector<int> v2{1};WHEN("size of vector is 1"){
vector<int> sortedRes2{1};THEN("test insertSort"){REQUIRE(v2.size()==1);REQUIRE(insertSort(v2)== sortedRes2);}}
std::vector<int> v3{5,4,3,2,1,0};WHEN("size of vector is 6"){
vector<int> sortedRes3{0,1,2,3,4,5};THEN("test insertSort"){REQUIRE(v3.size()==6);REQUIRE(insertSort(v3)== sortedRes3);}}
std::vector<int> v4{9,23,52,3,0,2};WHEN("size of vector is 6"){
vector<int> sortedRes4{0,2,3,9,23,52};THEN("test insertSort"){REQUIRE(v4.size()==6);REQUIRE(insertSort(v4)== sortedRes4);}}}}
1.2 二分插入排序
最坏情况下,O(nlogn)
原理:将直接插入排序的插入合适位置的那部分算法用折半查找法替换
#include<vector>
using namespace std;#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file#include"catch.hpp"
vector<int>&binaryInsertSort(vector<int>&v){int len = v.size();if(len ==0|| len ==1)return v;for(int i =1; i<len;++i){int insertNum = v[i];//寻找插入的位置int left=0, right=i-1;while(left<=right){int middle =(left+right)/2;if(insertNum < v[middle])
right = middle-1;else
left = middle +1;}//插入待排序元素到已排序集合中for(int j=i; j>left;--j)
v[j]= v[j-1];
v[left]= insertNum;}return v;}SCENARIO("二分插入排序","[vector]"){GIVEN("some vectors with some items"){
vector<int> v1{};WHEN("size of vector is 0"){
vector<int> sortedRes1{};THEN("test insertSort"){REQUIRE(v1.size()==0);REQUIRE(binaryInsertSort(v1)== sortedRes1);}}
vector<int> v2{1};WHEN("size of vector is 1"){
vector<int> sortedRes2{1};THEN("test insertSort"){REQUIRE(v2.size()==1);REQUIRE(binaryInsertSort(v2)== sortedRes2);}}
vector<int> v3{5,4,3,2,1,0};WHEN("size of vector is 6"){
vector<int> sortedRes3{0,1,2,3,4,5};THEN("test insertSort"){REQUIRE(v3.size()==6);REQUIRE(binaryInsertSort(v3)== sortedRes3);}}
vector<int> v4{9,23,52,3,0,2};WHEN("size of vector is 6"){
vector<int> sortedRes4{0,2,3,9,23,52};THEN("test insertSort"){REQUIRE(v4.size()==6);REQUIRE(binaryInsertSort(v4)== sortedRes4);}}}}
#include<vector>
using namespace std;#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file#include"catch.hpp"
vector<int>&shellInsertSort(vector<int>&v){int len = v.size();if(len ==0|| len ==1)return v;int gap = len/2;while(gap){for(int i=gap; i<len;++i){int insertNum = v[i];int j = i;while(j>=gap && insertNum< v[j-gap]){
v[j]= v[j-gap];
j -= gap;}
v[j]= insertNum;}
gap /=2;}return v;}SCENARIO("希尔插入排序","[vector]"){GIVEN("some vectors with some items"){
vector<int> v1{};WHEN("size of vector is 0"){
vector<int> sortedRes1{};THEN("test insertSort"){REQUIRE(v1.size()==0);REQUIRE(shellInsertSort(v1)== sortedRes1);}}
vector<int> v2{1};WHEN("size of vector is 1"){
vector<int> sortedRes2{1};THEN("test insertSort"){REQUIRE(v2.size()==1);REQUIRE(shellInsertSort(v2)== sortedRes2);}}
vector<int> v3{5,4,3,2,1,0};WHEN("size of vector is 6"){
vector<int> sortedRes3{0,1,2,3,4,5};THEN("test insertSort"){REQUIRE(v3.size()==6);REQUIRE(shellInsertSort(v3)== sortedRes3);}}
vector<int> v4{9,23,52,3,0,2};WHEN("size of vector is 6"){
vector<int> sortedRes4{0,2,3,9,23,52};THEN("test insertSort"){REQUIRE(v4.size()==6);REQUIRE(shellInsertSort(v4)== sortedRes4);}}}}
2 交换排序
2.1 冒泡排序
最坏情况,O(n^2),最坏情况下比较的次数为:
n
∗
(
n
−
1
)
/
2
n*(n-1)/2
n∗(n−1)/2,相应的对象移动的次数为3/2n(n-1)
原理:在一组数据中,相邻元素依次比较大小,最大的放后面,最小的冒上来
#include<vector>
using namespace std;#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file#include"catch.hpp"
vector<int>&bubble(vector<int>&v){int len = v.size();//进行len-1次冒泡,所有元素均已排好序for(int i=0; i<len-1;++i){//在剩下的len-1-i次 冒泡中,从头开始相邻元素进行比较,最后最大的元素沉到最底下for(int j=0; j<len-1-i;++j)if( v[j]> v[j+1])//交换相邻元素swap(v[j], v[j+1]);}return v;}SCENARIO("冒泡排序","[vector]"){GIVEN("some vectors with some items"){
vector<int> v1{};WHEN("size of vector is 0"){
vector<int> sortedRes1{};THEN("test insertSort"){REQUIRE(v1.size()==0);REQUIRE(bubble(v1)== sortedRes1);}}
vector<int> v2{1};WHEN("size of vector is 1"){
vector<int> sortedRes2{1};THEN("test insertSort"){REQUIRE(v2.size()==1);REQUIRE(bubble(v2)== sortedRes2);}}
vector<int> v3{5,4,3,2,1,0};WHEN("size of vector is 6"){
vector<int> sortedRes3{0,1,2,3,4,5};THEN("test insertSort"){REQUIRE(v3.size()==6);REQUIRE(bubble(v3)== sortedRes3);}}
vector<int> v4{9,23,52,3,0,2};WHEN("size of vector is 6"){
vector<int> sortedRes4{0,2,3,9,23,52};THEN("test insertSort"){REQUIRE(v4.size()==6);REQUIRE(bubble(v4)== sortedRes4);}}}}
2.2 鸡尾酒排序
最坏和平均情况,O(n^2),最坏情况下比较的次数为:
n
∗
(
n
−
1
)
/
2
n*(n-1)/2
n∗(n−1)/2
#include<vector>
using namespace std;#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file#include"catch.hpp"
vector<int>&shaker(vector<int>&v){int len = v.size();int left=0, right=len-1;while(left < right){//从左向右,将最大元素放入最后面for(int i = left; i < right;++i)if(v[i]> v[i +1])swap(v[i], v[i +1]);
right--;//从右向左,将最小元素放到最前面for(int i=right; i>left;--i)if(v[i]<v[i-1])swap(v[i], v[i-1]);
left++;}return v;}SCENARIO("鸡尾酒排序","[vector]"){GIVEN("some vectors with some items"){
vector<int> v1{};WHEN("size of vector is 0"){
vector<int> sortedRes1{};THEN("test insertSort"){REQUIRE(v1.size()==0);REQUIRE(shaker(v1)== sortedRes1);}}
vector<int> v2{1};WHEN("size of vector is 1"){
vector<int> sortedRes2{1};THEN("test insertSort"){REQUIRE(v2.size()==1);REQUIRE(shaker(v2)== sortedRes2);}}
vector<int> v3{5,4,3,2,1,0};WHEN("size of vector is 6"){
vector<int> sortedRes3{0,1,2,3,4,5};THEN("test insertSort"){REQUIRE(v3.size()==6);REQUIRE(shaker(v3)== sortedRes3);}}
vector<int> v4{9,23,52,3,0,2};WHEN("size of vector is 6"){
vector<int> sortedRes4{0,2,3,9,23,52};THEN("test insertSort"){REQUIRE(v4.size()==6);REQUIRE(shaker(v4)== sortedRes4);}}}}
#include<vector>
using namespace std;#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file#include"catch.hpp"voidquickSort(vector<int>&v,int left,int right){//当输入数组为空或者只有一个元素时,不做处理,直接返回if(left == right)return;int i=left, j=right;//选择最左边的元素为基数int pivot = v[left];while(i<j){while(i<j && v[j]>=pivot) j--;//找到比基数小的元素,如果它还在最左边元素的右边,就交换它与最左边元素值,把它作为基数if(i<j)swap(v[i], v[j]);while(i<j && v[i]<=pivot) i++;//找到比基数大的元素,如果它还在基数的左边,就交换它与基数, 把它作为基数if(i<j)swap(v[i], v[j]);}if(i != left)quickSort(v, left, i-1);//对v[left...i-1]排序if(j != right)quickSort(v, j+1, right);//对v[j+1...right]排序}// int main()// {// vector<int> a{8,2,6,12,1,9,5,5,10};// int i;// quickSort(a,0,8);/*排好序的结果*/// for(i=0;i<8;i++)// printf("%4d",a[i]);// getchar();// return 0;// }SCENARIO("排序","[vector]"){GIVEN("some vectors with some items"){
vector<int> v1{};WHEN("size of vector is 0"){
vector<int> sortedRes1{};THEN("test insertSort"){REQUIRE(v1.size()==0);quickSort(v1,-1,-1);//这里-1表示输入数组为空REQUIRE( v1 == sortedRes1);}}
vector<int> v2{1};WHEN("size of vector is 1"){
vector<int> sortedRes2{1};THEN("test insertSort"){REQUIRE(v2.size()==1);quickSort(v2,0,0);REQUIRE( v2 == sortedRes2);}}
vector<int> v3{5,4,3,2,1,0};WHEN("size of vector is 6"){
vector<int> sortedRes3{0,1,2,3,4,5};THEN("test insertSort"){REQUIRE(v3.size()==6);quickSort(v3,0,5);REQUIRE( v3 == sortedRes3);}}
vector<int> v4{9,23,52,3,0,2};WHEN("size of vector is 6"){
vector<int> sortedRes4{0,2,3,9,23,52};THEN("test insertSort"){REQUIRE(v4.size()==6);quickSort(v4,0,5);REQUIRE( v4 == sortedRes4);}}
vector<int> v5{10,9,8,7,6,5,4,3,2,1,0};WHEN("size of vector is 11"){
vector<int> sortedRes5{0,1,2,3,4,5,6,7,8,9,10};THEN("test insertSort"){REQUIRE(v5.size()==11);quickSort(v5,0,10);REQUIRE( v5 == sortedRes5);}}}}
3 选择排序
3.1 直接选择排序
最坏情况,O(n^2)
原理:每次从数组v[0],…v[len-1]中找到最小的元素v[k],然后与v[i]交换位置
#include<vector>
using namespace std;#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file#include"catch.hpp"
vector<int>&directSlecetSort(vector<int>&v){int len = v.size();for(int i=0; i<len;++i){int k = i;//暂时选择第i个元素为当前最小值//寻找最小的元素for(int j=i+1; j<len;++j)if( v[j]< v[k]) k=j;//交换v[i]与最小值v[k]swap(v[i], v[k]);}return v;}SCENARIO("直接选择排序","[vector]"){GIVEN("some vectors with some items"){
vector<int> v1{};WHEN("size of vector is 0"){
vector<int> sortedRes1{};THEN("test insertSort"){REQUIRE(v1.size()==0);REQUIRE(directSlecetSort(v1)== sortedRes1);}}
vector<int> v2{1};WHEN("size of vector is 1"){
vector<int> sortedRes2{1};THEN("test insertSort"){REQUIRE(v2.size()==1);REQUIRE(directSlecetSort(v2)== sortedRes2);}}
vector<int> v3{5,4,3,2,1,0};WHEN("size of vector is 6"){
vector<int> sortedRes3{0,1,2,3,4,5};THEN("test insertSort"){REQUIRE(v3.size()==6);REQUIRE(directSlecetSort(v3)== sortedRes3);}}
vector<int> v4{9,23,52,3,0,2};WHEN("size of vector is 6"){
vector<int> sortedRes4{0,2,3,9,23,52};THEN("test insertSort"){REQUIRE(v4.size()==6);REQUIRE(directSlecetSort(v4)== sortedRes4);}}}}