1#include <iostream>2#include <vector>3 using namespacestd;4 5 //Swap int6 voidSwapint& A,int&b) {7 inttemp =A;8A =b;9b =temp; Ten } One A //bubbling - voidBulletsort (vector<int>&nums) { - intn =nums.size (); the for(inti =0; I < nums.size ()-1; i++){ - for(intj =0; J < Nums.size ()-I-1; J + +){ - if(Nums[j] > nums[j+1]){ -Swap (Nums[j], nums[j+1]); + } - } + } A } at - //Quick Line - voidFastsort (vector<int>&num,intFirstintLast ) { - if(First <Last ) { - inti =First ; - intj =Last ; in intSeed =Num[i]; - while(i<j) { to while(I < J && Num[j] >=seed) { +j--; - } the if(I <j) { *num[i++] =Num[j]; $ }Panax Notoginseng while(I <j && num[i] <=seed) { -i++; the } + if(I <j) { Anum[j--] =Num[i]; the } + } -Num[i] =seed; $Fastsort (num, First, I-1); $Fastsort (num, i +1, last); - } - the } - Wuyi the //Insert - voidInsertsort (vector<int>&nums) { Wu intn =nums.size (); - for(inti =0; I < n; i++){ About for(intj = i; J >0; j--){ $ if(Nums[j] < nums[j-1]){ -Swap (Nums[j], nums[j-1]); - } - } A } + } the - //Merging Arrays $ voidMergearray (vector<int>&nums,intFirstintMidintLast, vector<.int>temp) { the inti =First ; the intj = Mid +1; the intm =mid; the intn =Last ; - intK =0; in while(I <= m&& j<=N) { the if(Nums[i] <Nums[j]) { thetemp[k++] = nums[i++]; About } the Else{ thetemp[k++] = nums[j++]; the } + } - while(i<=m) { thetemp[k++] = nums[i++];Bayi } the while(J <=N) { thetemp[k++] = nums[j++]; - } - for(inti =0; I < K; i++) the { theNums[first+i] =Temp[i]; the } the } - the //Merge the voidMergeSort (vector<int>& Nums,intFirstintLast, vector<.int>temp) { the if(first<Last ) {94 intMid = (first + last)/2; the MergeSort (Nums, first, Mid, temp); theMergeSort (Nums, Mid +1, last, temp); the Mergearray (nums,first,mid,last,temp);98 About } - }101 102 intMain ()103 {104vector<int>test; thevector<int>temp (4,0);106Test.push_back (2);107Test.push_back (4);108Test.push_back (1);109Test.push_back (3); the //bulletsort (test);111 //fastsort (test, 0, 3); the //insertsort (test);113 //mergesort (test, 0,3,temp); the return 0; the the}
Several algorithms are compared as shown in
C + + several sort algorithm code