Program to Implement Recursive Quick Sort where the First Element is taken as Pivot - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Program to Implement Recursive Quick Sort where the First Element is taken as Pivot

Share This
The quicksort method is highly efficient in sorting huge datasets. It is a member of the divide-and-conquer category, which involves breaking down the sorting problem into smaller, more easily handled subproblems. Let us explore its fundamental concept and comprehend its significance.

Implementation of the Divide and Conquer Strategy:


  1. Select a Pivot: The procedure commences by choosing an element from the array as the pivot. This element serves as a point of reference for the purpose of sorting. 
  2. Partitioning: Following that is the enchantment. The quicksort algorithm reorganizes the items of an array based on a selected pivot. Elements that are less than the pivot are placed on the left side, while elements that are larger are positioned on the right side. This method guarantees that the pivot ultimately reaches its final sorted location.
  3. Achieve Recursive Dominance: Now, we encounter the exquisite nature of the divide-and-conquer approach. The quicksort algorithm iteratively sorts the two subarrays, which consist of elements smaller and larger than the pivot, independently. This recursive process continues until each subarray contains only one element, which is intrinsically sorted by definition.

Significance of Quick Sort:


The key advantage of quick sort is its average time complexity of O(n log n), which makes it considerably faster than bubble sort or selection sort with a time complexity of O(n^2). This results in tangible advantages in practical applications, particularly when handling extensive datasets. The efficiency of quick sort is particularly evident when dealing with huge arrays that are mostly sorted, as it requires fewer comparisons and can perform quite well in such cases.
 
Nevertheless, it is crucial to acknowledge that the efficiency of quick sort might be influenced by the selection of the pivot. An inappropriately selected pivot can result in a worst-case temporal complexity of O(n^2). However, Quick Sort continues to be a very adaptable and effective sorting algorithm, which is widely used in numerous computing applications.
Source Code
#include<stdio.h> #include<stdlib.h> void swap(int *list, int left, int right) { int temp; temp = list[left]; list[left] = list[right]; list[right] = temp; } void quicksort(int *list, int low, int high) { int pivot, i; if(high>low) { pivot=partition(list, low, high); /*when pivot is correctly placed at the position this means left subarray elements are less than pivot and right subarray elements are greater than pivot*/ quicksort(list, low, pivot - 1); /*divide and conquer repeatedly until all the elements are arranged*/ quicksort(list, pivot + 1, high); } } int partition(int *list, int low, int high) { int left, right; int pv_item; int pivot = left = low; pv_item = list[low]; /*first positional element is taken as pivot*/ right = high; while(left<right) { while(list[left]<=pv_item) /*left pointer will move towards right unless it finds an element which is greater or equal to pivot.Take that position*/ left++; while(list[right]>pv_item) /*right pointer will move towards left unless it finds an element which is less than pivot.Take that position*/ right--; if(left<right) swap(list, left, right); /*postion of left pointer and position of right pointer will be swapped*/ } /*if it is found that right pointer positional index is lower than right pointer positional index then swap pivot with right pointer's position*/ list[low] = list[right]; list[right] = pv_item; return right; } int main() { int *list, i, n; printf("\nenter the no of elements:"); scanf("%d",&n); list = (int*) malloc(sizeof(int) * n); printf("\nenter the values:"); for(i=0;i<n;i++) scanf("%d",&list[i]); quicksort(list, 0, n - 1); for(i=0;i<n;i++) printf("%d ",list[i]); return 0; }

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.