0% found this document useful (0 votes)
1 views37 pages

Quicksort Algorithm

The document outlines the Quicksort algorithm, which sorts an array by selecting a pivot and partitioning the array into elements less than or equal to the pivot and elements greater than the pivot. It explains the process of selecting a pivot, partitioning the array, and recursively applying the Quicksort on the resulting sub-arrays. An example is provided to illustrate the steps involved in the Quicksort process.

Uploaded by

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

Quicksort Algorithm

The document outlines the Quicksort algorithm, which sorts an array by selecting a pivot and partitioning the array into elements less than or equal to the pivot and elements greater than the pivot. It explains the process of selecting a pivot, partitioning the array, and recursively applying the Quicksort on the resulting sub-arrays. An example is provided to illustrate the steps involved in the Quicksort process.

Uploaded by

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

Unit 4 – L11

Quick Sort

Dr Ashok Kumar Sahoo


Quicksort Algorithm
Given an array of n elements:
• If array only contains one element, return
• Else
– pick one element to use as pivot.
– Partition elements into two sub-arrays:
• Elements less than or equal to pivot
• Elements greater than pivot
– Recursively apply Quicksort on two sub-arrays
– Return results
Quicksort: Example

We are given array of n integers to sort:


40 20 10 80 60 50 7 30 100
Pick Pivot Element
There are a number of ways to pick the pivot element. In
this example, we will use the first element in the array:

40 20 10 80 60 50 7 30 100
Partitioning Array
• Given a pivot, partition the elements of the array
such that the resulting array consists of:
1. One sub-array that contains elements >= pivot
2. Another sub-array that contains elements < pivot
• The sub-arrays are stored in the original data
array.
• Partitioning loops through, swapping elements
below/above pivot.
Quicksort: Example

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]

pivot_index = 0 40 20 10 80 60 50 7 30 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 60 50 7 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.
5. Swap data[right] and data[pivot_index]

pivot_index = 0 40 20 10 30 7 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

right left
Quicksort: Example
1. While data[left] <= data[pivot]
++left
2. While data[right] > data[pivot]
--right
3. If left < right
swap data[left] and data[right]
4. While right> left, go to 1.
5. Swap data[right] and data[pivot_index]

pivot_index = 4 7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

left right
Quicksort: Example

7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]


Recursion: Quicksort Sub-arrays

7 20 10 30 40 50 60 80 100

[0] [1] [2] [3] [4] [5] [6] [7] [8]

<= data[pivot] > data[pivot]


Quicksort Function
void quicksort(int x[], int left, int right)
{
int pos;
if(left<right)
{
pos=partition(x, left, right);
quicksort(x,left,pos-1);
quicksort(x,pos+1,right);
}
}
Quicksort ‘Partition’ Function
int partition(int x[],int left,int right){
int pivot,j,temp,i;
if(left<right){
pivot=left;
i=left;
j=right;
while(i<j){
while(x[i]<=x[pivot]&&i<right)i++;
while(x[j]>x[pivot])j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
}
return j;
}
Quicksort ‘main’ Function
#include<stdio.h>
void quicksort(int x[],int,int);
int partition(int x[], int, int);
void main(){
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
}
Quicksort - Complexity

T (n) T (k )  T (n  k  1)  n)
• For average case, we can assume that k≈n/2.
• The complexity of quick-sort now reduces to

T (n) 2T (n / 2)  n)


T (n) n log n)
END

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy