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

13 Quick Sorti

Quicksort is a divide and conquer algorithm that works by first selecting a pivot element and then partitioning the array around that pivot. The array is partitioned such that elements less than the pivot come before elements greater than the pivot. This process is then recursively applied to both sub-arrays until the entire array is sorted. The key steps are selecting a pivot, partitioning the array around that pivot, and then recursively applying the process to both sub-arrays.

Uploaded by

Aman Singh
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)
45 views

13 Quick Sorti

Quicksort is a divide and conquer algorithm that works by first selecting a pivot element and then partitioning the array around that pivot. The array is partitioned such that elements less than the pivot come before elements greater than the pivot. This process is then recursively applied to both sub-arrays until the entire array is sorted. The key steps are selecting a pivot, partitioning the array around that pivot, and then recursively applying the process to both sub-arrays.

Uploaded by

Aman Singh
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/ 42

Data Structures & Algorithms

Topic: Quick Sort


By

Dr. Prakash Singh Tanwar

Associate Professor

Lovely Professional University, Punjab

By:Dr. Prakash Singh Tanwar


Quick Sort
Given an arr of n elements (e.g., integers):
1. If arr only contains one element, return

2. Else
a) pick one element to use as pivot.

b) Partition elements into two sub-arrs:


i. Elements less than or equal to pivot

ii. Elements greater than pivot

c) Quick sort two sub-arrs

d) Return results

By:Dr. Prakash Singh Tanwar


Quick Sort
QUICK(A, N, BEG, END, LOC)
1. [Initialize] Set LEFT = BEG, RIGHT=END and LOC=BEG.
2, [Scan from right to left]
(a) Repeat while A[LOC] <= A[RIGHT] and LOC ≠ RIGHT
RIGHT = RIGHT-1
[End of Loop]
(b) If LOC = RIGHT, then: Return.
(c) If A[LOC] > A[RIGHT], Then
(i) [ Interchange A[LOC] and A[RIGHT] ]
Set: TEMP = A[LOC], A[LOC] = A[RIGHT], A[RIGHT] = TEMP.
(ii) Set: LOC = RIGHT.
(iii) Go to Step 3.
3. [Scan from left to right]
(a) Repeat while A[LOC] >= A[LEFT] and LOC ≠ LEFT
LEFT = LEFT+1.
[End of Loop]
(b) If LOC = LEFT, then: Return.
(c) If A[LOC] < A[LEFT], Then
(i) [ Interchange A[LOC] and A[LEFT] ]
Set: TEMP = A[LOC], A[LOC] = A[LEFT], A[LEFT] = TEMP.
(ii) Set: LOC = LEFT.
(iii) Go to Step 2.
[End of IF structure]

By:Dr. Prakash Singh Tanwar


Quick Sort
QUICK SORT(A, N, LOWER, UPPER, TOP)
1. [Initialize] TOP = NULL.
2. [PUSH boundary values of A on to Stack when N>1 elements]
If N > 1, Then: TOP = TOP + 1, LOWER[1] = 1, UPPER[1] = N.
3. Repeat Step 4 to 7 while TOP ≠ NULL.
4. [POP sublist from Stack]
Set: BEG = LOWER[TOP], END = UPPER[TOP], TOP = TOP-1.
5. Call QUICK(A, N, BEG, END, LOC)
6. [PUSH left sublist on to Stack]
If BEG < LOC-1, Then:
TOP = TOP+1, LOWER[TOP] = BEG, UPPER[TOP] = LOC-1.
7. [PUSH right sublist on to Stack]
If END > LOC+1, Then:
TOP = TOP+1, LOWER[TOP] = LOC+1, UPPER[TOP] = END.
8. EXIT

By:Dr. Prakash Singh Tanwar


Quick Sort
Given

40 20 10 80 60 50 7 30 100

By:Dr. Prakash Singh Tanwar


Quick Sort
Pick the pivot point
There are a number of ways to pick the pivot element.

In this example, we are using the first element in the arr:

40 20 10 80 60 50 7 30 100

By:Dr. Prakash Singh Tanwar


Partitioning arr
Given a pivot, partition the elements of the arr such that
the resulting arr consists of:
1. One sub-arr that contains elements >= pivot

2. Another sub-arr that contains elements < pivot

The sub-arrs are stored in the original arr arr.

Partitioning loops through, swapping elements


below/above pivot.
Quick Sort
(Partitioning)

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap arr[too_small_index] and arr[pivot_index]

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

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

too_big_index too_small_index
Quick Sort
(Partitioning)
1. While arr[too_big_index] <= arr[pivot]
too_big_index++
2.While arr[too_small_index] > arr[pivot]
too_small_index--
3. If too_big_index < too_small_index
swap arr[too_big_index] and arr[too_small_index]
4. While too_small_index > too_big_index, go to 1.
5. Swap arr[too_small_index] and arr[pivot_index]

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

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

too_big_index too_small_index
Quick Sort
(Partition Result)

7 20 10 30 40 50 60 80 100

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

<= arr[pivot] > arr[pivot]


Quicksort
(Recursion: Sub-arrs)

7 20 10 30 40 50 60 80 100

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

<= arr[pivot] > arr[pivot]


Quick Sort
(Partitioning)
int pivotIndex = start ; 1. While arr[too_big_index] <= arr[pivot]
int i = start+1, j = end;
do too_big_index++
{ 2.While arr[too_small_index] > arr[pivot]
//Step 1 too_small_index--
while (arr[i] <= arr[pivotIndex])
{ 3. If too_big_index < too_small_index
i++; swap arr[too_big_index] and arr[too_small_index]
} 4. While too_small_index > too_big_index, go to 1.
//Step 2
while (arr[j] > arr[pivotIndex]) 5. Swap arr[too_small_index] and arr[pivot_index]
{
j--;
} 7 20 10 30 40 50 60 80 100
//Step 3
if (i < j )
{
swap(arr[i], arr[j]);
[0] [1] [2] [3] [4] [5] [6] [7] [8]
}
//Step 4.
}while(j>i); pivot_index too_big_index too_small_index
//Step 5.
swap(arr[pivotIndex], arr[j]); =4 Or Or
//Step 6 update pivotIndex i j
pivotindex=j;
Quick Sort
(Partitioning)
int partition(int arr[], int start, int end) void quickSort(int arr[], int left, int right)
{ {
int pivotIndex = start ;
int i = start+1, j = end;
// Terminating Condition
do if (left >= right)
{ return;
while (arr[i] <= arr[pivotIndex]) // partitioning the array
{ int p = partition(arr, left, right);
i++; // Sorting the left part
}
while (arr[j] > arr[pivotIndex])
quickSort(arr, left, p - 1);
{ // Sorting the right part
j--; quickSort(arr, p + 1, right);
} }
if (i < j )
{ int main()
swap(arr[i], arr[j]); void display(int *arr,int n) {
} { int arr[] = { 40, 20, 10, 80, 60, 50, 7, 30, 100 };
}while(j>i); for (int i = 0; i < n; i++) int n = 9;
swap(arr[pivotIndex], arr[j]); { display(arr,n);
pivotindex=j; cout <<"\t"<< arr[i] << " "; quickSort(arr, 0, n - 1);
return pivotIndex; } display(arr,n);
} cout <<endl; return 0;
} }
Quicksort
Assume that keys are random, uniformly distributed.

What is best case running time?

Recursion:
1. Partition splits arr in two sub-arrs of size n/2
2. Quicksort each sub-arr

Depth of Recursion:
Ω(log(n))

Number of accesses in partition?


Ω(n)

QuickSort
Ω(nlog(n))
Quicksort
Assume that keys are random, uniformly distributed.

What is best case running time?

QuickSort
Ω(nlog(n))
Quicksort
Assume that keys are random, uniformly distributed.

What is worst case running time?

QuickSort
cn+c(n−1)+c(n−2)+⋯+2c
=c(n+(n−1)+(n−2)+⋯+2)
=c((n+1)(n/2)−1) .

O(n^2)
Quicksort
Assume that keys are random, uniformly distributed.

What is worst case running time?

QuickSort
cn+c(n−1)+c(n−2)+⋯+2c
=c(n+(n−1)+(n−2)+⋯+2)
=c((n+1)(n/2)−1) .

O(n^2)
By:Dr. Prakash Singh Tanwar
Thank You

By:Dr. Prakash Singh Tanwar

You might also like