// C++ program to implement gravity/bead sort#include<cstdio>#include<cstring>#define BEAD(i, j) beads[i * max + j]// function to perform the above algorithmvoidbeadSort(int*a,intlen){// Find the maximum elementintmax=a[0];for(inti=1;i<len;i++)if(a[i]>max)max=a[i];// allocating memoryunsignedchar*beads=newunsignedchar[max*len];memset(beads,0,static_cast<size_t>(max)*len);// mark the beadsfor(inti=0;i<len;i++)for(intj=0;j<a[i];j++)BEAD(i,j)=1;for(intj=0;j<max;j++){// count how many beads are on each postintsum=0;for(inti=0;i<len;i++){sum+=BEAD(i,j);BEAD(i,j)=0;}// Move beads downfor(inti=len-sum;i<len;i++)BEAD(i,j)=1;}// Put sorted values in array using beadsfor(inti=0;i<len;i++){intj;for(j=0;j<max&&BEAD(i,j);j++){}a[i]=j;}delete[]beads;}// driver function to test the algorithmintmain(){inta[]={5,3,1,7,4,1,1,20};intlen=sizeof(a)/sizeof(a[0]);beadSort(a,len);for(inti=0;i<len;i++)printf("%d ",a[i]);return0;}
// Source : https://www.geeksforgeeks.org/bitonic-sort//* C++ Program for Bitonic Sort. Note that this program works only when size of input is a power of 2. */#include<algorithm>#include<iostream>/*The parameter dir indicates the sorting direction, ASCENDING or DESCENDING; if (a[i] > a[j]) agrees with the direction, then a[i] and a[j] are interchanged.*/voidcompAndSwap(inta[],inti,intj,intdir){if(dir==(a[i]>a[j]))std::swap(a[i],a[j]);}/*It recursively sorts a bitonic sequence in ascending order, if dir = 1, and in descending order otherwise (means dir=0). The sequence to be sorted starts at index position low, the parameter cnt is the number of elements to be sorted.*/voidbitonicMerge(inta[],intlow,intcnt,intdir){if(cnt>1){intk=cnt/2;for(inti=low;i<low+k;i++)compAndSwap(a,i,i+k,dir);bitonicMerge(a,low,k,dir);bitonicMerge(a,low+k,k,dir);}}/* This function first produces a bitonic sequence by recursively sorting its two halves in opposite sorting orders, and then calls bitonicMerge to make them in the same order */voidbitonicSort(inta[],intlow,intcnt,intdir){if(cnt>1){intk=cnt/2;// sort in ascending order since dir here is 1bitonicSort(a,low,k,1);// sort in descending order since dir here is 0bitonicSort(a,low+k,k,0);// Will merge wole sequence in ascending order// since dir=1.bitonicMerge(a,low,cnt,dir);}}/* Caller of bitonicSort for sorting the entire array of length N in ASCENDING order */voidsort(inta[],intN,intup){bitonicSort(a,0,N,up);}// Driver codeintmain(){inta[]={3,7,4,8,6,2,1,5};intN=sizeof(a)/sizeof(a[0]);intup=1;// means sort in ascending ordersort(a,N,up);std::cout<<"Sorted array: \n";for(inti=0;i<N;i++)std::cout<<a[i]<<" ";return0;}
/** * @file * @brief Implementation of [Bogosort algorithm](https://en.wikipedia.org/wiki/Bogosort) * * @details * In computer science, bogosort (also known as permutation sort, stupid sort, slowsort, * shotgun sort, random sort, monkey sort, bobosort or shuffle sort) is a highly inefficient * sorting algorithm based on the generate and test paradigm. Two versions of this algorithm * exist: a deterministic version that enumerates all permutations until it hits a sorted one, * and a randomized version that randomly permutes its input.Randomized version is implemented here. * * ### Algorithm * Shuffle the array untill array is sorted. * * @author [Deep Raval](https://github.com/imdeep2905) */#include<iostream>#include<algorithm>#include<array>#include<cassert>/** * @namespace sorting * @brief Sorting algorithms */namespacesorting{/** * Function to shuffle the elements of an array. (for reference) * @tparam T typename of the array * @tparam N length of array * @param arr array to shuffle * @returns new array with elements shuffled from a given array */template<typenameT,size_tN>std::array<T,N>shuffle(std::array<T,N>arr){for(inti=0;i<N;i++){// Swaps i'th index with random index (less than array size)std::swap(arr[i],arr[std::rand()%N]);}returnarr;}/** * Implement randomized Bogosort algorithm and sort the elements of a given array. * @tparam T typename of the array * @tparam N length of array * @param arr array to sort * @returns new array with elements sorted from a given array */template<typenameT,size_tN>std::array<T,N>randomized_bogosort(std::array<T,N>arr){// Untill array is not sortedwhile(!std::is_sorted(arr.begin(),arr.end())){std::random_shuffle(arr.begin(),arr.end());// Shuffle the array}returnarr;}}// namespace sorting/** * Function to display array on screen * @tparam T typename of the array * @tparam N length of array * @param arr array to display */template<typenameT,size_tN>voidshow_array(conststd::array<T,N>&arr){for(intx:arr){std::cout<<x<<' ';}std::cout<<'\n';}/** * Function to test above algorithm */voidtest(){// Test 1std::array<int,5>arr1;for(int&x:arr1){x=std::rand()%100;}std::cout<<"Original Array : ";show_array(arr1);arr1=sorting::randomized_bogosort(arr1);std::cout<<"Sorted Array : ";show_array(arr1);assert(std::is_sorted(arr1.begin(),arr1.end()));// Test 2std::array<int,5>arr2;for(int&x:arr2){x=std::rand()%100;}std::cout<<"Original Array : ";show_array(arr2);arr2=sorting::randomized_bogosort(arr2);std::cout<<"Sorted Array : ";show_array(arr2);assert(std::is_sorted(arr2.begin(),arr2.end()));}/** Driver Code */intmain(){// Testingtest();// Example Usagestd::array<int,5>arr={3,7,10,4,1};// Defining array which we want to sortstd::cout<<"Original Array : ";show_array(arr);arr=sorting::randomized_bogosort(arr);// Callling bogo sort on itstd::cout<<"Sorted Array : ";show_array(arr);// Printing sorted arrayreturn0;}
/** * @file * @brief Bubble sort algorithm * * The working principle of the Bubble sort algorithm:Bubble sort algorithm is the bubble sorting algorithm. The most important reasonfor calling the bubble is that the largest number is thrown at the end of thisalgorithm. This is all about the logic. In each iteration, the largest number isexpired and when iterations are completed, the sorting takes place.What is Swap?Swap in the software means that two variables are displaced.An additional variable is required for this operation. x = 5, y = 10.We want x = 10, y = 5. Here we create the most variable to do it.int z;z = x;x = y;y = z;The above process is a typical displacement process.When x assigns the value to x, the old value of x is lost.That's why we created a variable z to create the first value of the value of x,and finally, we have assigned to y.Bubble Sort Algorithm Analysis (Best Case - Worst Case - Average Case)Bubble Sort Worst Case Performance is O (n²). Why is that? Because if youremember Big O Notation, we were calculating the complexity of the algorithms inthe nested loops. The n * (n - 1) product gives us O (n²) performance. In theworst case all the steps of the cycle will occur. Bubble Sort (Avarage Case)Performance. Bubble Sort is not an optimal algorithm. in average, O (n²)performance is taken. Bubble Sort Best Case Performance. O (n). However, youcan't get the best status in the code we shared above. This happens on theoptimized bubble sort algorithm. It's right down there.*/#include<iostream>#include<vector>intmain(){intn;boolswap_check=true;std::cout<<"Enter the amount of numbers to sort: ";std::cin>>n;std::vector<int>numbers;std::cout<<"Enter "<<n<<" numbers: ";intnum;// Inputfor(inti=0;i<n;i++){std::cin>>num;numbers.push_back(num);}// Bubble Sortingfor(inti=0;(i<n)&&(swap_check);i++){swap_check=false;for(intj=0;j<n-1-i;j++){if(numbers[j]>numbers[j+1]){swap_check=true;std::swap(numbers[j],numbers[j+1]);// by changing swap location.// I mean, j. If the number is// greater than j + 1, then it// means the location.}}}// Outputstd::cout<<"\nSorted Array : ";for(inti=0;i<numbers.size();i++){if(i!=numbers.size()-1){std::cout<<numbers[i]<<", ";}else{std::cout<<numbers[i]<<std::endl;}}return0;}
// C++ program to sort an array using bucket sort#include<algorithm>#include<iostream>#include<vector>// Function to sort arr[] of size n using bucket sortvoidbucketSort(floatarr[],intn){// 1) Create n empty bucketsstd::vector<float>*b=newstd::vector<float>[n];// 2) Put array elements in different bucketsfor(inti=0;i<n;i++){intbi=n*arr[i];// Index in bucketb[bi].push_back(arr[i]);}// 3) Sort individual bucketsfor(inti=0;i<n;i++)std::sort(b[i].begin(),b[i].end());// 4) Concatenate all buckets into arr[]intindex=0;for(inti=0;i<n;i++)for(intj=0;j<b[i].size();j++)arr[index++]=b[i][j];delete[]b;}/* Driver program to test above funtion */intmain(){floatarr[]={0.897,0.565,0.656,0.1234,0.665,0.3434};intn=sizeof(arr)/sizeof(arr[0]);bucketSort(arr,n);std::cout<<"Sorted array is \n";for(inti=0;i<n;i++)std::cout<<arr[i]<<" ";return0;}
// Returns Sorted elements after performing Cocktail Selection Sort// It is a Sorting algorithm which chooses the minimum and maximum element in an// array simultaneously, and swaps it with the lowest and highest available// position iteratively or recursively#include<algorithm>#include<iostream>#include<vector>// Iterative VersionvoidCocktailSelectionSort(std::vector<int>*vec,intlow,inthigh){while(low<=high){intminimum=(*vec)[low];intminimumindex=low;intmaximum=(*vec)[high];intmaximumindex=high;for(inti=low;i<=high;i++){if((*vec)[i]>=maximum){maximum=(*vec)[i];maximumindex=i;}if((*vec)[i]<=minimum){minimum=(*vec)[i];minimumindex=i;}}if(low!=maximumindex||high!=minimumindex){std::swap((*vec)[low],(*vec)[minimumindex]);std::swap((*vec)[high],(*vec)[maximumindex]);}else{std::swap((*vec)[low],(*vec)[high]);}low++;high--;}}// Recursive VersionvoidCocktailSelectionSort_v2(std::vector<int>*vec,intlow,inthigh){if(low>=high)return;intminimum=(*vec)[low];intminimumindex=low;intmaximum=(*vec)[high];intmaximumindex=high;for(inti=low;i<=high;i++){if((*vec)[i]>=maximum){maximum=(*vec)[i];maximumindex=i;}if((*vec)[i]<=minimum){minimum=(*vec)[i];minimumindex=i;}}if(low!=maximumindex||high!=minimumindex){std::swap((*vec)[low],(*vec)[minimumindex]);std::swap((*vec)[high],(*vec)[maximumindex]);}else{std::swap((*vec)[low],(*vec)[high]);}CocktailSelectionSort(vec,low+1,high-1);}// main function, select any one of iterative or recursive versionintmain(){intn;std::cout<<"Enter number of elements\n";std::cin>>n;std::vector<int>v(n);std::cout<<"Enter all the elements\n";for(inti=0;i<n;++i){std::cin>>v[i];}intmethod;std::cout<<"Enter method: \n\t0: iterative\n\t1: recursive:\t";std::cin>>method;if(method==0){CocktailSelectionSort(&v,0,n-1);}elseif(method==1){CocktailSelectionSort_v2(&v,0,n-1);}else{std::cerr<<"Unknown method"<<std::endl;return-1;}std::cout<<"Sorted elements are\n";for(inti=0;i<n;++i){std::cout<<v[i]<<" ";}return0;}
/** * * \file * \brief [Comb Sort Algorithm * (Comb Sort)](https://en.wikipedia.org/wiki/Comb_sort) * * \author * * \details * - A better version of bubble sort algorithm * - Bubble sort compares adjacent values whereas comb sort uses gap larger * than 1 * - Best case Time complexity O(n) * Worst case Time complexity O(n^2) * */#include<algorithm>#include<cassert>#include<iostream>/** * * Find the next gap by shrinking the current gap by shrink factor of 1.3 * @param gap current gap * @return new gap * */intFindNextGap(intgap){gap=(gap*10)/13;returnstd::max(1,gap);}/** Function to sort array * * @param arr array to be sorted * @param l start index of array * @param r end index of array * */voidCombSort(int*arr,intl,intr){/** * * initial gap will be maximum and the maximum possible value is * the size of the array that is n and which is equal to r in this * case so to avoid passing an extra parameter n that is the size of * the array we are using r to initialize the initial gap. * */intgap=r;/// Initialize swapped as true to make sure that loop runsboolswapped=true;/// Keep running until gap = 1 or none elements were swappedwhile(gap!=1||swapped){/// Find next gapgap=FindNextGap(gap);swapped=false;/// Compare all elements with current gapfor(inti=l;i<r-gap;++i){if(arr[i]>arr[i+gap]){std::swap(arr[i],arr[i+gap]);swapped=true;}}}}voidtests(){/// Test 1intarr1[10]={34,56,6,23,76,34,76,343,4,76};CombSort(arr1,0,10);assert(std::is_sorted(arr1,arr1+10));std::cout<<"Test 1 passed\n";/// Test 2intarr2[8]={-6,56,-45,56,0,-1,8,8};CombSort(arr2,0,8);assert(std::is_sorted(arr2,arr2+8));std::cout<<"Test 2 Passed\n";}/** Main function */intmain(){/// Running predefined teststests();/// For user interactionintn;std::cin>>n;int*arr=newint[n];for(inti=0;i<n;++i)std::cin>>arr[i];CombSort(arr,0,n);for(inti=0;i<n;++i)std::cout<<arr[i]<<' ';delete[]arr;return0;}
8、Count Inversions
Count Inversions排序,也称为Merge Sort With Inversions Counting,是一种使用Count Inversions算法计算逆序对数量的排序算法。在排序过程中,算法会对数组进行分割、排序和合并,同时计算数组中逆序对的数量。
/** * @file * @brief Counting Inversions using [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) * * @details * Program to count the number of inversions in an array * using merge-sort. * * The count of inversions help to determine how close the array * is to be sorted in ASCENDING order. * * two elements a[i] and a[j] form an inversion if `a[i]` > `a[j]` and i < j * * Time Complexity --> `O(n.log n)` * Space Complexity --> `O(n)` ; additional array `temp[1..n]` * ### Algorithm * 1. The idea is similar to merge sort, divide the array into two equal or almost * equal halves in each step until the base case is reached. * 2. Create a function merge that counts the number of inversions when two halves of * the array are merged, create two indices i and j, i is the index for first half * and j is an index of the second half. if `a[i]` is greater than `a[j]`, then there are (mid – i) * inversions, Because left and right subarrays are sorted, so all the remaining elements * in left-subarray (a[i+1], a[i+2] … a[mid]) will be greater than a[j]. * 3. Create a recursive function to divide the array into halves and find the answer by summing * the number of inversions is the first half, number of inversion in the second half and * the number of inversions by merging the two. * 4. The base case of recursion is when there is only one element in the given half. * 5. Print the answer * * @author [Rakshit Raj](https://github.com/rakshitraj) */#include<cassert> /// for assert#include<cstdint> /// for typedef datatype uint64_t#include<iostream> /// for IO operations#include<vector> /// for std::vector/** * @namespace sorting * @brief Sorting algorithms */namespacesorting{/** * @namespace inversion * @brief Functions for counting inversions using Merge Sort algorithm */namespaceinversion{// Functions used --->// int mergeSort(int* arr, int* temp, int left, int right);// int merge(int* arr, int* temp, int left, int mid, int right);// int countInversion(int* arr, const int size);// void show(int* arr, const int size);/** * @brief Function to merge two sub-arrays. * * @details * merge() function is called from mergeSort() * to merge the array after it split for sorting * by the mergeSort() funtion. * * In this case the merge fuction will also count and return * inversions detected when merging the sub arrays. * * @param arr input array, data-menber of vector * @param temp stores the resultant merged array * @param left lower bound of `arr[]` and left-sub-array * @param mid midpoint, upper bound of left sub-array, * `(mid+1)` gives the lower bound of right-sub-array * @param right upper bound of `arr[]` and right-sub-array * @returns number of inversions found in merge step */template<typenameT>uint32_tmerge(T*arr,T*temp,uint32_tleft,uint32_tmid,uint32_tright){uint32_ti=left;/* i --> index of left sub-array */uint32_tj=mid+1;/* j --> index for right sub-array */uint32_tk=left;/* k --> index for resultant array temp */uint32_tinv_count=0;// inversion countwhile((i<=mid)&&(j<=right)){if(arr[i]<=arr[j]){temp[k++]=arr[i++];}else{temp[k++]=arr[j++];inv_count+=(mid-i+1);// tricky; may vary depending on selection of sub-array}}// Add remaining elements from the larger subarray to the end of tempwhile(i<=mid){temp[k++]=arr[i++];}while(j<=right){temp[k++]=arr[j++];}// Copy temp[] to arr[]for(k=left;k<=right;k++){arr[k]=temp[k];}returninv_count;}/** * @brief Implement merge Sort and count inverions while merging * * @details * The mergeSort() function implements Merge Sort, a * Divide and conquer algorithm, it divides the input * array into two halves and calls itself for each * sub-array and then calls the merge() function to * merge the two halves. * * @param arr - array to be sorted * @param temp - merged resultant array * @param left - lower bound of array * @param right - upper bound of array * @returns number of inversions in array */template<typenameT>uint32_tmergeSort(T*arr,T*temp,uint32_tleft,uint32_tright){uint32_tmid=0,inv_count=0;if(right>left){// midpoint to split the arraymid=(right+left)/2;// Add inversions in left and right sub-arraysinv_count+=mergeSort(arr,temp,left,mid);// left sub-arrayinv_count+=mergeSort(arr,temp,mid+1,right);// inversions in the merge stepinv_count+=merge(arr,temp,left,mid,right);}returninv_count;}/** * @brief Function countInversion() returns the number of inversion * present in the input array. Inversions are an estimate of * how close or far off the array is to being sorted. * * @details * Number of inversions in a sorted array is 0. * Number of inversion in an array[1...n] sorted in * non-ascending order is n(n-1)/2, since each pair of elements * contitute an inversion. * * @param arr - array, data member of std::vector<int>, input for counting * inversions * @param array_size - number of elementa in the array * @returns number of inversions in input array, sorts the array */template<classT>uint32_tcountInversion(T*arr,constuint32_tsize){std::vector<T>temp;temp.reserve(size);temp.assign(size,0);returnmergeSort(arr,temp.data(),0,size-1);}/** * @brief UTILITY function to print array. * @param arr[] array to print * @param array_size size of input array arr[] * @returns void * */template<typenameT>voidshow(T*arr,constuint32_tarray_size){std::cout<<"Printing array: \n";for(uint32_ti=0;i<array_size;i++){std::cout<<" "<<arr[i];}std::cout<<"\n";}}// namespace inversion}// namespace sorting/** * @brief Test implementations * @returns void */staticvoidtest(){// Test 1std::vector<uint64_t>arr1={100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};uint32_tsize1=arr1.size();uint32_tinv_count1=4950;uint32_tresult1=sorting::inversion::countInversion(arr1.data(),size1);assert(inv_count1==result1);// Test 2std::vector<int>arr2={22,66,75,23,11,87,2,44,98,43};uint32_tsize2=arr2.size();uint32_tinv_count2=20;uint32_tresult2=sorting::inversion::countInversion(arr2.data(),size2);assert(inv_count2==result2);// Test 3std::vector<double>arr3={33.1,45.2,65.4,76.5,1.0,2.9,5.4,7.7,88.9,12.4};uint32_tsize3=arr3.size();uint32_tinv_count3=21;uint32_tresult3=sorting::inversion::countInversion(arr3.data(),size3);assert(inv_count3==result3);// Test 4std::vector<char>arr4={'a','b','c','d','e'};uint32_tsize4=arr4.size();uint32_tinv_count4=0;uint32_tresult4=sorting::inversion::countInversion(arr4.data(),size4);assert(inv_count4==result4);}// /**// * @brief Program Body contains all main funtionality// * @returns void// */// template <typename T>// static void body() {// // Input your own sequence// uint_t size;// T input;// std::cout << "Enter number of elements:";// std::cin >> size;//// std::vector<T> arr;// arr.reserve(size);//// std::cout << "Enter elements -->\n";// for (uint64_t i=1; i<=size; i++) {// std::cout << "Element "<< i <<" :";// std::cin >> input;// arr.push_back(input);// }//// if (size != arr.size()) {// size = arr.size();// }//// std::cout << "\n";// sorting::inversion::show(arr.data(), size);// std::cout << "\n";//// // Counting inversions// std::cout << "\nThe number of inversions: "<<// sorting::inversion::countInversion(arr.data(), size) << "\n";//// // Output sorted array// std::cout << "\nSorted array --> \n";// sorting::inversion::show(arr.data(), size);// }/** * @brief Main function * @returns 0 on exit */intmain(){test();// Run test implementations// body(); // test your own arrayreturn0;}
// C++ Program for counting sort#include<iostream>usingnamespacestd;voidcountSort(stringarr){stringoutput;intcount[256],i;for(inti=0;i<256;i++)count[i]=0;for(i=0;arr[i];++i)++count[arr[i]];for(i=1;i<256;++i)count[i]+=count[i-1];for(i=0;arr[i];++i){output[count[arr[i]]-1]=arr[i];--count[arr[i]];}for(i=0;arr[i];++i)arr[i]=output[i];cout<<"Sorted character array is "<<arr;}intmain(){stringarr;cin>>arr;countSort(arr);return0;}
/** * @file * @brief Implementation of [Cycle * sort](https://en.wikipedia.org/wiki/Cycle_sort) algorithm * @details * Cycle Sort is a sorting algorithm that works in \f$O(n^2)\f$ time in the best * case and works in \f$O(n^2)\f$ in worst case. If a element is already at its * correct position, do nothing. If a element is not at its correct position, * we then need to move it to its correct position by computing the correct * positions.Therefore, we should make sure the duplicate elements. * @author [TsungHan Ho](https://github.com/dalaoqi) */#include<algorithm> /// for std::is_sorted, std::swap#include<cassert> /// for assert#include<iostream> /// for io operations#include<vector> /// for std::vector/** * @namespace sorting * @brief Sorting algorithms */namespacesorting{/** * @namespace cycle_sort * @brief Functions for [Cycle sort](https://en.wikipedia.org/wiki/Cycle_sort) * algorithm */namespacecycle_sort{/** * @brief The main function implements cycleSort * @tparam T type of array * @param in_arr array to be sorted * @returns void */template<typenameT>std::vector<T>cycleSort(conststd::vector<T>&in_arr){std::vector<T>arr(in_arr);for(intcycle_start=0;cycle_start<=arr.size()-1;cycle_start++){// initialize itemTitem=arr[cycle_start];// Count the number of elements smaller than item, this number is the// correct index of item.intpos=cycle_start;for(inti=cycle_start+1;i<arr.size();i++){if(arr[i]<item){pos++;}}// item is already in correct positionif(pos==cycle_start){continue;}// duplicate elementswhile(item==arr[pos])pos+=1;if(pos==cycle_start){continue;}else{std::swap(item,arr[pos]);}// Rest of the elementswhile(pos!=cycle_start){pos=cycle_start;// Find position where we put the elementfor(size_ti=cycle_start+1;i<arr.size();i++){if(arr[i]<item){pos+=1;}}// duplicate elementswhile(item==arr[pos])pos+=1;if(item==arr[pos]){continue;}else{std::swap(item,arr[pos]);}}}returnarr;}}// namespace cycle_sort}// namespace sorting/** * @brief Test implementations * @returns void */staticvoidtest(){// Test 1// [4, 3, 2, 1] return [1, 2, 3, 4]std::vector<uint32_t>array1={4,3,2,1};std::cout<<"Test 1... ";std::vector<uint32_t>arr1=sorting::cycle_sort::cycleSort(array1);assert(std::is_sorted(std::begin(arr1),std::end(arr1)));std::cout<<"passed"<<std::endl;// [4.3, -6.5, -7.4, 0, 2.7, 1.8] return [-7.4, -6.5, 0, 1.8, 2.7, 4.3]std::vector<double>array2={4.3,-6.5,-7.4,0,2.7,1.8};std::cout<<"Test 2... ";std::vector<double>arr2=sorting::cycle_sort::cycleSort(array2);assert(std::is_sorted(std::begin(arr2),std::end(arr2)));std::cout<<"passed"<<std::endl;// Test 3// [3, 3, 3, 3] return [3, 3, 3, 3]std::vector<uint32_t>array3={3,3,3,3};std::cout<<"Test 3... ";std::vector<uint32_t>arr3=sorting::cycle_sort::cycleSort(array3);assert(std::is_sorted(std::begin(arr3),std::end(arr3)));std::cout<<"passed"<<std::endl;// [9, 4, 6, 8, 14, 3] return [9, 4, 6, 8, 14, 3]std::vector<uint32_t>array4={3,4,6,8,9,14};std::cout<<"Test 4... ";std::vector<uint32_t>arr4=sorting::cycle_sort::cycleSort(array4);assert(std::is_sorted(std::begin(arr4),std::end(arr4)));std::cout<<"passed"<<std::endl;}/** * @brief Main function * @returns 0 on exit */intmain(){test();// execute the testreturn0;}
12、Dnf Sort
Dutch National Flag(DNF)Sort是一种三向切分快速排序的变种,它是由荷兰计算机科学家Edsger W. Dijkstra提出的。这个算法的目的是将一个包含三种不同元素的数组(例如0、1、2)排序,使得所有相同元素都聚在一起。
/** * @file * @brief Implementation of the [DNF * sort](https://www.geeksforgeeks.org/sort-an-array-of-0s-1s-and-2s/) * implementation * @details * C++ program to sort an array with 0, 1 and 2 in a single pass(DNF sort). * Since one traversal of the array is there hence it works in O(n) time * complexity. * @author [Sujal Gupta](https://github.com/heysujal) */#include<algorithm> /// for std::is_sorted#include<cassert> /// for assert#include<iostream> /// for std::swap and io operations#include<vector> /// for std::vector/** * @namespace sorting * @breif Sorting algorithms */namespacesorting{/** * @namespace dnf_sort * @brief Functions for the [DNF * sort](https://en.wikipedia.org/wiki/Dutch_national_flag_problem) * implementation */namespacednf_sort{/** * @brief The main function implements DNF sort * @tparam T type of array * @param a array to be sorted, * @param arr_size size of array * @returns void */template<typenameT>std::vector<T>dnfSort(conststd::vector<T>&in_arr){std::vector<T>arr(in_arr);uint64_tlo=0;uint64_thi=arr.size()-1;uint64_tmid=0;// Iterate till all the elements// are sortedwhile(mid<=hi){switch(arr[mid]){// If the element is 0case0:std::swap(arr[lo++],arr[mid++]);break;// If the element is 1 .case1:mid++;break;// If the element is 2case2:std::swap(arr[mid],arr[hi--]);break;}}returnarr;}}// namespace dnf_sort}// namespace sorting/** * @brief Self-test implementations * @returns void */staticvoidtest(){// 1st test// [1, 0, 2, 1] return [0, 1, 1, 2]std::vector<uint64_t>array1={0,1,1,2};std::cout<<"Test 1... ";std::vector<uint64_t>arr1=sorting::dnf_sort::dnfSort(array1);assert(std::is_sorted(std::begin(arr1),std::end(arr1)));std::cout<<"passed"<<std::endl;// 2nd test// [1, 0, 0, 1, 1, 0, 2, 1] return [0, 0, 0, 1, 1, 1, 1, 2]std::vector<uint64_t>array2={1,0,0,1,1,0,2,1};std::cout<<"Test 2... ";std::vector<uint64_t>arr2=sorting::dnf_sort::dnfSort(array2);assert(std::is_sorted(std::begin(arr2),std::end(arr2)));std::cout<<"passed"<<std::endl;// 3rd test// [1, 1, 0, 0, 1, 2, 2, 0, 2, 1] return [0, 0, 0, 1, 1, 1, 1, 2, 2, 2]std::vector<uint64_t>array3={1,1,0,0,1,2,2,0,2,1};std::cout<<"Test 3... ";std::vector<uint64_t>arr3=sorting::dnf_sort::dnfSort(array3);assert(std::is_sorted(std::begin(arr3),std::end(arr3)));std::cout<<"passed"<<std::endl;// 4th test// [2, 2, 2, 0, 0, 1, 1] return [0, 0, 1, 1, 2, 2, 2]std::vector<uint64_t>array4={2,2,2,0,0,1,1};std::cout<<"Test 4... ";std::vector<uint64_t>arr4=sorting::dnf_sort::dnfSort(array4);assert(std::is_sorted(std::begin(arr4),std::end(arr4)));std::cout<<"passed"<<std::endl;}/** * @brief Main function * @returns 0 on exit */intmain(){test();// execute the testreturn0;}
/** * @file * @brief Implementation of [gnome * sort](https://en.wikipedia.org/wiki/Gnome_sort) algorithm. * @author [beqakd](https://github.com/beqakd) * @author [Krishna Vedala](https://github.com/kvedala) * @details * Gnome sort algorithm is not the best one but it is widely used. * The algorithm iteratively checks the order of pairs in the array. If they are * on right order it moves to the next successive pair, otherwise it swaps * elements. This operation is repeated until no more swaps are made thus * indicating the values to be in ascending order. * * The time Complexity of the algorithm is \f$O(n^2)\f$ and in some cases it * can be \f$O(n)\f$. */#include<algorithm> // for std::swap#include<array> // for std::array#include<cassert> // for assertions#include<iostream> // for io operations/** * @namespace sorting * Sorting algorithms */namespacesorting{/** * This implementation is for a C-style array input that gets modified in place. * @param [in,out] arr our array of elements. * @param size size of given array */template<typenameT>voidgnomeSort(T*arr,intsize){// few easy casesif(size<=1){return;}intindex=0;// initialize some variables.while(index<size){// check for swapif((index==0)||(arr[index]>=arr[index-1])){index++;}else{std::swap(arr[index],arr[index-1]);// swapindex--;}}}/** * This implementation is for a C++-style array input. The function argument is * a pass-by-value and hence a copy of the array gets created which is then * modified by the function and returned. * @tparam T type of data variables in the array * @tparam size size of the array * @param [in] arr our array of elements. * @return array with elements sorted */template<typenameT,size_tsize>std::array<T,size>gnomeSort(std::array<T,size>arr){// few easy casesif(size<=1){returnarr;}intindex=0;// initialize loop indexwhile(index<size){// check for swapif((index==0)||(arr[index]>=arr[index-1])){index++;}else{std::swap(arr[index],arr[index-1]);// swapindex--;}}returnarr;}}// namespace sorting/** * Test function */staticvoidtest(){// Example 1. Creating array of int,std::cout<<"Test 1 - as a C-array...";constintsize=6;std::array<int,size>arr={-22,100,150,35,-10,99};sorting::gnomeSort(arr.data(),size);// pass array data as a C-style array pointerassert(std::is_sorted(std::begin(arr),std::end(arr)));std::cout<<" Passed\n";for(inti=0;i<size;i++){std::cout<<arr[i]<<", ";}std::cout<<std::endl;// Example 2. Creating array of doubles.std::cout<<"\nTest 2 - as a std::array...";std::array<double,size>double_arr={-100.2,10.2,20.0,9.0,7.5,7.2};std::array<double,size>sorted_arr=sorting::gnomeSort(double_arr);assert(std::is_sorted(std::begin(sorted_arr),std::end(sorted_arr)));std::cout<<" Passed\n";for(inti=0;i<size;i++){std::cout<<double_arr[i]<<", ";}std::cout<<std::endl;// Example 3. Creating random array of float.std::cout<<"\nTest 3 - 200 random numbers as a std::array...";constintsize2=200;std::array<float,size2>rand_arr{};for(auto&a:rand_arr){// generate random numbers between -5.0 and 4.99a=float(std::rand()%1000-500)/100.f;}std::array<float,size2>float_arr=sorting::gnomeSort(rand_arr);assert(std::is_sorted(std::begin(float_arr),std::end(float_arr)));std::cout<<" Passed\n";// for (int i = 0; i < size; i++) std::cout << double_arr[i] << ", ";std::cout<<std::endl;}/** * Our main function with example of sort method. */intmain(){test();return0;}
/** * \file * \brief [Heap Sort Algorithm * (heap sort)](https://en.wikipedia.org/wiki/Heapsort) implementation * * \author [Ayaan Khan](http://github.com/ayaankhan98) * * \details * Heap-sort is a comparison-based sorting algorithm. * Heap-sort can be thought of as an improved selection sort: * like selection sort, heap sort divides its input into a sorted * and an unsorted region, and it iteratively shrinks the unsorted * region by extracting the largest element from it and inserting * it into the sorted region. Unlike selection sort, * heap sort does not waste time with a linear-time scan of the * unsorted region; rather, heap sort maintains the unsorted region * in a heap data structure to more quickly find the largest element * in each step. * * Time Complexity - \f$O(n \log(n))\f$ * */#include<algorithm>#include<cassert>#include<iostream>/** * * Utility function to print the array after * sorting. * * @param arr array to be printed * @param sz size of array * */template<typenameT>voidprintArray(T*arr,intsz){for(inti=0;i<sz;i++)std::cout<<arr[i]<<" ";std::cout<<"\n";}/** * * \addtogroup sorting Sorting Algorithm * @{ * * The heapify procedure can be thought of as building a heap from * the bottom up by successively sifting downward to establish the * heap property. * * @param arr array to be sorted * @param n size of array * @param i node position in Binary Tress or element position in * Array to be compared with it's childern * */template<typenameT>voidheapify(T*arr,intn,inti){intlargest=i;intl=2*i+1;intr=2*i+2;if(l<n&&arr[l]>arr[largest])largest=l;if(r<n&&arr[r]>arr[largest])largest=r;if(largest!=i){std::swap(arr[i],arr[largest]);heapify(arr,n,largest);}}/** * Utilizes heapify procedure to sort * the array * * @param arr array to be sorted * @param n size of array * */template<typenameT>voidheapSort(T*arr,intn){for(inti=n-1;i>=0;i--)heapify(arr,n,i);for(inti=n-1;i>=0;i--){std::swap(arr[0],arr[i]);heapify(arr,i,0);}}/** * * @} * Test cases to test the program * */voidtest(){std::cout<<"Test 1\n";intarr[]={-10,78,-1,-6,7,4,94,5,99,0};intsz=sizeof(arr)/sizeof(arr[0]);// sz - size of arrayprintArray(arr,sz);// displaying the array before sortingheapSort(arr,sz);// calling heapsort to sort the arrayprintArray(arr,sz);// display array after sortingassert(std::is_sorted(arr,arr+sz));std::cout<<"Test 1 Passed\n========================\n";std::cout<<"Test 2\n";doublearr2[]={4.5,-3.6,7.6,0,12.9};sz=sizeof(arr2)/sizeof(arr2[0]);printArray(arr2,sz);heapSort(arr2,sz);printArray(arr2,sz);assert(std::is_sorted(arr2,arr2+sz));std::cout<<"Test 2 passed\n";}/** Main function */intmain(){test();return0;}
/** * * \file * \brief [Insertion Sort Algorithm * (Insertion Sort)](https://en.wikipedia.org/wiki/Insertion_sort) * * \details * Insertion sort is a simple sorting algorithm that builds the final * sorted array one at a time. It is much less efficient compared to * other sorting algorithms like heap sort, merge sort or quick sort. * However it has several advantages such as * 1. Easy to implement * 2. For small set of data it is quite efficient * 3. More efficient that other Quadratic complexity algorithms like * Selection sort or bubble sort. * 4. It's stable that is it does not change the relative order of * elements with equal keys * 5. Works on hand means it can sort the array or list as it receives. * * It is based on the same idea that people use to sort the playing cards in * their hands. * the algorithms goes in the manner that we start iterating over the array * of elements as soon as we find a unsorted element that is a misplaced * element we place it at a sorted position. * * Example execution steps: * 1. Suppose initially we have * \f{bmatrix}{4 &3 &2 &5 &1\f} * 2. We start traversing from 4 till we reach 1 * when we reach at 3 we find that it is misplaced so we take 3 and place * it at a correct position thus the array will become * \f{bmatrix}{3 &4 &2 &5 &1\f} * 3. In the next iteration we are at 2 we find that this is also misplaced so * we place it at the correct sorted position thus the array in this iteration * becomes * \f{bmatrix}{2 &3 &4 &5 &1\f} * 4. We do not do anything with 5 and move on to the next iteration and * select 1 which is misplaced and place it at correct position. Thus, we have * \f{bmatrix}{1 &2 &3 &4 &5\f} */#include<algorithm>#include<cassert>#include<iostream>#include<vector>/** \namespace sorting * \brief Sorting algorithms */namespacesorting{/** \brief * Insertion Sort Function * * @tparam T type of array * @param [in,out] arr Array to be sorted * @param n Size of Array */template<typenameT>voidinsertionSort(T*arr,intn){for(inti=1;i<n;i++){Ttemp=arr[i];intj=i-1;while(j>=0&&temp<arr[j]){arr[j+1]=arr[j];j--;}arr[j+1]=temp;}}/** Insertion Sort Function * * @tparam T type of array * @param [in,out] arr pointer to array to be sorted */template<typenameT>voidinsertionSort(std::vector<T>*arr){size_tn=arr->size();for(size_ti=1;i<n;i++){Ttemp=arr[0][i];int32_tj=i-1;while(j>=0&&temp<arr[0][j]){arr[0][j+1]=arr[0][j];j--;}arr[0][j+1]=temp;}}}// namespace sorting/** * @brief Create a random array objecthelper function to create a random array * * @tparam T type of array * @param arr array to fill (must be pre-allocated) * @param N number of array elements */template<typenameT>staticvoidcreate_random_array(T*arr,intN){while(N--){doubler=(std::rand()%10000-5000)/100.f;arr[N]=static_cast<T>(r);}}/** Test Cases to test algorithm */voidtests(){intarr1[10]={78,34,35,6,34,56,3,56,2,4};std::cout<<"Test 1... ";sorting::insertionSort(arr1,10);assert(std::is_sorted(arr1,arr1+10));std::cout<<"passed"<<std::endl;intarr2[5]={5,-3,7,-2,1};std::cout<<"Test 2... ";sorting::insertionSort(arr2,5);assert(std::is_sorted(arr2,arr2+5));std::cout<<"passed"<<std::endl;floatarr3[5]={5.6,-3.1,-3.0,-2.1,1.8};std::cout<<"Test 3... ";sorting::insertionSort(arr3,5);assert(std::is_sorted(arr3,arr3+5));std::cout<<"passed"<<std::endl;std::vector<float>arr4({5.6,-3.1,-3.0,-2.1,1.8});std::cout<<"Test 4... ";sorting::insertionSort(&arr4);assert(std::is_sorted(std::begin(arr4),std::end(arr4)));std::cout<<"passed"<<std::endl;intarr5[50];std::cout<<"Test 5... ";create_random_array(arr5,50);sorting::insertionSort(arr5,50);assert(std::is_sorted(arr5,arr5+50));std::cout<<"passed"<<std::endl;floatarr6[50];std::cout<<"Test 6... ";create_random_array(arr6,50);sorting::insertionSort(arr6,50);assert(std::is_sorted(arr6,arr6+50));std::cout<<"passed"<<std::endl;}/** Main Function */intmain(){/// Running predefined tests to test algorithmtests();/// For user insteractionsize_tn;std::cout<<"Enter the length of your array (0 to exit): ";std::cin>>n;if(n==0){return0;}int*arr=newint[n];std::cout<<"Enter any "<<n<<" Numbers for Unsorted Array : ";for(inti=0;i<n;i++){std::cin>>arr[i];}sorting::insertionSort(arr,n);std::cout<<"\nSorted Array : ";for(inti=0;i<n;i++){std::cout<<arr[i]<<" ";}std::cout<<std::endl;delete[]arr;return0;}
/** * @file * @author [@sinkyoungdeok](https://github.com/sinkyoungdeok) * @author [Krishna Vedala](https://github.com/kvedala) * @brief Algorithm that combines insertion sort and merge sort. [Wiki * link](https://en.wikipedia.org/wiki/Merge-insertion_sort) * * @see Individual algorithms: insertion_sort.cpp and merge_sort.cpp */#include<algorithm>#include<array>#include<cassert>#include<ctime>#include<iostream>#include<memory>/** \namespace sorting * \brief Sorting algorithms */namespacesorting{/** \namespace merge_insertion * \brief Combined Intersion-Merge sorting algorithm */namespacemerge_insertion{/** * @brief Insertion merge algorithm * @see insertion_sort.cpp * * @tparam T array data type * @tparam N length of array * @param A pointer to array to sort * @param start start index of sorting window * @param end end index of sorting window */template<typenameT,size_tN>staticvoidInsertionSort(std::array<T,N>*A,size_tstart,size_tend){size_ti=0,j=0;T*ptr=A->data();for(i=start;i<end;i++){Ttemp=ptr[i];j=i;while(j>start&&temp<ptr[j-1]){ptr[j]=ptr[j-1];j--;}// for (j = i; j > start && temp < ptr[j - 1]; --j) {// ptr[j] = ptr[j - 1];// }ptr[j]=temp;}}/** * @brief Perform merge of data in a window * * @tparam T array data type * @tparam N length of array * @param A pointer to array to sort * @param min start index of window * @param max end index of window * @param mid mid-point of window */template<typenameT,size_tN>staticvoidmerge(std::array<T,N>*array,size_tmin,size_tmax,size_tmid){size_tfirstIndex=min;size_tsecondIndex=mid+1;autoptr=array->data();std::array<T,N+1>tempArray{0};// While there are elements in the left or right runsfor(size_tindex=min;index<=max;index++){// If left run head exists and is <= existing right run head.if(firstIndex<=mid&&(secondIndex>max||ptr[firstIndex]<=ptr[secondIndex])){tempArray[index]=ptr[firstIndex];firstIndex++;}else{tempArray[index]=ptr[secondIndex];secondIndex++;}}// transfer to the initial arraymemcpy(ptr+min,tempArray.data()+min,(max-min)*sizeof(T));// for (int index = min; index <= max; index++) ptr[index] =// tempArray[index];}/** * @brief Final combined algorithm. * Algorithm utilizes ::sorting::merge_insertion::InsertionSort if window length * is less than threshold, else performs merge sort recursively using * ::sorting::merge_insertion::mergeSort * * @tparam T array data type * @tparam N length of array * @param A pointer to array to sort * @param min start index of sort window * @param max end index of sort window * @param threshold window length threshold */template<typenameT,size_tN>voidmergeSort(std::array<T,N>*array,size_tmin,size_tmax,size_tthreshold){// prerequisiteif((max-min)<=threshold){InsertionSort(array,min,max);}else{// get the middle pointsize_tmid=(max+min)>>1;// apply merge sort to both parts of thismergeSort(array,min,mid,threshold);mergeSort(array,mid,max,threshold);// and finally merge all that sorted stuffmerge(array,min,max,mid);}}}// namespace merge_insertion}// namespace sorting/** * @brief Function to test code using random arrays * @returns none */staticvoidtest(){constexprsize_tsize=30;std::array<int,size>array{0};// inputfor(inti=0;i<size;i++){array[i]=std::rand()%100-50;std::cout<<array[i]<<" ";}std::cout<<std::endl;sorting::merge_insertion::InsertionSort(&array,0,size);// sorting::merge_insertion::mergeSort(&array, 0, size, 10);// outputfor(inti=0;i<size;++i){std::cout<<array[i]<<" ";}std::cout<<std::endl;assert(std::is_sorted(std::begin(array),std::end(array)));std::cout<<"Test passed\n";}/** * @brief Main function * @return 0 on exit */intmain(){std::srand(std::time(nullptr));test();return0;}
/** * \addtogroup sorting Sorting Algorithms * @{ * \file * \brief [Merege Sort Algorithm * (MEREGE SORT)](https://en.wikipedia.org/wiki/Merge_sort) implementation * * \author [Ayaan Khan](http://github.com/ayaankhan98) * * \details * Merge Sort is an efficient, general purpose, comparison * based sorting algorithm. * Merge Sort is a divide and conquer algorithm * */#include<iostream>/** * * The merge() function is used for merging two halves. * The merge(arr, l, m, r) is key process that assumes that * arr[l..m] and arr[m+1..r] are sorted and merges the two * sorted sub-arrays into one. * * @param arr - array with two halves arr[l...m] and arr[m+1...l] * @param l - left index or start index of first half array * @param m - right index or end index of first half array * * (The second array starts form m+1 and goes till l) * * @param l - end index or right index of second half array */voidmerge(int*arr,intl,intm,intr){inti,j,k;intn1=m-l+1;intn2=r-m;int*L=newint[n1],*R=newint[n2];for(i=0;i<n1;i++)L[i]=arr[l+i];for(j=0;j<n2;j++)R[j]=arr[m+1+j];i=0;j=0;k=l;while(i<n1||j<n2){if(j>=n2||(i<n1&&L[i]<=R[j])){arr[k]=L[i];i++;}else{arr[k]=R[j];j++;}k++;}delete[]L;delete[]R;}/** * Merge sort is a divide and conquer algorithm, it divides the * input array into two halves and calls itself for the two halves * and then calls merge() to merge the two halves * * @param arr - array to be sorted * @param l - left index or start index of array * @param r - right index or end index of array * */voidmergeSort(int*arr,intl,intr){if(l<r){intm=l+(r-l)/2;mergeSort(arr,l,m);mergeSort(arr,m+1,r);merge(arr,l,m,r);}}/** * Utility function used to print the array after * sorting */voidshow(int*arr,intsize){for(inti=0;i<size;i++)std::cout<<arr[i]<<" ";std::cout<<"\n";}/** Main function */intmain(){intsize;std::cout<<"Enter the number of elements : ";std::cin>>size;int*arr=newint[size];std::cout<<"Enter the unsorted elements : ";for(inti=0;i<size;++i){std::cin>>arr[i];}mergeSort(arr,0,size-1);std::cout<<"Sorted array : ";show(arr,size);delete[]arr;return0;}/** @} */
/** * Copyright 2020 @author Albirair * @file * * A generic implementation of non-recursive merge sort. */#include<cstddef> // for size_t#include<iostream>#include<utility> // for std::move & std::remove_reference_tnamespacesorting{template<classIterator>voidmerge(Iterator,Iterator,constIterator,char[]);/// bottom-up merge sort which sorts elements in a non-decreasing order/** * sorts elements non-recursively by breaking them into small segments, * merging adjacent segments into larger sorted segments, then increasing * the sizes of segments by factors of 2 and repeating the same process. * best-case = worst-case = O(n log(n)) * @param first points to the first element * @param last points to 1-step past the last element * @param n the number of elements */template<classIterator>voidnon_recursive_merge_sort(constIteratorfirst,constIteratorlast,constsize_tn){// create a buffer large enough to store all elements// dynamically allocated to comply with cpplintchar*buffer=newchar[n*sizeof(*first)];// buffer size can be optimized to largest power of 2 less than n// elements divide the container into equally-sized segments whose// length start at 1 and keeps increasing by factors of 2for(size_tlength(1);length<n;length<<=1){// merge adjacent segments whose number is n / (length * 2)Iteratorleft(first);for(size_tcounter(n/(length<<1));counter;--counter){Iteratorright(left+length),end(right+length);merge(left,right,end,buffer);left=end;}// if the number of remaining elements (n * 2 % length) is longer// than a segment, merge the remaining elementsif((n&((length<<1)-1))>length)merge(left,left+length,last,buffer);}delete[]buffer;}/// merges 2 sorted adjacent segments into a larger sorted segment/** * best-case = worst-case = O(n) * @param l points to the left part * @param r points to the right part, end of left part * @param e points to end of right part * @param b points at the buffer */template<classIterator>voidmerge(Iteratorl,Iteratorr,constIteratore,charb[]){// create 2 pointers to point at the bufferautop(reinterpret_cast<std::remove_reference_t<decltype(*l)>*>(b)),c(p);// move the left part of the segmentfor(Iteratort(l);r!=t;++t)*p++=std::move(*t);// while neither the buffer nor the right part has been exhausted// move the smallest element of the two back to the containerwhile(e!=r&&c!=p)*l++=std::move(*r<*c?*r++:*c++);// notice only one of the two following loops will be executed// while the right part hasn't bee exhausted, move it backwhile(e!=r)*l++=std::move(*r++);// while the buffer hasn't bee exhausted, move it backwhile(c!=p)*l++=std::move(*c++);}/// bottom-up merge sort which sorts elements in a non-decreasing order/** * @param first points to the first element * @param n the number of elements */template<classIterator>voidnon_recursive_merge_sort(constIteratorfirst,constsize_tn){non_recursive_merge_sort(first,first+n,n);}/// bottom-up merge sort which sorts elements in a non-decreasing order/** * @param first points to the first element * @param last points to 1-step past the last element */template<classIterator>voidnon_recursive_merge_sort(constIteratorfirst,constIteratorlast){non_recursive_merge_sort(first,last,last-first);}}// namespace sortingusingsorting::non_recursive_merge_sort;intmain(intargc,char**argv){intsize;std::cout<<"Enter the number of elements : ";std::cin>>size;int*arr=newint[size];for(inti=0;i<size;++i){std::cout<<"arr["<<i<<"] = ";std::cin>>arr[i];}non_recursive_merge_sort(arr,size);std::cout<<"Sorted array\n";for(inti=0;i<size;++i)std::cout<<"arr["<<i<<"] = "<<arr[i]<<'\n';delete[]arr;return0;}
// Using general algorithms to sort a collection of strings results in// alphanumeric sort. If it is a numeric string, it leads to unnatural sorting// eg, an array of strings 1,10,100,2,20,200,3,30,300// would be sorted in that same order by using conventional sorting,// even though we know the correct sorting order is 1,2,3,10,20,30,100,200,300// This Programme uses a comparator to sort the array in Numerical order instead// of Alphanumeric order#include<algorithm>#include<iostream>#include<string>#include<vector>boolNumericSort(std::stringa,std::stringb){while(a[0]=='0'){a.erase(a.begin());}while(b[0]=='0'){b.erase(b.begin());}intn=a.length();intm=b.length();if(n==m)returna<b;returnn<m;}intmain(){intn;std::cout<<"Enter number of elements to be sorted Numerically\n";std::cin>>n;std::vector<std::string>v(n);std::cout<<"Enter the string of Numbers\n";for(inti=0;i<n;i++){std::cin>>v[i];}sort(v.begin(),v.end());std::cout<<"Elements sorted normally \n";for(inti=0;i<n;i++){std::cout<<v[i]<<" ";}std::cout<<"\n";std::sort(v.begin(),v.end(),NumericSort);std::cout<<"Elements sorted Numerically \n";for(inti=0;i<n;i++){std::cout<<v[i]<<" ";}return0;}
/* C++ implementation Odd Even Sort */#include<iostream>#include<vector>usingnamespacestd;voidoddEven(vector<int>&arr,intsize){boolsorted=false;while(!sorted){sorted=true;for(inti=1;i<size-1;i+=2)// Odd{if(arr[i]>arr[i+1]){swap(arr[i],arr[i+1]);sorted=false;}}for(inti=0;i<size-1;i+=2)// Even{if(arr[i]>arr[i+1]){swap(arr[i],arr[i+1]);sorted=false;}}}}voidshow(vector<int>A,intsize){inti;for(i=0;i<size;i++)cout<<A[i]<<"\n";}intmain(){intsize,temp;cout<<"\nEnter the number of elements : ";cin>>size;vector<int>arr;cout<<"\nEnter the unsorted elements : \n";for(inti=0;i<size;++i){cin>>temp;arr.push_back(temp);}oddEven(arr,size);cout<<"Sorted array\n";show(arr,size);return0;}
/** * @file * @brief pancake sort sorts a disordered stack of pancakes by flipping any * number of pancakes using a spatula using minimum number of flips. * * @details * Unlike a traditional sorting algorithm, which attempts to sort with the * fewest comparisons possible, the goal is to sort the sequence in as few * reversals as possible. Overall time complexity of pancake sort is O(n^2) For * example: example 1:- Disordered pancake sizes: {2,5,3,7,8} Sorted: * {2,3,5,7,8} For example: example 2:- Disordered pancake sizes: * {22,51,37,73,81} Sorted: {22,37,51,73,81} * @author [Divyansh Gupta](https://github.com/divyansh12323) * @see more on [Pancake sort](https://en.wikipedia.org/wiki/Pancake_sorting) * @see related problem at * [Leetcode](https://leetcode.com/problems/pancake-sorting/) */#include<algorithm> // for std::is_sorted#include<cassert> // for std::assert#include<iostream> // for io operations#include<vector> // for std::vector/** * @namespace sorting * @brief Sorting algorithms */namespacesorting{/** * @namespace pancake_sort * @brief Functions for [Pancake * sort](https://en.wikipedia.org/wiki/Pancake_sorting) algorithm */namespacepancake_sort{/** * @brief This implementation is for reversing elements in a a C-style array . * @param [start,end] arr our vector of elements. * @param start starting index of array * @param end ending index of array * @returns void */template<typenameT>voidreverse(std::vector<T>&arr,intstart,intend){Ttemp;// Temporary variablewhile(start<=end){temp=arr[start];arr[start]=arr[end];arr[end]=temp;start++;end--;}}/** * @brief This implementation is for a C-style array input that gets modified in * place. * @param [start,end] arr our vector of elements. * @param size size of given array * @returns 0 on exit */template<typenameT>intpancakeSort(std::vector<T>&arr,intsize){for(inti=size;i>1;--i){intmax_index=0,j=0;// intialize some variables.Tmax_value=0;for(j=0;j<i;j++){if(arr[j]>=max_value){max_value=arr[j];max_index=j;}}if(max_index!=i-1)// check for reversing{reverse(arr,0,max_index);reverse(arr,0,i-1);}}return0;}}// namespace pancake_sort}// namespace sorting/** * @brief Test implementations * @returns void */staticvoidtest(){// example 1: vector of intconstintsize1=7;std::cout<<"\nTest 1- as std::vector<int>...";std::vector<int>arr1={23,10,20,11,12,6,7};sorting::pancake_sort::pancakeSort(arr1,size1);assert(std::is_sorted(arr1.begin(),arr1.end()));std::cout<<"Passed\n";for(inti=0;i<size1;i++){std::cout<<arr1[i]<<" ,";}std::cout<<std::endl;// example 2: vector of doubleconstintsize2=8;std::cout<<"\nTest 2- as std::vector<double>...";std::vector<double>arr2={23.56,10.62,200.78,111.484,3.9,1.2,61.77,79.6};sorting::pancake_sort::pancakeSort(arr2,size2);assert(std::is_sorted(arr2.begin(),arr2.end()));std::cout<<"Passed\n";for(inti=0;i<size2;i++){std::cout<<arr2[i]<<", ";}std::cout<<std::endl;// example 3:vector of floatconstintsize3=7;std::cout<<"\nTest 3- as std::vector<float>...";std::vector<float>arr3={6.56,12.62,200.78,768.484,19.27,68.87,9.6};sorting::pancake_sort::pancakeSort(arr3,size3);assert(std::is_sorted(arr3.begin(),arr3.end()));std::cout<<"Passed\n";for(inti=0;i<size3;i++){std::cout<<arr3[i]<<", ";}std::cout<<std::endl;}/** * @brief Main function * @returns 0 on exit */intmain(){test();return0;}
/** * @file * @brief Implementation of [Pigeonhole Sort algorithm] * (https://en.wikipedia.org/wiki/Pigeonhole_sort) * @author [Lownish](https://github.com/Lownish) * @details * Pigeonhole sorting is a sorting algorithm that is suitable for sorting lists * of elements where the number of elements and the number of possible key * values are approximately the same. It requires O(n + Range) time where n is * number of elements in input array and ‘Range’ is number of possible values in * array. * * The time Complexity of the algorithm is \f$O(n+N)\f$. */#include<algorithm> //for std::is_sorted#include<array> //for std::array#include<cassert> //for assert#include<iostream> //for io operations/** * @namespace sorting * @brief Sorting algorithms */namespacesorting{/** * Pigeonhole sorting of array of size n * The function will sort the array through Pigeonhole algorithm and print * @param arr unsorted array of elements * @returns sorted array of elements */template<std::size_tN>std::array<int,N>pigeonSort(std::array<int,N>arr){// Finding min and max*automin=std::min_element(std::begin(arr),std::end(arr));automax=std::max_element(std::begin(arr),std::end(arr));// Range refers to the number of holes requiredintrange=*max-*min+1;int*hole=newint[range]();// Copying all array values to pigeonholefor(inti=0;i<N;i++){hole[arr[i]-*min]=arr[i];}// Deleting elements from list and storing to original arrayintcount=0;for(inti=0;i<range;i++){while(hole[i]!='\0'){arr[count]=hole[i];hole[i]={};count++;}}delete[]hole;returnarr;}}// namespace sorting/** * Test function 1 with unsorted array * {8, 3, 2, 7, 4, 6, 8} * @returns none */staticvoidtest_1(){constintn=7;std::array<int,n>test_array={8,3,2,7,4,6,8};test_array=sorting::pigeonSort<n>(test_array);assert(std::is_sorted(std::begin(test_array),std::end(test_array)));// Printing sorted arrayfor(inti=0;i<n;i++){std::cout<<test_array.at(i)<<" ";}std::cout<<"\nPassed\n";}/** * Test function 2 with unsorted array * {802, 630, 20, 745, 52, 300, 612, 932, 78, 187} * @returns none */staticvoidtest_2(){constintn=10;std::array<int,n>test_array={802,630,20,745,52,300,612,932,78,187};test_array=sorting::pigeonSort<n>(test_array);assert(std::is_sorted(std::begin(test_array),std::end(test_array)));// Printing sorted arrayfor(inti=0;i<n;i++){std::cout<<test_array.at(i)<<" ";}std::cout<<"\nPassed\n";}/** * Test function 1 with unsorted array * {11,13,12,14} * @returns none */staticvoidtest_3(){constintn=4;std::array<int,n>test_array={11,13,12,14};test_array=sorting::pigeonSort<n>(test_array);assert(std::is_sorted(std::begin(test_array),std::end(test_array)));// Printing sorted arrayfor(inti=0;i<n;i++){std::cout<<test_array.at(i)<<" ";}std::cout<<"\nPassed\n";}/** * Main function */intmain(){test_1();test_2();test_3();return0;}
/** * @file * @brief Quick sort algorithm * * Implementation Details - * Quick Sort is a divide and conquer algorithm. It picks and element as * pivot and partition the given array around the picked pivot. There * are many different versions of quickSort that pick pivot in different * ways. * * 1. Always pick the first element as pivot * 2. Always pick the last element as pivot (implemented below) * 3. Pick a random element as pivot * 4. Pick median as pivot * * The key process in quickSort is partition(). Target of partition is, * given an array and an element x(say) of array as pivot, put x at it's * correct position in sorted array and put all smaller elements (samller * than x) before x, and put all greater elements (greater than x) after * x. All this should be done in linear time * */#include<cstdlib>#include<iostream>namespacesorting{/** * This function takes last element as pivot, places * the pivot element at its correct position in sorted * array, and places all smaller (smaller than pivot) * to left of pivot and all greater elements to right * of pivot * */intpartition(intarr[],intlow,inthigh){intpivot=arr[high];// taking the last element as pivotinti=(low-1);// Index of smaller elementfor(intj=low;j<high;j++){// If current element is smaller than or// equal to pivotif(arr[j]<=pivot){i++;// increment index of smaller elementinttemp=arr[i];arr[i]=arr[j];arr[j]=temp;}}inttemp=arr[i+1];arr[i+1]=arr[high];arr[high]=temp;return(i+1);}/** * The main function that implements QuickSort * arr[] --> Array to be sorted, * low --> Starting index, * high --> Ending index */voidquickSort(intarr[],intlow,inthigh){if(low<high){intp=partition(arr,low,high);quickSort(arr,low,p-1);quickSort(arr,p+1,high);}}}// namespace sortingusingsorting::quickSort;// prints the array after sortingvoidshow(intarr[],intsize){for(inti=0;i<size;i++)std::cout<<arr[i]<<" ";std::cout<<"\n";}/** Driver program to test above functions */intmain(){intsize;std::cout<<"\nEnter the number of elements : ";std::cin>>size;int*arr=newint[size];std::cout<<"\nEnter the unsorted elements : ";for(inti=0;i<size;++i){std::cout<<"\n";std::cin>>arr[i];}quickSort(arr,0,size);std::cout<<"Sorted array\n";show(arr,size);delete[]arr;return0;}
/** * @file * @brief Implementation Details * @details Quick sort 3 works on Dutch National Flag Algorithm * The major difference between simple quicksort and quick sort 3 comes in the * function partition3 In quick_sort_partition3 we divide the vector/array into * 3 parts. quick sort 3 works faster in some cases as compared to simple * quicksort. * @author immortal-j * @author [Krishna Vedala](https://github/kvedala) */#include<algorithm>#include<cassert>#include<ctime>#include<iostream>#include<vector>namespace{/** * Operator to print the array. * @param out std::ostream object to write to * @param arr array to write */template<typenameT>std::ostream&operator<<(std::ostream&out,conststd::vector<T>&arr){for(size_ti=0;i<arr.size();++i){out<<arr[i];if(i<arr.size()-1){out<<", ";}}returnout;}}// namespace/** * @namespace sorting * @brief Sorting Algorithms */namespacesorting{namespace{// using un-named namespace here to prevent partition function// being visible to end-users/** This function partitions `arr[]` in three parts * 1. \f$arr[l\ldots i]\f$ contains all elements smaller than pivot * 2. \f$arr[(i+1)\ldots (j-1)]\f$ contains all occurrences of pivot * 3. \f$arr[j\ldots r]\f$ contains all elements greater than pivot * @tparam T type of data in the vector array * @param [in,out] arr vector array being partitioned * @param [in] low lower limit of window to partition * @param [in] high upper limit of window to partition * @param [out] i updated lower limit of partition * @param [out] j updated upper limit of partition */template<typenameT>voidpartition3(std::vector<T>*arr,int32_tlow,int32_thigh,int32_t*i,int32_t*j){// To handle 2 elementsif(high-low<=1){if((*arr)[high]<(*arr)[low]){std::swap((*arr)[high],(*arr)[low]);}*i=low;*j=high;return;}int32_tmid=low;Tpivot=(*arr)[high];while(mid<=high){if((*arr)[mid]<pivot){std::swap((*arr)[low++],(*arr)[mid++]);}elseif((*arr)[mid]==pivot){mid++;}elseif((*arr)[mid]>pivot){std::swap((*arr)[mid],(*arr)[high--]);}}// update i and j*i=low-1;*j=mid;// or high-1}}// namespace/** 3-way partition based quick sort. This function accepts array pointer and * modified the input array. * @tparam T type of data in the vector array * @param [in,out] arr vector array to sort * @param [in] low lower limit of window to partition * @param [in] high upper limit of window to partition */template<typenameT>voidquicksort(std::vector<T>*arr,int32_tlow,int32_thigh){if(low>=high){// 1 or 0 elementsreturn;}int32_ti=0,j=0;// i and j are passed as referencepartition3(arr,low,high,&i,&j);// Recur two halvesquicksort(arr,low,i);quicksort(arr,j,high);}/** 3-way partition based quick sort. This function accepts array by value and * creates a copy of it. The array copy gets sorted and returned by the * function. * @tparam T type of data in the vector array * @param [in] arr vector array to sort * @param [in] low lower limit of window to partition * @param [in] high upper limit of window to partition * @returns sorted array vector */template<typenameT>std::vector<T>quicksort(std::vector<T>arr,int32_tlow,int32_thigh){if(low>=high){// 1 or 0 elementsreturnarr;}int32_ti=0,j=0;// i and j are passed as referencepartition3(&arr,low,high,&i,&j);// Recur two halvesquicksort(&arr,low,i);quicksort(&arr,j,high);returnarr;}}// namespace sorting/** Test function for integer type arrays */staticvoidtest_int(){std::cout<<"\nTesting integer type arrays\n";for(intnum_tests=1;num_tests<21;num_tests++){size_tsize=std::rand()%500;std::vector<int>arr(size);for(auto&a:arr){a=std::rand()%500-250;// random numbers between -250, 249}std::cout<<"Test "<<num_tests<<"\t Array size:"<<size<<"\t ";std::vector<int>sorted=sorting::quicksort(arr,0,size-1);if(size<20){std::cout<<"\t Sorted Array is:\n\t";std::cout<<sorted<<"\n";}assert(std::is_sorted(std::begin(sorted),std::end(sorted)));std::cout<<"\t Passed\n";}}/** Test function for double type arrays */staticvoidtest_double(){std::cout<<"\nTesting Double type arrays\n";for(intnum_tests=1;num_tests<21;num_tests++){size_tsize=std::rand()%500;std::vector<double>arr(size);for(auto&a:arr){a=double(std::rand()%500)-250.f;// random numbers between -250, 249a/=100.f;// convert to -2.5 to 2.49}std::cout<<"Test "<<num_tests<<"\t Array size:"<<size<<"\t ";std::vector<double>sorted=sorting::quicksort(arr,0,size-1);if(size<20){std::cout<<"\t Sorted Array is:\n\t";std::cout<<sorted<<"\n";}assert(std::is_sorted(std::begin(sorted),std::end(sorted)));std::cout<<"\t Passed\n";}}/** Driver program for above functions */intmain(){std::srand(std::time(nullptr));test_int();test_double();return0;}
/** * @file * @brief Implementation of the [Random Pivot Quick * Sort](https://www.sanfoundry.com/cpp-program-implement-quick-sort-using-randomisation) * algorithm. * @details * * A random pivot quick sort algorithm is pretty much same as quick * sort with a difference of having a logic of selecting next pivot element from * the input array. * * Where in quick sort is fast, but still can give you the time * complexity of O(n^2) in worst case. * * To avoid hitting the time complexity of O(n^2), we use the logic * of randomize the selection process of pivot element. * * ### Logic * * The logic is pretty simple, the only change is in the * partitioning algorithm, which is selecting the pivot element. * * Instead of selecting the last or the first element from array * for pivot we use a random index to select pivot element. * * This avoids hitting the O(n^2) time complexity in practical * use cases. * * ### Partition Logic * * Partitions are done such as numbers lower than the "pivot" * element is arranged on the left side of the "pivot", and number larger than * the "pivot" element are arranged on the right part of the array. * * ### Algorithm * * Select the pivot element randomly using getRandomIndex() function * from this namespace. * * Initialize the pInd (partition index) from the start of the * array. * * Loop through the array from start to less than end. (from start * to < end). (Inside the loop) :- * * Check if the current element (arr[i]) is less than the * pivot element in each iteration. * * If current element in the iteration is less than the * pivot element, then swap the elements at current index (i) and partition * index (pInd) and increment the partition index by one. * * At the end of the loop, swap the pivot element with partition * index element. * * Return the partition index from the function. * * @author [Nitin Sharma](https://github.com/foo290) */#include<algorithm> /// for std::is_sorted(), std::swap()#include<array> /// for std::array#include<cassert> /// for assert#include<ctime> /// for initializing random number generator#include<iostream> /// for IO operations#include<tuple> /// for returning multiple values form a function at once/** * @namespace sorting * @brief Sorting algorithms */namespacesorting{/** * @brief Functions for the [Random Pivot Quick * Sort](https://www.sanfoundry.com/cpp-program-implement-quick-sort-using-randomisation) * implementation * @namespace random_pivot_quick_sort */namespacerandom_pivot_quick_sort{/** * @brief Utility function to print the array * @tparam T size of the array * @param arr array used to print its content * @returns void * */template<size_tT>voidshowArray(std::array<int64_t,T>arr){for(int64_ti=0;i<arr.size();i++){std::cout<<arr[i]<<" ";}std::cout<<std::endl;}/** * @brief Takes the start and end indices of an array and returns a random * int64_teger between the range of those two for selecting pivot element. * * @param start The starting index. * @param end The ending index. * @returns int64_t A random number between start and end index. * */int64_tgetRandomIndex(int64_tstart,int64_tend){srand(time(nullptr));// Initialize random number generator.int64_trandomPivotIndex=start+rand()%(end-start+1);returnrandomPivotIndex;}/** * @brief A partition function which handles the partition logic of quick sort. * @tparam size size of the array to be passed as argument. * @param start The start index of the passed array * @param end The ending index of the passed array * @returns std::tuple<int64_t , std::array<int64_t , size>> A tuple of pivot * index and pivot sorted array. */template<size_tsize>std::tuple<int64_t,std::array<int64_t,size>>partition(std::array<int64_t,size>arr,int64_tstart,int64_tend){int64_tpivot=arr[end];// Randomly selected element will be here from// caller function (quickSortRP()).int64_tpInd=start;for(int64_ti=start;i<end;i++){if(arr[i]<=pivot){std::swap(arr[i],arr[pInd]);// swapping the elements from current// index to pInd.pInd++;}}std::swap(arr[pInd],arr[end]);// swapping the pivot element to its sorted positionreturnstd::make_tuple(pInd,arr);}/** * @brief Random pivot quick sort function. This function is the starting point * of the algorithm. * @tparam size size of the array to be passed as argument. * @param start The start index of the passed array * @param end The ending index of the passed array * @returns std::array<int64_t , size> A fully sorted array in ascending order. */template<size_tsize>std::array<int64_t,size>quickSortRP(std::array<int64_t,size>arr,int64_tstart,int64_tend){if(start<end){int64_trandomIndex=getRandomIndex(start,end);// switching the pivot with right most bound.std::swap(arr[end],arr[randomIndex]);int64_tpivotIndex=0;// getting pivot index and pivot sorted array.std::tie(pivotIndex,arr)=partition(arr,start,end);// Recursively callingstd::array<int64_t,arr.size()>rightSortingLeft=quickSortRP(arr,start,pivotIndex-1);std::array<int64_t,arr.size()>full_sorted=quickSortRP(rightSortingLeft,pivotIndex+1,end);arr=full_sorted;}returnarr;}/** * @brief A function utility to generate unsorted array of given size and range. * @tparam size Size of the output array. * @param from Stating of the range. * @param to Ending of the range. * @returns std::array<int64_t , size> Unsorted array of specified size. * */template<size_tsize>std::array<int64_t,size>generateUnsortedArray(int64_tfrom,int64_tto){srand(time(nullptr));std::array<int64_t,size>unsortedArray{};assert(from<to);int64_ti=0;while(i<size){int64_trandomNum=from+rand()%(to-from+1);if(randomNum){unsortedArray[i]=randomNum;i++;}}returnunsortedArray;}}// namespace random_pivot_quick_sort}// namespace sorting/** * @brief a class containing the necessary test cases */classTestCases{private:/** * @brief A function to print64_t given message on console. * @tparam T Type of the given message. * @returns void * */template<typenameT>voidlog(Tmsg){// It's just to avoid writing cout and endlstd::cout<<"[TESTS] : ---> "<<msg<<std::endl;}public:/** * @brief Executes test cases * @returns void * */voidrunTests(){log("Running Tests...");testCase_1();testCase_2();testCase_3();log("Test Cases over!");std::cout<<std::endl;}/** * @brief A test case with single input * @returns void * */voidtestCase_1(){constint64_tinputSize=1;log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~""~");log("This is test case 1 for Random Pivot Quick Sort Algorithm : ");log("Description:");log(" EDGE CASE : Only contains one element");std::array<int64_t,inputSize>unsorted_arr{2};int64_tstart=0;int64_tend=unsorted_arr.size()-1;// length - 1log("Running algorithm of data of length 50 ...");std::array<int64_t,unsorted_arr.size()>sorted_arr=sorting::random_pivot_quick_sort::quickSortRP(unsorted_arr,start,end);log("Algorithm finished!");log("Checking assert expression...");assert(std::is_sorted(sorted_arr.begin(),sorted_arr.end()));log("Assertion check passed!");log("[PASS] : TEST CASE 1 PASS!");log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~""~");}/** * @brief A test case with input array of length 500 * @returns void * */voidtestCase_2(){constint64_tinputSize=500;log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~""~");log("Description:");log(" BIG INPUT : Contains 500 elements and repeated elements");log("This is test case 2 for Random Pivot Quick Sort Algorithm : ");std::array<int64_t,inputSize>unsorted_arr=sorting::random_pivot_quick_sort::generateUnsortedArray<inputSize>(1,10000);int64_tstart=0;int64_tend=unsorted_arr.size()-1;// length - 1log("Running algorithm of data of length 500 ...");std::array<int64_t,unsorted_arr.size()>sorted_arr=sorting::random_pivot_quick_sort::quickSortRP(unsorted_arr,start,end);log("Algorithm finished!");log("Checking assert expression...");assert(std::is_sorted(sorted_arr.begin(),sorted_arr.end()));log("Assertion check passed!");log("[PASS] : TEST CASE 2 PASS!");log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~""~");}/** * @brief A test case with array of length 1000. * @returns void * */voidtestCase_3(){constint64_tinputSize=1000;log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~""~");log("This is test case 3 for Random Pivot Quick Sort Algorithm : ");log("Description:");log(" LARGE INPUT : Contains 1000 elements and repeated elements");std::array<int64_t,inputSize>unsorted_arr=sorting::random_pivot_quick_sort::generateUnsortedArray<inputSize>(1,10000);int64_tstart=0;int64_tend=unsorted_arr.size()-1;// length - 1log("Running algorithm...");std::array<int64_t,unsorted_arr.size()>sorted_arr=sorting::random_pivot_quick_sort::quickSortRP(unsorted_arr,start,end);log("Algorithm finished!");log("Checking assert expression...");assert(std::is_sorted(sorted_arr.begin(),sorted_arr.end()));log("Assertion check passed!");log("[PASS] : TEST CASE 3 PASS!");log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~""~");}};/** * @brief Self-test implementations * @returns void */staticvoidtest(){TestCasestc=TestCases();tc.runTests();}/** * @brief Main function * @param argc commandline argument count (ignored) * @param argv commandline array of arguments (ignored) * @returns 0 on exit */intmain(intargc,char*argv[]){test();// Executes various test cases.constint64_tinputSize=10;std::array<int64_t,inputSize>unsorted_array=sorting::random_pivot_quick_sort::generateUnsortedArray<inputSize>(50,1000);std::cout<<"Unsorted array is : "<<std::endl;sorting::random_pivot_quick_sort::showArray(unsorted_array);std::array<int64_t,inputSize>sorted_array=sorting::random_pivot_quick_sort::quickSortRP(unsorted_array,0,unsorted_array.size()-1);std::cout<<"Sorted array is : "<<std::endl;sorting::random_pivot_quick_sort::showArray(sorted_array);return0;}
/** * @file * @brief Algorithm of [Radix sort](https://en.wikipedia.org/wiki/Radix_sort) * @author [Suyash Jaiswal](https://github.com/Suyashjaiswal) * @details * Sort the vector of unsigned integers using radix sort i.e. sorting digit by * digit using [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) as * subroutine. Running time of radix sort is O(d*(n+b)) where b is the base for * representing numbers and d in the max digits in input integers and n is * number of unsigned integers. consider example for n = 5, aray elements = * 432,234,143,332,123 sorting digit by digit sorting according to 1) 1st digit * place * => 432, 332, 143, 123, 234 * * 2) 2nd digit place * => 123, 432, 332, 234, 143 * * 3) 3rd digit place * => 123, 143, 234, 332, 432 * * using count sort at each step, which is stable. * stable => already sorted according to previous digits. *//// header files#include<algorithm> /// for collection of functions#include<cassert> /// for a macro called assert which can be used to verify assumptions#include<iostream> /// for io operations#include<vector> /// for std::vector/** * @namespace sorting * @brief Sorting algorithms */namespacesorting{/** * @namespace radix_sort * @brief Functions for [Radix sort](https://en.wikipedia.org/wiki/Radix_sort) * algorithm */namespaceradix_sort{/** * @brief Function to sort vector according to current digit using stable * sorting. * @param cur_digit - sort according to the cur_digit * @param ar - vector to be sorted * @returns std::vector sorted till ith digit */std::vector<uint64_t>step_ith(uint16_tcur_digit,conststd::vector<uint64_t>&ar){// sorting according to current digit.intn=ar.size();std::vector<uint32_t>position(10,0);for(inti=0;i<n;++i){position[(ar[i]/cur_digit)%10]++;// counting frequency of 0-9 at cur_digit.}intcur=0;for(inti=0;i<10;++i){inta=position[i];position[i]=cur;// assingning starting position of 0-9.cur+=a;}std::vector<uint64_t>temp(n);for(inti=0;i<n;++i){temp[position[(ar[i]/cur_digit)%10]]=ar[i];// storing ar[i] in ar[i]'s cur_digit expected position of// this step.position[(ar[i]/cur_digit)%10]++;// incrementing ar[i]'s cur_digit position by 1, as// current place used by ar[i].}returntemp;}/** * @brief Function to sort vector digit by digit. * @param ar - vector to be sorted * @returns sorted vector */std::vector<uint64_t>radix(conststd::vector<uint64_t>&ar){uint64_tmax_ele=*max_element(ar.begin(),ar.end());// returns the max element.std::vector<uint64_t>temp=ar;for(inti=1;max_ele/i>0;i*=10){// loop breaks when i > max_ele because no further digits// left to makes changes in aray.temp=step_ith(i,temp);}for(uint64_ti:temp){std::cout<<i<<" ";}std::cout<<"\n";returntemp;}}// namespace radix_sort}// namespace sorting/** * @brief Function to test the above algorithm * @returns none */staticvoidtests(){/// Test 1std::vector<uint64_t>ar1={432,234,143,332,123};ar1=sorting::radix_sort::radix(ar1);assert(std::is_sorted(ar1.begin(),ar1.end()));/// Test 2std::vector<uint64_t>ar2={213,3214,123,111,112,142,133,132,32,12,113};ar2=sorting::radix_sort::radix(ar2);assert(std::is_sorted(ar2.begin(),ar2.end()));}/** * @brief Main function * @returns 0 on exit */intmain(){tests();// execute the testsreturn0;}
/** * @file * @author [Aditya Prakash](https://adityaprakash.tech) * @brief This is an implementation of a recursive version of the [Bubble sort algorithm](https://www.geeksforgeeks.org/recursive-bubble-sort/) * * @details * The working principle of the Bubble sort algorithm. * Bubble sort is a simple sorting algorithm used to rearrange a set of ascending or descending order elements. * Bubble sort gets its name from the fact that data "bubbles" to the top of the dataset. * ### Algorithm * What is Swap? * Swapping two numbers means that we interchange their values. * Often, an additional variable is required for this operation. * This is further illustrated in the following: * void swap(int x, int y){ * int z = x; * x = y; * y = z; * } * The above process is a typical displacement process. * When we assign a value to x, the old value of x is lost. * That's why we create a temporary variable z to store the initial value of x. * z is further used to assign the initial value of x to y, to complete swapping. * Recursion * While the recursive method does not necessarily have advantages over iterative * versions, but it is useful to enhance the understanding of the algorithm and * recursion itself. In Recursive Bubble sort algorithm, we firstly call the * function on the entire array, and for every subsequent function call, we exclude * the last element. This fixes the last element for that sub-array.Formally, for * `ith` iteration, we consider elements up to n-i, where n is the number of * elements in the array. Exit condition: n==1; i.e. the sub-array contains only * one element. * Complexity * Time complexity: O(n) best case; O(n²) average case; O(n²) worst case * Space complexity: O(n) * We need to traverse the array `n * (n-1)` times. However, if the entire array is * already sorted, then we need to traverse it only once. Hence, O(n) is the best case * complexity*/#include<cassert> /// for assert#include<iostream> /// for IO operations#include<vector> /// for std::vector#include<array> /// for std::array#include<algorithm> /// for std::is_sorted/** * @namespace sorting * @brief Sorting algorithms */namespacesorting{/** * @brief This is an implementation of the recursive_bubble_sort. A vector is passed * to the function which is then dereferenced, so that the changes are * reflected in the original vector. It also accepts a second parameter of * type `int` and name `n`, which is the size of the array. * * @tparam T type of data variables in the array * @param nums our array of elements. * @param n size of the array */template<typenameT>voidrecursive_bubble_sort(std::vector<T>*nums,uint64_tn){if(n==1){//!< base case; when size of the array is 1return;}for(uint64_ti=0;i<n-1;i++){//!< iterating over the entire array//!< if a larger number appears before the smaller one, swap them.if((*nums)[i]>(*nums)[i+1]){std::swap((*nums)[i],(*nums)[i+1]);}}//!< calling the function after we have fixed the last elementrecursive_bubble_sort(nums,n-1);}}// namespace sorting/** * @brief Self-test implementations * @returns void */staticvoidtest(){// 1st example. Creating an array of type `int`.std::cout<<"1st test using `int`\n";constuint64_tsize=6;std::vector<int64_t>arr;// populating the arrayarr.push_back(22);arr.push_back(46);arr.push_back(94);arr.push_back(12);arr.push_back(37);arr.push_back(63);// array populating endssorting::recursive_bubble_sort(&arr,size);assert(std::is_sorted(std::begin(arr),std::end(arr)));std::cout<<" 1st test passed!\n";// printing the arrayfor(uint64_ti=0;i<size;i++){std::cout<<arr[i]<<", ";}std::cout<<std::endl;// 2nd example. Creating an array of type `double`.std::cout<<"2nd test using doubles\n";std::vector<double>double_arr;// populating the arraydouble_arr.push_back(20.4);double_arr.push_back(62.7);double_arr.push_back(12.2);double_arr.push_back(43.6);double_arr.push_back(74.1);double_arr.push_back(57.9);// array populating endssorting::recursive_bubble_sort(&double_arr,size);assert(std::is_sorted(std::begin(double_arr),std::end(double_arr)));std::cout<<" 2nd test passed!\n";// printing the arrayfor(uint64_ti=0;i<size;i++){std::cout<<double_arr[i]<<", ";}std::cout<<std::endl;}/** * @brief Main function * @returns 0 on exit */intmain(){test();// run self-test implementationsreturn0;}
// Selection Sort#include<iostream>usingnamespacestd;intmain(){intArray[6];cout<<"\nEnter any 6 Numbers for Unsorted Array : ";// Inputfor(inti=0;i<6;i++){cin>>Array[i];}// Selection Sortingfor(inti=0;i<6;i++){intmin=i;for(intj=i+1;j<6;j++){if(Array[j]<Array[min]){min=j;// Finding the smallest number in Array}}inttemp=Array[i];Array[i]=Array[min];Array[min]=temp;}// Outputcout<<"\nSorted Array : ";for(inti=0;i<6;i++){cout<<Array[i]<<"\t";}}
#include<iostream>intmain(){intsize=10;int*array=newint[size];// Inputstd::cout<<"\nHow many numbers do want to enter in unsorted array : ";std::cin>>size;std::cout<<"\nEnter the numbers for unsorted array : ";for(inti=0;i<size;i++){std::cin>>array[i];}// Sortingfor(inti=size/2;i>0;i=i/2){for(intj=i;j<size;j++){for(intk=j-i;k>=0;k=k-i){if(array[k]<array[k+i]){break;}else{inttemp=array[k+i];array[k+i]=array[k];array[k]=temp;}}}}// Outputstd::cout<<"\nSorted array : ";for(inti=0;i<size;++i){std::cout<<array[i]<<"\t";}delete[]array;return0;}
/** * \file * \brief [Shell sort](https://en.wikipedia.org/wiki/Shell_sort) algorithm * \author [Krishna Vedala](https://github.com/kvedala) */#include<cassert>#include<cstdlib>#include<ctime>#include<iostream>#include<utility> // for std::swap#include<vector>/** pretty print array * \param[in] arr array to print * \param[in] LEN length of array to print */template<classT>voidshow_data(T*arr,size_tLEN){size_ti;for(i=0;i<LEN;i++){std::cout<<arr[i]<<", ";}std::cout<<std::endl;}/** pretty print array * \param[in] arr array to print * \param[in] N length of array to print */template<typenameT,size_tN>voidshow_data(T(&arr)[N]){show_data(arr,N);}/** \namespace sorting * \brief Sorting algorithms */namespacesorting{/** * Optimized algorithm - takes half the time by utilizing * Mar **/template<typenameT>voidshell_sort(T*arr,size_tLEN){constunsignedintgaps[]={701,301,132,57,23,10,4,1};constunsignedintgap_len=8;size_ti,j,g;for(g=0;g<gap_len;g++){unsignedintgap=gaps[g];for(i=gap;i<LEN;i++){Ttmp=arr[i];for(j=i;j>=gap&&(arr[j-gap]-tmp)>0;j-=gap){arr[j]=arr[j-gap];}arr[j]=tmp;}}}/** function overload - when input array is of a known length array type */template<typenameT,size_tN>voidshell_sort(T(&arr)[N]){shell_sort(arr,N);}/** function overload - when input array is of type std::vector, * simply send the data content and the data length to the above function. */template<typenameT>voidshell_sort(std::vector<T>*arr){shell_sort(arr->data(),arr->size());}}// namespace sortingusingsorting::shell_sort;/** * function to compare sorting using cstdlib's qsort **/template<typenameT>intcompare(constvoid*a,constvoid*b){Targ1=*static_cast<constT*>(a);Targ2=*static_cast<constT*>(b);if(arg1<arg2)return-1;if(arg1>arg2)return1;return0;// return (arg1 > arg2) - (arg1 < arg2); // possible shortcut// return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present)}/** * Test implementation of shell_sort on integer arrays by comparing results * against std::qsort. */voidtest_int(constintNUM_DATA){// int array = new int[NUM_DATA];int*data=newint[NUM_DATA];int*data2=newint[NUM_DATA];// int array2 = new int[NUM_DATA];intrange=1800;for(inti=0;i<NUM_DATA;i++)data[i]=data2[i]=(std::rand()%range)-(range>>1);/* sort using our implementation */std::clock_tstart=std::clock();shell_sort(data,NUM_DATA);std::clock_tend=std::clock();doubleelapsed_time=static_cast<double>(end-start)/CLOCKS_PER_SEC;std::cout<<"Time spent sorting using shell_sort2: "<<elapsed_time<<"s\n";/* sort using std::qsort */start=std::clock();std::qsort(data2,NUM_DATA,sizeof(data2[0]),compare<int>);end=std::clock();elapsed_time=static_cast<double>(end-start)/CLOCKS_PER_SEC;std::cout<<"Time spent sorting using std::qsort: "<<elapsed_time<<"s\n";for(inti=0;i<NUM_DATA;i++){assert(data[i]==data2[i]);// ensure that our sorting results match// the standard results}delete[]data;delete[]data2;}/** * Test implementation of shell_sort on float arrays by comparing results * against std::qsort. */voidtest_f(constintNUM_DATA){// int array = new int[NUM_DATA];float*data=newfloat[NUM_DATA];float*data2=newfloat[NUM_DATA];// int array2 = new int[NUM_DATA];intrange=1000;for(inti=0;i<NUM_DATA;i++){data[i]=data2[i]=((std::rand()%range)-(range>>1))/100.;}/* sort using our implementation */std::clock_tstart=std::clock();shell_sort(data,NUM_DATA);std::clock_tend=std::clock();doubleelapsed_time=static_cast<double>(end-start)/CLOCKS_PER_SEC;std::cout<<"Time spent sorting using shell_sort2: "<<elapsed_time<<"s\n";/* sort using std::qsort */start=std::clock();std::qsort(data2,NUM_DATA,sizeof(data2[0]),compare<float>);end=std::clock();elapsed_time=static_cast<double>(end-start)/CLOCKS_PER_SEC;std::cout<<"Time spent sorting using std::qsort: "<<elapsed_time<<"s\n";for(inti=0;i<NUM_DATA;i++){assert(data[i]==data2[i]);// ensure that our sorting results match// the standard results}delete[]data;delete[]data2;}/** Main function */intmain(intargc,char*argv[]){// initialize random number generator - once per programstd::srand(std::time(NULL));test_int(100);// test with sorting random array of 100 valuesstd::cout<<"Test 1 - 100 int values - passed. \n";test_int(1000);// test with sorting random array of 1000 valuesstd::cout<<"Test 2 - 1000 int values - passed.\n";test_int(10000);// test with sorting random array of 10000 valuesstd::cout<<"Test 3 - 10000 int values - passed.\n";test_f(100);// test with sorting random array of 100 valuesstd::cout<<"Test 1 - 100 float values - passed. \n";test_f(1000);// test with sorting random array of 1000 valuesstd::cout<<"Test 2 - 1000 float values - passed.\n";test_f(10000);// test with sorting random array of 10000 valuesstd::cout<<"Test 3 - 10000 float values - passed.\n";inti,NUM_DATA;if(argc==2)NUM_DATA=atoi(argv[1]);elseNUM_DATA=200;// int array = new int[NUM_DATA];int*data=newint[NUM_DATA];// int array2 = new int[NUM_DATA];intrange=1800;std::srand(time(NULL));for(i=0;i<NUM_DATA;i++){// allocate random numbers in the given rangedata[i]=(std::rand()%range)-(range>>1);}std::cout<<"Unsorted original data: "<<std::endl;show_data(data,NUM_DATA);std::clock_tstart=std::clock();shell_sort(data,NUM_DATA);// perform sortingstd::clock_tend=std::clock();std::cout<<std::endl<<"Data Sorted using custom implementation: "<<std::endl;show_data(data,NUM_DATA);doubleelapsed_time=(end-start)*1.f/CLOCKS_PER_SEC;std::cout<<"Time spent sorting: "<<elapsed_time<<"s\n"<<std::endl;delete[]data;return0;}
// Returns the sorted vector after performing SlowSort// It is a sorting algorithm that is of humorous nature and not useful.// It's based on the principle of multiply and surrender, a tongue-in-cheek joke// of divide and conquer. It was published in 1986 by Andrei Broder and Jorge// Stolfi in their paper Pessimal Algorithms and Simplexity Analysis. This// algorithm multiplies a single problem into multiple subproblems It is// interesting because it is provably the least efficient sorting algorithm that// can be built asymptotically, and with the restriction that such an algorithm,// while being slow, must still all the time be working towards a result.#include<iostream>voidSlowSort(inta[],inti,intj){if(i>=j)return;intm=i+(j-i)/2;// midpoint, implemented this way to avoid// overflowinttemp;SlowSort(a,i,m);SlowSort(a,m+1,j);if(a[j]<a[m]){temp=a[j];// swapping a[j] & a[m]a[j]=a[m];a[m]=temp;}SlowSort(a,i,j-1);}// Sample Main functionintmain(){intsize;std::cout<<"\nEnter the number of elements : ";std::cin>>size;int*arr=newint[size];std::cout<<"\nEnter the unsorted elements : ";for(inti=0;i<size;++i){std::cout<<"\n";std::cin>>arr[i];}SlowSort(arr,0,size);std::cout<<"Sorted array\n";for(inti=0;i<size;++i){std::cout<<arr[i]<<" ";}delete[]arr;return0;}
/** * @file strand_sort.cpp * @brief Implementation of [Strand Sort](https://en.wikipedia.org/wiki/Strand_sort) algorithm. * * @details * Strand Sort is a sorting algorithm that works in \f$O(n)\f$ time if list is already sorted and works in \f$O(n^2)\f$ in worst case. * * It is passed over the array to be sorted once and the ascending (sequential) numbers are taken. * After the first iteration, the sequential sub-array is put on the empty sorted array. * The main sequence is passed over again and a new sub-sequence is created in order. * Now that the sorted array is not empty, the newly extracted substring is merged with the sorted array. * Repeat types 3 and 4 until the sub-sequence and main sequence are empty. * * @author [Mertcan Davulcu](https://github.com/mertcandav) */#include<iostream>#include<list>/** * @namespace sorting * @brief Sorting algorithms */namespacesorting{/** * @namespace strand * @brief Functions for [Strand Sort](https://en.wikipedia.org/wiki/Strand_sort) algorithm */namespacestrand{/** * @brief Apply sorting * @tparam element type of list * @param lst List to be sorted * @returns Sorted list<T> instance */template<typenameT>std::list<T>strand_sort(std::list<T>lst){if(lst.size()<2){// Returns list if empty or contains only one elementreturnlst;// Returns list}std::list<T>result;// Define new "result" named list instance.std::list<T>sorted;// Define new "sorted" named list instance.while(!lst.empty())/* if lst is not empty */{sorted.push_back(lst.front());// Adds the first element of "lst" list to the bottom of the "sorted" list.lst.pop_front();// Remove first element of "lst" list.for(autoit=lst.begin();it!=lst.end();){// Return the loop as long as the current iterator is not equal to the last literator of the "lst" list.if(sorted.back()<=*it){// If the last reference of the "sorted" list is less than or equal to the current iterator reference.sorted.push_back(*it);// Adds the iterator retrieved in the loop under the "sorted" list.it=lst.erase(it);// Deletes the element with the current iterator and assigns the deleted element to the iterator.}else{it++;// Next iterator.}}result.merge(sorted);// Merge "result" list with "sorted" list.}returnresult;// Returns sorted list}}// namespace strand}// namespace sorting/** * @brief Function for testing * @return N/A */staticvoidtest(){std::list<int>lst={-333,525,1,0,94,52,33};std::cout<<"Before: ";for(autoitem:lst){std::cout<<item<<" ";}lst=sorting::strand::strand_sort(lst);// Sort list.std::cout<<"\nAfter: ";for(autoitem:lst){std::cout<<item<<" ";}}/** * @brief Main function * @returns 0 on exit */intmain(){test();return0;}
// C++ program to find minimum number of swaps required to sort an array#include<algorithm>#include<iostream>#include<utility>#include<vector>// Function returns the minimum number of swaps// required to sort the arrayintminSwaps(intarr[],intn){// Create an array of pairs where first// element is array element and second element// is position of first elementstd::pair<int,int>*arrPos=newstd::pair<int,int>[n];for(inti=0;i<n;i++){arrPos[i].first=arr[i];arrPos[i].second=i;}// Sort the array by array element values to// get right position of every element as second// element of pair.std::sort(arrPos,arrPos+n);// To keep track of visited elements. Initialize// all elements as not visited or false.std::vector<bool>vis(n,false);// Initialize resultintans=0;// Traverse array elementsfor(inti=0;i<n;i++){// already swapped and corrected or// already present at correct posif(vis[i]||arrPos[i].second==i)continue;// find out the number of node in// this cycle and add in ansintcycle_size=0;intj=i;while(!vis[j]){vis[j]=1;// move to next nodej=arrPos[j].second;cycle_size++;}// Update answer by adding current cycle.if(cycle_size>0){ans+=(cycle_size-1);}}delete[]arrPos;// Return resultreturnans;}// program to testintmain(){intarr[]={6,7,8,1,2,3,9,12};intn=(sizeof(arr)/sizeof(int));std::cout<<minSwaps(arr,n);return0;}
32、Tim Sort
Tim Sort是一种用于排序的高效算法,它是由Tim Peters在2002年设计的。它结合了合并排序和插入排序的思想,可以在大多数情况下快速地对任意类型的数据进行排序。
Tim Sort的主要思想是使用一个自适应的算法,在数组的各个部分使用不同的排序策略。对于小的数组,使用插入排序可以更有效地排序。对于较大的数组,合并排序更适合。
Tim Sort的实现方法是首先将数组分成大小为M的块,每个块都使用插入排序进行排序。然后将排好序的块合并为一个更大的排好序的数组。不断重复这个过程,每次将块的大小加倍,直到整个数组被完全排序为止。
Tim Sort的时间复杂度为O(n log n),空间复杂度为O(n)。在实践中,Tim Sort通常比其他排序算法更快,尤其是对于已经部分排序的数组。
// C++ program to perform TimSort.#include<algorithm>#include<iostream>constintRUN=32;// this function sorts array from left index to to right index which is of size// atmost RUNvoidinsertionSort(intarr[],intleft,intright){for(inti=left+1;i<=right;i++){inttemp=arr[i];intj=i-1;while(arr[j]>temp&&j>=left){arr[j+1]=arr[j];j--;}arr[j+1]=temp;}}// merge function merges the sorted runsvoidmerge(intarr[],intl,intm,intr){// original array is broken in two parts, left and right arrayintlen1=m-l+1,len2=r-m;int*left=newint[len1],*right=newint[len2];for(inti=0;i<len1;i++)left[i]=arr[l+i];for(inti=0;i<len2;i++)right[i]=arr[m+1+i];inti=0;intj=0;intk=l;// after comparing, we merge those two array in larger sub arraywhile(i<len1&&j<len2){if(left[i]<=right[j]){arr[k]=left[i];i++;}else{arr[k]=right[j];j++;}k++;}// copy remaining elements of left, if anywhile(i<len1){arr[k]=left[i];k++;i++;}// copy remaining element of right, if anywhile(j<len2){arr[k]=right[j];k++;j++;}delete[]left;delete[]right;}// iterative Timsort function to sort the array[0...n-1] (similar to merge sort)voidtimSort(intarr[],intn){// Sort individual subarrays of size RUNfor(inti=0;i<n;i+=RUN)insertionSort(arr,i,std::min((i+31),(n-1)));// start merging from size RUN (or 32). It will merge to form size 64, then// 128, 256 and so on ....for(intsize=RUN;size<n;size=2*size){// pick starting point of left sub array. We are going to merge// arr[left..left+size-1] and arr[left+size, left+2*size-1] After every// merge, we increase left by 2*sizefor(intleft=0;left<n;left+=2*size){// find ending point of left sub array// mid+1 is starting point of right sub arrayintmid=left+size-1;intright=std::min((left+2*size-1),(n-1));// merge sub array arr[left.....mid] & arr[mid+1....right]merge(arr,left,mid,right);}}}// utility function to print the ArrayvoidprintArray(intarr[],intn){for(inti=0;i<n;i++)printf("%d ",arr[i]);std::cout<<std::endl;}// Driver program to test above functionintmain(){intarr[]={5,21,7,23,19};intn=sizeof(arr)/sizeof(arr[0]);printf("Given Array is\n");printArray(arr,n);timSort(arr,n);printf("After Sorting Array is\n");printArray(arr,n);return0;}
/** * \addtogroup sorting Sorting Algorithms * @{ * \file * \brief [Wiggle Sort Algorithm] * (https://leetcode.com/problems/wiggle-sort-ii/) Implementation * * \author [Roshan Kanwar](http://github.com/roshan0708) * * \details * Wiggle Sort sorts the array into a wave like array. * An array ‘arr[0..n-1]’ is sorted in wave form, * if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ….. * * \example * arr = [1,1,5,6,1,4], after wiggle sort arr will become equal to [1,1,6,1,5,4] * arr = [2,8,9,1,7], after wiggle sort arr will become equal to [8,2,9,1,7] */#include<algorithm>#include<cassert>#include<ctime>#include<iostream> /// for io operations#include<vector>/** * @namespace sorting * @brief Sorting algorithms */namespacesorting{/** * @namespace wiggle_sort * @brief Functions for [Wiggle * Sort](https://leetcode.com/problems/wiggle-sort-ii/) algorithm */namespacewiggle_sort{/** * * @brief Function used for sorting the elements in wave form. * @details * Checking whether the even indexed elements are greater than * their adjacent odd elements. * Traversing all even indexed elements of the input arr. * If current element is smaller than the previous odd element, swap them. * If current element is smaller than the next odd element, swap them. * * @param arr input array (unsorted elements) * */template<typenameT>// this allows to have vectors of ints, double, float,// etcstd::vector<T>wiggleSort(conststd::vector<T>&arr){uint32_tsize=arr.size();std::vector<T>out(arr);// create a copy of input vector. this way, the original input// vector does not get modified. a sorted array is is returned.for(inti=0;i<size;i+=2){if(i>0&&out[i-1]>out[i]){std::swap(out[i],out[i-1]);// swapping the two values}if(i<size-1&&out[i]<out[i+1]){std::swap(out[i],out[i+1]);// swapping the two values}}returnout;// returns the sorted vector}}// namespace wiggle_sort}// namespace sorting/** * * @brief Utility function used for printing the elements. * Prints elements of the array after they're sorted using wiggle sort * algorithm. * * @param arr array containing the sorted elements * */template<typenameT>staticvoiddisplayElements(conststd::vector<T>&arr){uint32_tsize=arr.size();std::cout<<"Sorted elements are as follows: ";std::cout<<"[";for(inti=0;i<size;i++){std::cout<<arr[i];if(i!=size-1){std::cout<<", ";}}std::cout<<"]"<<std::endl;}/** * Test function * @returns void */staticvoidtest(){std::srand(std::time(nullptr));// initialize random number generatorstd::vector<float>data1(100);for(auto&d:data1){// generate random numbers between -5.0 and 4.99d=float(std::rand()%1000-500)/100.f;}std::vector<float>sorted=sorting::wiggle_sort::wiggleSort<float>(data1);displayElements(sorted);for(uint32_tj=0;j<data1.size();j+=2){assert(data1[j]<=data1[j+1]&&data1[j+1]>=data1[j+2]);// check the validation condition}std::cout<<"Test 1 passed\n";}/** Driver Code */intmain(){test();return0;}/** @} */