Data Structures and Algorithms: in This Session, You Will Learn To
Data Structures and Algorithms: 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
0 1 2 3 4 5 6 7
arr 28 55 46 38 16 89 83 30
Select a Pivot.
0 1 2 3 4 5 6 7
arr 28 55 46 38 16 89 83 30
Pivot
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
0 1 2 3 4 5 6 7
arr 28 55 46 38 16 89 83 30
Pivot
Greater Value Smaller Value
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
0 1 2 3 4 5 6 7
arr 28 16 46 38 55 89 83 30
Pivot
Greater Value
0 1 2 3 4 5 6 7
arr 28 16 46 38 55 89 83 30
Pivot
Greater Value
Smaller Value
0 1 2 3 4 5 6 7
arr 28 16 46 38 55 89 83 30
Pivot
Greater Value
Smaller Value
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
Swap
0 1 2 3 4 5 6 7
arr 28
16 16
28 46 38 55 89 83 30
List 1 List 2
00 1 2 3 4 5 6 7
arr 16 28 46 38 55 89 83 30
List 1 List 2
0 2 3 4 5 6 7
arr 16 46 38 55 89 83 30
List 1 List 2
0 2 3 4 5 6 7
arr 16 46 38 55 89 83 30
List 1 List 2
Select a pivot.
The pivot in this case will be arr[2], that is, 46.
0 2 3 4 5 6 7
arr 16 46 38 55 89 83 30
List 1 List 2
Pivot
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 2 3 4 5 6 7
arr 16 46 38 55 89 83 30
List 1 List 2
Pivot
Greater Value
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 2 3 4 5 6 7
arr 16 46 38 55 89 83 30
List 1 List 2
Pivot
Greater Value Smaller Value
Swap
0 2 3 4 5 6 7
arr 16 46 38 30
55 89 83 30
55
List 1 List 2
Pivot
Greater Value Smaller Value
0 2 3 4 5 6 7
arr 16 46 38 30 89 83 55
List 1 List 2
Pivot
Greater Value
0 2 3 4 5 6 7
arr 16 46 38 30 89 83 55
List 1 List 2
Pivot
Greater Value
Smaller Value
0 2 3 4 5 6 7
arr 16 46 38 30 89 83 55
List 1 List 2
Pivot
Greater Value
Smaller Value
0 1 2 3 4 5 6 7
arr 16 28 46 38 30 89 83 55
Sublist 1 Sublist 2
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
Answer:
O(n log n)
Problem Statement:
Write a program that stores 10 numbers in an array, and sorts
them by using the quick sort algorithm.
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
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
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
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
0 1 2 2 3 3 4 4 5 5 6 6
arr 53 10
10 3030 7676 3 3 5757 24 24
0 11 22 3 3 4 45 56 6
arr 53 10
1010 303030
7676 76 3 357 24
57 24
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
0 11 22 3 3 4 5
4 65 6
arr 10
53 5310 3030 76 76 3 357 57
24
57 24
0 1 22 3 3 4 4 55 6 6
arr 10 30
53 5330 7676 3 3 2457 57 57
24
00 11 22 3 44 55 6 6
arr 10
3 30
10 53
24 76
30 533 5724 7657
0 1 2 3 4 5 6
arr 3 10 24 30 53 57 76
Answer:
Quick sort
Answer:
Quick sort and merge sort are based on the divide and conquer
technique.
Linear Search:
Is the simplest searching method
Is also referred to as sequential search
Involves comparing the items sequentially with the elements in
the list
Answer:
5,000
Problem Statement:
Write a program to search a given number in an array that
contains a maximum of 20 numbers by using the linear search
algorithm. If there are more than one occurrences of the
element to be searched, then the program should display the
position of the first occurrence. The program should also
display the total number of comparisons made.
Consider an example.
You have to search for the name Steve in a telephone
directory that is sorted alphabetically.
To search the name Steve by using binary search algorithm:
– You open the telephone directory at the middle to determine which
half contains the name.
– Open that half at the middle to determine which quarter of the
directory contains the name.
– Repeat this process until the name Steve is not found.
– Binary search reduces the number of pages to be searched by
half each time.
0 1 2 3 4 5 6 7 8
arr 9 13 17 19 25 29 39 40 47
0 1 2 3 4 5 6 7 8
arr 9 13 17 19 25 29 39 40 47
0 1 2 3 4 5 6 7 8
arr 9 13 17 19 25 29 39 40 47
0 1 2 3 4 5 6 7 8
arr 9 13 17 19 25 29 39 40 47
0 1 2 3 4 5 6 7 8
arr 9 13 17 19 25 29 39 40 47
Element found
Answer:
linear
Answer:
binary
Problem Statement:
Write a program to search a number in an array that contains a
maximum of 20 elements by using binary search. Assume that
the array elements are entered in ascending order. If the
number to be searched is present at more than one location in
the array, the search should stop when one match is found.
The program should also display the total number of
comparisons made.
The best case efficiency of linear search is O(1) and the worst
case efficiency of linear search is O(n).
To apply binary search algorithm, you should ensure that the
list to be searched is sorted.
The best case efficiency of binary search is O(1) and the
worst case efficiency of binary search is O(log n).