Concepts of Algorithms
CSC-244
Unit 17 & 18
Divide-and-conquer Algorithms
Quick Sort
Shahid Iqbal Lone
Computer College
Qassim University K.S.A.
Divide and Conquer
The most well known algorithm design strategy:
1. Divide a large problem into two or more smaller
problems
2. Solve smaller problems recursively
3. Obtain solution to original (larger) problem by
combining/merging these solutions of small
problems.
CSC 244 Concepts of Algorithms
2
Quicksort Algorithm
Given an array of n elements (e.g., integers):
• 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
– Quicksort two sub-arrays
– Return results
CSC 244 Concepts of Algorithms
3
Example
We are given array of n integers to sort:
40
20
10
80
60
50
7
30 100
CSC 244 Concepts of Algorithms
4
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
CSC 244 Concepts of Algorithms
5
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.
CSC 244 Concepts of Algorithms
6
pivot_index = 0
40
20 10
80
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
30 100
J
CSC 244 Concepts of Algorithms
7
1. While data[I] <= data[pivot]
++I
pivot_index = 0
40
20 10
80
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
30 100
J
CSC 244 Concepts of Algorithms
8
1. While data[I] <= data[pivot]
++I
pivot_index = 0
40
20 10
80
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
30 100
J
CSC 244 Concepts of Algorithms
9
1. While data[I] <= data[pivot]
++I
pivot_index = 0
40
20 10
80
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
30 100
J
CSC 244 Concepts of Algorithms
10
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
pivot_index = 0
40
20 10
80
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
30 100
J
CSC 244 Concepts of Algorithms
11
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
pivot_index = 0
40
20 10
80
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
30 100
J
CSC 244 Concepts of Algorithms
12
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
pivot_index = 0
40
20 10
80
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
30 100
J
CSC 244 Concepts of Algorithms
13
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If
I<J
swap data[I] and data[J]
pivot_index = 0
40
20 10
30
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
80 100
J
CSC 244 Concepts of Algorithms
14
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
80 100
J
CSC 244 Concepts of Algorithms
15
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
80 100
J
CSC 244 Concepts of Algorithms
16
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
80 100
J
CSC 244 Concepts of Algorithms
17
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
80 100
J
CSC 244 Concepts of Algorithms
18
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
80 100
J
CSC 244 Concepts of Algorithms
19
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
60 50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
7
80 100
J
CSC 244 Concepts of Algorithms
20
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
7
50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
60
80 100
J
CSC 244 Concepts of Algorithms
21
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If
I<J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
7
50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
60
80 100
J
CSC 244 Concepts of Algorithms
22
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
7
50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
60
80 100
J
CSC 244 Concepts of Algorithms
23
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
7
50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
60
80 100
J
CSC 244 Concepts of Algorithms
24
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
7
50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
60
80 100
J
CSC 244 Concepts of Algorithms
25
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
7
50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
60
80 100
J
CSC 244 Concepts of Algorithms
26
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
7
50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
60
80 100
J
CSC 244 Concepts of Algorithms
27
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
7
50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
60
80 100
J
CSC 244 Concepts of Algorithms
28
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
pivot_index = 0
40
20 10
30
7
50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
60
80 100
J
CSC 244 Concepts of Algorithms
29
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
5. Swap data[J] and data[pivot]
pivot_index = 0
40
20 10
30
7
50
[0]
[1] [2] [3] [4] [5] [6] [7] [8]
I
60
80 100
J
CSC 244 Concepts of Algorithms
30
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
5. Swap data[J] and data[pivot_index]
pivot_index = 4
7
[0]
20 10
30
40 50
60
80 100
[1] [2] [3] [4] [5] [6] [7] [8]
I
J
CSC 244 Concepts of Algorithms
31
Partition Result
7
20
10 30
40
50
60
80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
<= data[pivot]
> data[pivot]
CSC 244 Concepts of Algorithms
32
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]
CSC 244 Concepts of Algorithms
33
Quicksort Analysis
• Assume that keys are random, uniformly
distributed.
• What is best case running time?
CSC 244 Concepts of Algorithms
34
Quicksort Analysis
•
•
Assume that keys are random, uniformly
distributed.
What is best case running time?
–
Recursion:
1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array
CSC 244 Concepts of Algorithms
35
Quicksort Analysis
•
•
Assume that keys are random, uniformly
distributed.
What is best case running time?
–
Recursion:
1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array
–
Depth of recursion tree?
CSC 244 Concepts of Algorithms
36
Quicksort Analysis
•
•
Assume that keys are random, uniformly
distributed.
What is best case running time?
–
Recursion:
1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array
–
Depth of recursion tree? O(log2n)
CSC 244 Concepts of Algorithms
37
Quicksort Analysis
•
•
Assume that keys are random, uniformly
distributed.
What is best case running time?
–
Recursion:
1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array
–
–
Depth of recursion tree? O(log2n)
Number of accesses in partition?
CSC 244 Concepts of Algorithms
38
Quicksort Analysis
•
•
Assume that keys are random, uniformly
distributed.
What is best case running time?
–
Recursion:
1. Partition splits array in two sub-arrays of size n/2
2. Quicksort each sub-array
–
–
Depth of recursion tree? O(log2n)
Number of accesses in partition? O(n)
CSC 244 Concepts of Algorithms
39
Quicksort Analysis
•
•
Assume that keys are random, uniformly
distributed.
Best case running time: O(n log2n)
CSC 244 Concepts of Algorithms
40
Quicksort Analysis
•
•
•
Assume that keys are random, uniformly
distributed.
Best case running time: O(n log2n)
Worst case running time?
CSC 244 Concepts of Algorithms
41
Quicksort: Worst Case
• Assume first element is chosen as pivot.
• Assume we get array that is already in
order:
pivot_index = 0
2
4
10
12
13
50
57
63 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
I
J
CSC 244 Concepts of Algorithms
42
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
5. Swap data[J] and data[pivot]
pivot_index = 0
2
4
[0]
10
12
13
50
57
63 100
[1] [2] [3] [4] [5] [6] [7] [8]
I
J
CSC 244 Concepts of Algorithms
43
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
5. Swap data[J] and data[pivot]
pivot_index = 0
2
[0]
4
10
12
13
50
57
63 100
[1] [2] [3] [4] [5] [6] [7] [8]
I
J
CSC 244 Concepts of Algorithms
44
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
5. Swap data[J] and data[pivot]
pivot_index = 0
2
4
[0]
10
12
13
50
57
63 100
[1] [2] [3] [4] [5] [6] [7] [8]
I
J
CSC 244 Concepts of Algorithms
45
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
5. Swap data[J] and data[pivot]
pivot_index = 0
2
4
[0]
10
12
13
50
57
63 100
[1] [2] [3] [4] [5] [6] [7] [8]
I
J
CSC 244 Concepts of Algorithms
46
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
5. Swap data[J] and data[pivot]
pivot_index = 0
2
4
[0]
10
12
13
50
57
63 100
[1] [2] [3] [4] [5] [6] [7] [8]
I
J
CSC 244 Concepts of Algorithms
47
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
5. Swap data[J] and data[pivot]
pivot_index = 0
2
4
[0]
10
12
13
50
57
63 100
[1] [2] [3] [4] [5] [6] [7] [8]
I
J
CSC 244 Concepts of Algorithms
48
1. While data[I] <= data[pivot]
++I
2. While data[J] > data[pivot]
--J
3. If I < J
swap data[I] and data[J]
4. While J > I, go to 1.
5. Swap data[J] and data[pivot]
pivot_index = 0
2
[0]
<= data[pivot]
4
10
12
13
50
57
63 100
[1] [2] [3] [4] [5] [6] [7] [8]
> data[pivot]
CSC 244 Concepts of Algorithms
49
Quicksort Analysis
•
•
•
Assume that keys are random, uniformly
distributed.
Best case running time: O(n log2n)
Worst case running time?
–
Recursion:
1.
Partition splits array in two sub-arrays:
•
•
2.
–
one sub-array of size 0
the other sub-array of size n-1
Quicksort each sub-array
Depth of recursion tree?
CSC 244 Concepts of Algorithms
50
Quicksort Analysis
•
•
•
Assume that keys are random, uniformly
distributed.
Best case running time: O(n log2n)
Worst case running time?
–
Recursion:
1.
Partition splits array in two sub-arrays:
•
•
2.
–
one sub-array of size 0
the other sub-array of size n-1
Quicksort each sub-array
Depth of recursion tree? O(n)
CSC 244 Concepts of Algorithms
51
Quicksort Analysis
•
•
•
Assume that keys are random, uniformly
distributed.
Best case running time: O(n log2n)
Worst case running time?
–
Recursion:
1.
Partition splits array in two sub-arrays:
•
•
2.
–
–
one sub-array of size 0
the other sub-array of size n-1
Quicksort each sub-array
Depth of recursion tree? O(n)
Number of accesses per partition?
CSC 244 Concepts of Algorithms
52
Quicksort Analysis
•
•
•
Assume that keys are random, uniformly
distributed.
Best case running time: O(n log2n)
Worst case running time?
–
Recursion:
1.
Partition splits array in two sub-arrays:
•
•
2.
–
–
one sub-array of size 0
the other sub-array of size n-1
Quicksort each sub-array
Depth of recursion tree? O(n)
Number of accesses per partition? O(n)
CSC 244 Concepts of Algorithms
53
Quicksort Analysis
•
•
•
Assume that keys are random, uniformly
distributed.
Best case running time: O(n log2n)
Worst case running time: O(n2)!!!
CSC 244 Concepts of Algorithms
54
Quicksort Analysis
•
•
•
•
Assume that keys are random, uniformly
distributed.
Best case running time: O(n log2n)
Worst case running time: O(n2)!!!
What can we do to avoid worst case?
CSC 244 Concepts of Algorithms
55
Improved Pivot Selection
Pick median value of three elements from data array:
data[0], data[n/2], and data[n-1].
Use this median value as pivot.
CSC 244 Concepts of Algorithms
56
C-Language Code for Quick Sort
#include <iostream.h>
void get_Values( int data[], int N)
{
cout<<"\n\t\t Put "<<N<<" random
values..\n\n";
for(int i=0;i<N;i++)
{cin>>data[i];}
}
void display( int data[], int N)
{
for(int i=0;i<N;i++)
{cout<<data[i]<<"\t";}
}
void Qsort(int data[], int left, int right)
{
int pivot, i, j;
i = left; j = right; pivot = (left + right) / 2;
do
{
while (data[i] < data[pivot]) i++;
while (data[j] > data[pivot] ) j--;
if (i <= j)
{ int temp = data[i];
data[i] = data[j];
data[j] = temp;
i++; j--;
}
}while (i <= j);
if (left < j) Qsort(data, left, j);
if (i < right) Qsort(data, i, right);
}
CSC 244 Concepts of Algorithms
57
C-Language Code for Quick Sort
int main()
{
int A[100];
int N;
cout<<"\n\n How many values you want to sort -> ";
cin>>N;
get_Values(A, N);
cout<<"Before sort:\n\n";
display(A, N);
Qsort(A,0,N-1);
cout<<"\n\nAfter sort:\n\n";
display(A, N);
return 0;
} // end of main ( ) function
CSC 244 Concepts of Algorithms
58
QUICK SORT C-Code
#include<stdio.h>
void quicksort(int [10],int,int);
int main( )
{
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
void quicksort(int x[10],int first,int last)
{ int pivot,j,temp,i;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{ while(x[i]<=x[pivot]&&i<last)
printf("Enter %d elements: ",size);
i++;
for(i=0;i<size;i++)
while(x[j]>x[pivot])
scanf("%d",&x[i]);
j--;
if(i<j)
quicksort(x,0,size-1);
{
temp=x[i]; x[i]=x[j]; x[j]=temp;
printf("Sorted elements: ");
}
for(i=0;i<size;i++)
} // closing of while
printf(" %d",x[i]);
temp=x[pivot]; x[pivot]=x[j]; x[j]=temp;
quicksort(x,first,j-1); quicksort(x,j+1,last);
CSC 244 Concepts of} Algorithms
return 0;
// closing of if
59
}
}// closing of function
END
CSC 244 Concepts of Algorithms
60
© Copyright 2026 Paperzz