Prerequisites:Insertion Sort, Quick Sort, Selection Sort In this article, a Hybrid algorithm with the combination of quick sort and insertion sort is implemented. As the name suggests, the Hybrid algorithm combines more than one algorithm. Why Hybrid algorithm: Quicksort algorithm is efficient if the size of the input is very large. But, insertion sort is more efficient than quick sort in case of small arrays as the number of comparisons and swaps are less compared to quicksort. So we combine the two algorithms to sort efficiently using both approaches. Note:Selectionsort algorithm can also be used to combine with quicksort. Though the time complexity is of O(N2), these algorithms prove to be efficient in this case because these are used only when the size of the array is less than a threshold value(10 in this article).
Approach: The idea is to use recursion and continuously find the size of the array. If the size is greater than the threshold value(10), then the quicksort function is called for that portion of the array. Else, insertion sort is called. Below is the implementation of the Hybrid algorithm:
C++
// C++ implementation of the above approach #include<bits/stdc++.h>usingnamespacestd;// Function to perform the insertion sortvoidinsertion_sort(intarr[],intlow,intn){for(inti=low+1;i<n+1;i++){intval=arr[i];intj=i;while(j>low&&arr[j-1]>val){arr[j]=arr[j-1];j-=1;}arr[j]=val;}}//The following two functions are used // to perform quicksort on the array. // Partition function for quicksort intpartition(intarr[],intlow,inthigh){intpivot=arr[high];inti,j;i=low;j=low;for(inti=low;i<high;i++){if(arr[i]<pivot){inttemp=arr[i];arr[i]=arr[j];arr[j]=temp;j+=1;}}inttemp=arr[j];arr[j]=arr[high];arr[high]=temp;returnj;}// Function to call the partition function // and perform quick sort on the array voidquick_sort(intarr[],intlow,inthigh){if(low<high){intpivot=partition(arr,low,high);quick_sort(arr,low,pivot-1);quick_sort(arr,pivot+1,high);}}// Hybrid function -> Quick + Insertion sort voidhybrid_quick_sort(intarr[],intlow,inthigh){while(low<high){// If the size of the array is less // than threshold apply insertion sort // and stop recursion if(high-low+1<10){insertion_sort(arr,low,high);break;}else{intpivot=partition(arr,low,high);// Optimised quicksort which works on // the smaller arrays first // If the left side of the pivot // is less than right, sort left part // and move to the right part of the array if(pivot-low<high-pivot){hybrid_quick_sort(arr,low,pivot-1);low=pivot+1;}else{// If the right side of pivot is less // than left, sort right side and // move to the left side hybrid_quick_sort(arr,pivot+1,high);high=pivot-1;}}}}// Driver Codeintmain(){intarr[21]={24,97,40,67,88,85,15,66,53,44,26,48,16,52,45,23,90,18,49,80,23};hybrid_quick_sort(arr,0,20);for(inti=0;i<21;i++)cout<<arr[i]<<", ";}// This code is contributed by ishayadav2918
Java
/*package whatever //do not write package name here */importjava.io.*;classGFG{privatestaticvoidinsertionSort(inta[],intlow,inthigh){for(inti=low+1;i<=high;i++){for(intj=i-1;j>=low;j--){if(a[j]>a[j+1]){// Swapinttemp=a[j];a[j]=a[j+1];a[j+1]=temp;}elsebreak;}}}privatestaticintpartition(intarr[],intlow,inthigh){intpivot=arr[high];inti=low;intj=low;while(i<=high){if(arr[i]>pivot)i++;else{inttemp=arr[i];arr[i]=arr[j];arr[j]=temp;i++;j++;}}returnj-1;}publicstaticvoidhybridQuickSort(intarr[],intlow,inthigh){while(low<high){// Check if array size on which we will be working is less than 10if(high-low<10){insertionSort(arr,low,high);break;}else{intpivot=partition(arr,low,high);// We will do recursion on small size// subarray So we can check pivot - low and// pivot - highif(pivot-low<pivot-high){hybridQuickSort(arr,low,pivot-1);low=pivot+1;}else{hybridQuickSort(arr,pivot+1,high);high=pivot-1;}}}}// Driver codepublicstaticvoidmain(String[]args){intarr[]={24,97,40,67,88,85,15,66,53,44,26,48,16,52,45,23,90,18,49,80,23};hybridQuickSort(arr,0,arr.length-1);for(inti:arr)System.out.print(i+" ");}}// This code is contribute by @mahi_07
Python3
# Python implementation of the above approach# Function to perform the insertion sortdefinsertion_sort(arr,low,n):foriinrange(low+1,n+1):val=arr[i]j=iwhilej>lowandarr[j-1]>val:arr[j]=arr[j-1]j-=1arr[j]=val# The following two functions are used # to perform quicksort on the array. # Partition function for quicksortdefpartition(arr,low,high):pivot=arr[high]i=j=lowforiinrange(low,high):ifarr[i]<pivot:a[i],a[j]=a[j],a[i]j+=1a[j],a[high]=a[high],a[j]returnj# Function to call the partition function # and perform quick sort on the arraydefquick_sort(arr,low,high):iflow<high:pivot=partition(arr,low,high)quick_sort(arr,low,pivot-1)quick_sort(arr,pivot+1,high)returnarr# Hybrid function -> Quick + Insertion sortdefhybrid_quick_sort(arr,low,high):whilelow<high:# If the size of the array is less # than threshold apply insertion sort # and stop recursionifhigh-low+1<10:insertion_sort(arr,low,high)breakelse:pivot=partition(arr,low,high)# Optimised quicksort which works on # the smaller arrays first# If the left side of the pivot # is less than right, sort left part# and move to the right part of the arrayifpivot-low<high-pivot:hybrid_quick_sort(arr,low,pivot-1)low=pivot+1else:# If the right side of pivot is less # than left, sort right side and # move to the left sidehybrid_quick_sort(arr,pivot+1,high)high=pivot-1# Driver codea=[24,97,40,67,88,85,15,66,53,44,26,48,16,52,45,23,90,18,49,80,23]hybrid_quick_sort(a,0,20)print(a)
C#
// C# implementation of QuickSortusingSystem;classGFG{// Function to perform the insertion sortstaticvoidinsertion_sort(int[]arr,intlow,intn){for(inti=low+1;i<n+1;i++){intval=arr[i];intj=i;while(j>low&&arr[j-1]>val){arr[j]=arr[j-1];j-=1;}arr[j]=val;}}// The following two function are used to // perform the quick sort on the array// Partition function for quicksortstaticintpartition(int[]arr,intlow,inthigh){// pivotintpivot=arr[high];inti;intj;i=low;j=low;for(i=low;i<high;i++){if(arr[i]<pivot){inttemp=arr[i];arr[i]=arr[j];arr[j]=temp;j+=1;}}inttemp2=arr[j];arr[j]=arr[high];arr[high]=temp2;returnj;}// Function to call the partition function // amd perform quick sort on the arraystaticvoidquickSort(int[]arr,intlow,inthigh){if(low<high){intpivot=partition(arr,low,high);quickSort(arr,low,pivot-1);quickSort(arr,pivot+1,high);}}// Hybrid function -> Quick + Insertion Sort staticvoidhybrid_quick_sort(int[]arr,intlow,inthigh){while(low<high){// If the size of the array is less// than threshold apply insertion sort // and stop recursionif(high+low+1<10){insertion_sort(arr,low,high);break;}else{intpivot=partition(arr,low,high);// Optimized quickSort which works on // the smaller arrays first// If the left side of the pivot // is less than right, sort left part // and move to the right part of the arrayif(pivot-low<high-pivot){hybrid_quick_sort(arr,low,pivot-1);low=pivot-1;}else{// If the right side of pivot is less// than left, sort right side and // move to the left sidehybrid_quick_sort(arr,pivot+1,high);high=pivot-1;}}}}// Function to print an arraystaticvoidprintArray(int[]arr,intsize){for(inti=0;i<size;i++)Console.Write(arr[i]+", ");Console.WriteLine();}// Driver CodepublicstaticvoidMain(){int[]arr={24,97,40,67,88,85,15,66,53,44,26,48,16,52,45,23,90,18,49,80,23};intn=arr.Length;hybrid_quick_sort(arr,0,n-1);printArray(arr,n);}}// This code is contributed by Aditya Sharma