0% found this document useful (0 votes)
3 views

Quick sort

Quick sort is a fast sorting algorithm that uses a divide and conquer strategy to sort a list by partitioning it around a pivot element. The process involves recursively sorting elements smaller than the pivot to its left and those greater than or equal to it to its right. A step-by-step example of the algorithm is provided, along with a C program implementation.

Uploaded by

abhinavsrivas3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Quick sort

Quick sort is a fast sorting algorithm that uses a divide and conquer strategy to sort a list by partitioning it around a pivot element. The process involves recursively sorting elements smaller than the pivot to its left and those greater than or equal to it to its right. A step-by-step example of the algorithm is provided, along with a C program implementation.

Uploaded by

abhinavsrivas3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Quick Sort

Definition
• Quick sort is a fast sorting algorithm used to sort a list of elements.
• The quick sort algorithm attempts to separate the list of elements into
two parts and then sort each part recursively. That means it use divide
and conquer strategy.
In quick sort, the partition of the list is performed based on the
element called pivot. Here pivot element is one of the elements in the
list.
• The list is divided into two partitions such that "all elements to the
left of pivot are smaller than the pivot and all elements to the right of
pivot are greater than or equal to the pivot".
Step by Step Process

• In Quick sort algorithm, partitioning of the list is performed using following


steps...
• Step 1 - Consider the first element of the list as pivot (i.e., Element at first position
in the list).
• Step 2 - Define two variables i and j. Set i and j to first and last elements of the list
respectively.
• Step 3 - Increment i until list[i] > pivot then stop.
• Step 4 - Decrement j until list[j] < pivot then stop.
• Step 5 - If i < j then exchange list[i] and list[j].
• Step 6 - Repeat steps 3,4 & 5 until i > j.
• Step 7 - Exchange the pivot element with list[j] element.
Example
• #include<stdio.h>

Program • void quicksort(int number[25],int first,int last){


• int i, j, pivot, temp;

• if(first<last){
• pivot=first;
• i=first;
• j=last;

• while(i<j){
• while(number[i]<=number[pivot]&&i<last)
• i++;
• while(number[j]>number[pivot])
• j--;
• if(i<j){
• temp=number[i];
• number[i]=number[j];
• quicksort(number,first,j-1);
• quicksort(number,j+1,last);

• }
• }

• int main(){
• int i, count, number[25];

• printf("How many elements are u going to enter?: ");
• scanf("%d",&count);

• printf("Enter %d elements: ", count);
• for(i=0;i<count;i++)
• scanf("%d",&number[i]);

• quicksort(number,0,count-1);

Thankyou
Made By :- AKTU WALA ( Satyam Sahu )

You might also like