One more day with Recursion

One more day with Recursion

Hello! everyone. I hope you are all doing well. So, today again I'll be presenting myself with our ongoing topic i.e. Recursion. Today, I'll be covering two types of sorting using the concept of recursion. Quick Sort and Merge Sort are the topics that we're going to talk about in today's blog.

Quick Sort = In this type of sorting, we assume the first element of the data structure as the pivot element and this pivot element is compared with the rest of the data members of the data structure and arranged in such a way that left sub-array to the pivot element consists of smaller data members as compared to pivot element. Similarly, larger data members form the right sub-array concerning the pivot element.

#include<bits/stdc++.h>
using namespace std;

int partition(int arr[], int start, int size){
    int pivot = arr[start], count = 0;

    for(int i = start+1; i <= size; i++){
        if(arr[i] <= pivot)
        {
            count++;
        }
    }

    int pivotIndex = start + count;
    swap(arr[pivotIndex], arr[start]);

    int i = start, j = size; 
    while (i < pivotIndex && j > pivotIndex)            
    {
        while(arr[i] < pivot)    
        {
            i++;
        }
        while (arr[j] > pivot)
        {
            j--;
        }
        if(arr[i] < pivotIndex && arr[j] > pivotIndex){
            swap(arr[i], arr[j]);
            i++;
            j--;
        }
    }
    return pivotIndex;  
}

void QuickSort(int arr[], int start, int size){
    if(start >= size){
        return;
    } 
    //partition karre
    int p = partition(arr, start, size);

    //left sub-array solved
    QuickSort(arr, start, p-1);

    //right sub-array solved
    QuickSort(arr, p+1, size);
}

int main(){
    int n;
    cout << "Enter the size of array : ";
    cin >> n;

    int arr[n];
    cout << "Enter the elements of array : ";
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }

    QuickSort(arr, 0, n-1);

    cout << "Array after sorting...\n";
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    return 0;
}
  • Merge Sort = In this type of sorting, we keep dividing the data structure until it's separated from one another and after that it's merged either in ascending or descending order.

      #include<bits/stdc++.h>
      using namespace std;
    
      void Merge(int arr[], int start, int size){
          int mid = (start+size)/2;
    
          int length_arr1 = mid - start + 1;
          int length_arr2 = size - mid;
    
          int *first = new int[length_arr1];
          int *second = new int[length_arr2];
    
          int mainArrayIndex = start;
          for (int i = 0; i < length_arr1; i++)
          {
              first[i] = arr[mainArrayIndex++];
          }
    
          mainArrayIndex = mid+1;
          for (int i = 0; i < length_arr1; i++)
          {
              second[i] = arr[mainArrayIndex++];
          }    
    
          int index1 = 0, index2 = 0;
    
          mainArrayIndex = start;
    
          while (index1 < length_arr1 && index2 < length_arr2)
          {
              if(first[index1] < second[index2]){
                  arr[mainArrayIndex++] = first[index1++];
              }
              else{
                  arr[mainArrayIndex++] = second[index2++];
              }
          }
    
          while (index1 < length_arr1)
          {
              arr[mainArrayIndex++] = first[index1++];
          }
          while (index2 < length_arr2)
          {
              arr[mainArrayIndex++] = second[index2++];
          }
    
      }
    
      void MergeSort(int arr[], int start, int size){
          if (start >= size)
          {
              return;
          }
          int mid = (start+size)/2;
    
          //left part sort karna hai
          MergeSort(arr, start, mid-1);
    
          //right part solve karna hai
          MergeSort(arr, mid+1, size);
    
          Merge(arr, start, size);
      }
    
      int main(){
          int n;
          cout << "Enter the size of array : ";
          cin >> n;
    
          int arr[n];
          cout << "Enter the elements of array : ";
          for (int i = 0; i < n; i++)
          {
              cin >> arr[i];
          }
    
          MergeSort(arr, 0, n-1);
    
          cout << "Array after sorting...\n";
          for (int i = 0; i < n; i++)
          {
              cout << arr[i] << " ";
          }
          return 0;
      }