Skip to main content

Posts

Showing posts with the label Quick sort algorithm to select first/last element as pivot

Quick Sort algorithm to select random/middle/first/last element as a pivot

Below i have attached quick sort algorithm that can select pivot element position from the user for the first time and sort the array with  nlogn complexity. Approx. in all the books ,algorithm shows to select  either first or last element as a pivot.This algorithm is able to select pivot position from user and sort the array elements accordingly. Lets take an example: 95 5 1 3 7 try a quick sort dry run of this array elements on pen and paper,here 1 is a pivot element:- 1st pass:-1 5 95 3 7   (pivot element 1 is in final position) 2nd pass:-1 3 5 95 7  (now pivot element 5 is in final position) 3rd pass:-1 3 5 7 95   (pivot element 95 is in final position) Just with the slight modification in the main & quick sort function , you can able to select middle element as a pivot element.To make a middle element as a pivot element,you only need to do three changes : 1.)Remove below two lines in the main function    printf("Enter pivot elemen...