Professional Documents
Culture Documents
In This Session, You Will Learn To:: Objectives
In This Session, You Will Learn To:: Objectives
0 1 2 3 4 5 6 7
arr 28 55 46 38 16 89 83 30
Implementing Quick Sort Algorithm (Contd.)
0 1 2 3 4 5 6 7
arr 28 55 46 38 16 89 83 30
Implementing Quick Sort Algorithm (Contd.)
Select a Pivot.
0 1 2 3 4 5 6 7
arr 28 55 46 38 16 89 83 30
Pivot
Implementing Quick Sort Algorithm (Contd.)
Start from the left end of the list (at index 1).
Move in the left to right direction.
Search for the first element that is greater than the pivot
value.
Greater element
0 1 2 3 4 5 6 7
arr 28 55 46 38 16 89 83 30
Pivot
Greater Value
Implementing Quick Sort Algorithm (Contd.)
0 1 2 3 4 5 6 7
arr 28 55 46 38 16 89 83 30
Pivot
Greater Value Smaller Value
Implementing Quick Sort Algorithm (Contd.)
Swap
0 1 2 3 4 5 6 7
arr 28 16
55 46 38 16
55 89 83 30
Pivot
Greater Value Smaller Value
Implementing Quick Sort Algorithm (Contd.)
0 1 2 3 4 5 6 7
arr 28 16 46 38 55 89 83 30
Pivot
Greater Value
Implementing Quick Sort Algorithm (Contd.)
0 1 2 3 4 5 6 7
arr 28 16 46 38 55 89 83 30
Pivot
Greater Value
Smaller Value
Implementing Quick Sort Algorithm (Contd.)
0 1 2 3 4 5 6 7
arr 28 16 46 38 55 89 83 30
Pivot
Greater Value
Smaller Value
Implementing Quick Sort Algorithm (Contd.)
0 0 1 1 2 3 4 2 53 6 4 7 5 6 7
arr 2828 16 16 46 38 55 4689 3883 5530 89 83 30
List 1 List 2
Pivot
Implementing Quick Sort Algorithm (Contd.)
Swap
0 1 2 3 4 5 6 7
arr 28
16 16
28 46 38 55 89 83 30
List 1 List 2
Implementing Quick Sort Algorithm (Contd.)
0 1 2 3 4 5 6 7
arr 16 28 46 38 55 89 83 30
List 1 List 2
Implementing Quick Sort Algorithm (Contd.)
0 1 2 3 4 5 6 7
arr 16 28 46 38 55 89 83 30
List 1 List 2
Implementing Quick Sort Algorithm (Contd.)
0 1 2 3 4 5 6 7
arr 16 28 46 38 55 89 83 30
List 1 List 2
Implementing Quick Sort Algorithm (Contd.)
Select a pivot.
The pivot in this case will be arr[2], that is, 46.
0 1 2 3 4 5 6 7
arr 16 28 46 38 55 89 83 30
List 1 List 2
Pivot
Implementing Quick Sort Algorithm (Contd.)
Start from the left end of the list (at index 3).
Move in the left to right direction.
Search for the first element that is greater than the pivot
value.
Greater element
0 1 2 3 4 5 6 7
arr 16 28 46 38 55 89 83 30
List 1 List 2
Pivot
Greater Value
Implementing Quick Sort Algorithm (Contd.)
Start from the right end of the list (at index 7).
Move in the right to left direction.
Search for the first element that is smaller than or equal to
the pivot value.
Smaller element
0 1 2 3 4 5 6 7
arr 16 28 46 38 55 89 83 30
List 1 List 2
Pivot
Greater Value Smaller Value
Implementing Quick Sort Algorithm (Contd.)
Swap
0 1 2 3 4 5 6 7
arr 16 28 46 38 30
55 89 83 30
55
List 1 List 2
Pivot
Greater Value Smaller Value
Implementing Quick Sort Algorithm (Contd.)
0 1 2 3 4 5 6 7
arr 16 28 46 38 30 89 83 55
List 1 List 2
Pivot
Greater Value
Implementing Quick Sort Algorithm (Contd.)
0 1 2 3 4 5 6 7
arr 16 28 46 38 30 89 83 55
List 1 List 2
Pivot
Greater Value
Smaller Value
Implementing Quick Sort Algorithm (Contd.)
0 1 2 3 4 5 6 7
arr 16 28 46 38 30 89 83 55
List 1 List 2
Pivot
Greater Value
Smaller Value
Implementing Quick Sort Algorithm (Contd.)
0 1 2 3 4 5 6 7
arr 16 28 46 38 30 89 83 55
Implementing Quick Sort Algorithm (Contd.)
Sublist 1 Sublist 2
Implementing Quick Sort Algorithm (Contd.)
// element greater
than
// pivot
6. Increment i by 1
7. Repeat step 8 until j < low or arr[j] < pivot // Search for an
element
10. If i <= j:
a. Go to step 5 // Continue the search
11. If low < j:
a. Swap arr[low] with arr[j] // Swap pivot with last element in
// first
part of the list
12. QuickSort(low, j – 1) // Apply quicksort on list left to pivot
13. QuickSort(j + 1, high) // Apply quicksort on list right to pivot
Determining the Efficiency of Quick Sort Algorithm
Answer:
O(n log n)
Activity: Sorting Data by Using Quick Sort Algorithm
Problem Statement:
Write a program that stores 10 numbers in an array, and sorts
them by using the quick sort algorithm.
Sorting Data by Using Merge Sort
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)
The first step to sort data by using merge sort is to split the
list into two parts.
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)
The first step to sort data by using merge sort is to split the
list into two parts.
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)
0 1 2 2 3 3 4 4 5 5 6 6
arr 53 10
10 3030 7676 3 3 5757 24 24
Implementing Merge Sort Algorithm (Contd.)
0 11 22 3 3 4 45 56 6
arr 53 10
1010 303030
7676 76 3 357 24
57 24
Implementing Merge Sort Algorithm (Contd.)
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)
0 11 22 3 3 4 5
4 65 6
arr 10
53 5310 3030 76 76 3 357 57
24
57 24
Implementing Merge Sort Algorithm (Contd.)
0 1 22 3 3 4 4 55 6 6
arr 10 30
53 5330 7676 3 3 2457 57 57
24
Implementing Merge Sort Algorithm (Contd.)
00 11 22 3 44 55 6 6
arr 10
3 30
10 53
24 76
30 533 5724 7657
Implementing Merge Sort Algorithm (Contd.)
0 1 2 3 4 5 6
arr 3 10 24 30 53 57 76
Implementing Merge Sort Algorithm (Contd.)
// two sublists.
Implementing Merge Sort Algorithm (Contd.)
Answer:
Quick sort
Just a minute
Answer:
Quick sort and merge sort are based on the divide and conquer
technique.
Summary