Quick Sort ( C & Python 3 )


How Quick Sort algorithm works

Quick Sort Algorithm

Quick Sort is also based on the concept of Divide and Conquer, just like merge sort. But in quick sort all the heavy lifting(major work) is done while dividing the array into subarrays, while in case of merge sort, all the real work happens during merging the subarrays. In case of quick sort, the combine step does absolutely nothing.
It is also called partition-exchange sort. This algorithm divides the list into three main parts:
  1. Elements less than the Pivot element
  2. Pivot element(Central element)
  3. Elements greater than the pivot element
Pivot element can be any element from the array, it can be the first element, the last element or any random element. In this tutorial, we will take the rightmost element or the last element as pivot.
For example: In the array {52, 37, 63, 14, 17, 8, 6, 25}, we take 25 as pivot. So after the first pass, the list will be changed like this.{6 8 17 14 25 63 37 52}. Hence after the first pass, pivot will be set at its position, with all the elements smaller to it on its left and all the elements larger than to its right. Now 6 8 17 14 and 63 37 52 are considered as two separate sunarrays, and same recursive logic will be applied on them, and we will keep doing this until the complete array is sorted.

How Quick Sorting Works?

Following are the steps involved in quick sort algorithm:
  1. After selecting an element as pivot, which is the last index of the array in our case, we divide the array for the first time.
  2. In quick sort, we call this partitioning. It is not simple breaking down of array into 2 subarrays, but in case of partitioning, the array elements are so positioned that all the elements smaller than the pivot will be on the left side of the pivot and all the elements greater than the pivot will be on the right side of it.
  3. And the pivot element will be at its final sorted position.
  4. The elements to the left and right, may not be sorted.
  5. Then we pick subarrays, elements on the left of pivot and elements on the right of pivot, and we perform partitioning on them by choosing a pivot in the subarrays.
Let's consider an array with values {9, 7, 5, 11, 12, 2, 14, 3, 10, 6}
Below, we have a pictorial representation of how quick sort will sort the given array.
In step 1, we select the last element as the pivot, which is 6 in this case, and call for partitioning, hence re-arranging the array in such a way that 6 will be placed in its final position and to its left will be all the elements less than it and to its right, we will have all the elements greater than it.
Then we pick the subarray on the left and the subarray on the right and select a pivot for them, in the above diagram, we chose 3 as pivot for the left subarray and 11 as pivot for the right subarray.
And we again call for partitioning.

Implementing Quick Sort in C

/*
Quick Sorting in Asceding in Order in C
@uther Abhishek 
*/

#include <stdio.h>

void quicksort(int* ,int ,int );
int main()
{
    int n,i;
    printf("\nEnter the number of items in the array  : ");
    scanf("%d",&n);
    
    int array[n];
    for(i=0;i<n;i++)
    {
        printf("\nEnter element %d : ",i+1);
        scanf("%d",&array[i]);
    }
    
    quicksort(array,0,n-1);
    
    for(i=0;i<n;i++)
        printf(" %d",array[i]);
    
    return 0;
}

void quicksort(int *array,int start,int end)
{
    int i,j,pivot,swapper;
    
    if(start<end)
    {
        pivot=start;
        i=start;
        j=end;
        
        while(i<j)
       {
          while(array[i]<=array[pivot] && i<end)
            i++;  
          while(array[j]>array[pivot])
            j--;  
          if(i<j)
          {
            swapper=array[i];
            array[i]=array[j];
            array[j]=swapper;
          }
        }   
      swapper=array[pivot];
      array[pivot]=array[j];
      array[j]=swapper;
      quicksort(array,start,j-1);
      quicksort(array,j+1,end);
    }
}

Output of the C program

Enter the number of items in the array  : 3                                                    
                                                                                               
Enter element 1 : 2                                                                            
                                                                                               
Enter element 2 : 3                                                                            
                                                                                               
Enter element 3 : 1                                                                            
 1 2 3     
  Implementing Quick Sort in Python 3
"""
Quint Sort to arrange the elements in the ascending order
Created on Wed May  6 11:07:36 2020
@author: Abhishek
"""

def quick_sort(array,first,last):
    if(first<last):
        pivot=first
        i=first
        j=last
        while(i<j):
            while(array[i]<=array[pivot] and i<last):
                i=i+1
            while(array[j]>array[pivot]):
                j=j-1
            if(i<j):
                array[i],array[j]=array[j],array[i]
        array[j],array[pivot]=array[pivot],array[j]
        quick_sort(array,first,j-1)
        quick_sort(array,j+1,last)
    
    

array=list(map(int,input().rstrip().split()))
first=0
quick_sort(array,0,len(array)-1)
print(array)

Output of the Python 3 Program



10 32 17 19

[10, 17, 19, 32]




Comments

  1. Thank you sir it's very helpful for the poor students like us...

    ReplyDelete

Post a Comment

Popular posts from this blog

Bubble Sort ( C & Python 3)

Comparison Logical and Bitwise Operator ( Java Part - 4 )

Something about me