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

Final Sorting

Uploaded by

samarth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Final Sorting

Uploaded by

samarth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 121

Accredited with Grade A by NAAC

Accredited with Grade A by KCG

CE261 : Data Structure and Algorithms

Topic: Sorting

Prepared By,
Dr. Nikita Bhatt
Examples: Sorting
 Sort a list of names.
 Words in a dictionary are sorted.
 Display Google PageRank Result.
 Perform binary search.
 Find duplicates in a mailing list.

2
Sorting Applications
 Commercial computing: Government organizations, financial
institutions, and commercial enterprises organize information by
performing sorting.
 Searching: Keeping data in sorted order makes it possible to
efficiently search through search algorithm.
 Operations research: Suppose that we have N jobs to complete,
where job j requires tj seconds of processing time. We need to
complete all of the jobs, but want to maximize customer satisfaction
by minimizing the average completion time of the jobs.
 Computer Graphics and computational geometry problems
 Algorithm design

3
Introduction
 Sorting: an operation that segregates items into groups according to specified
criterion.

4
Some definition
 Internal Sort:
An internal sort requires that the collection of data fit entirely in
the computer’s main memory.
 External Sort:
The data to be sorted might be stored in some external device.
 In-Place Sort:
In-place algorithm is an algorithm which transforms input with a
small, constant amount of extra storage space. The input is usually
overwritten by the output as the algorithm executes.

5
Some definition (Continue)
 Stable sort:
• An algorithm is said to be stable if it maintains the relative order
of records with equal keys.

6
Classification of Sorting Algorithms
 Computational Complexity:
Specifies behaviour of element comparisons in terms of the size of
the list (n).
 Stability:
It maintains the relative order of records with equal keys (i.e.,
values).
 Memory usage :
In particular, some sorting algorithms are "in place". Strictly, an in
place sort needs only O(1) memory beyond the items being sorted;
sometimes O(log(n)) additional memory is considered "in place".

7
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise comparisons and
swapping

1 2 3 4 5 6

77 42 35 12 101 5
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6
42 Swap42
77 35 12 101 5
77
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6

42 7735Swap35
77 12 101 5
"Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6

42 35 12 Swap12
77 77 101 5
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6

42 35 12 77 101 5

No need to swap
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6

42 35 12 77 5 Swap 101
101 5
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed


Items of Interest

• Notice that only the largest value is correctly


placed
• All other values are still out of order
• So we need to repeat this process

1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed


Repeat “Bubble Up” How Many Times?

• If we have N elements…

• And if each time we bubble an element, we place it in its


correct location…

• Then we repeat the “bubble up” process N – 1 times. That


guarantees to correctly
place all N elements.
“Bubbling” All the Elements

1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
N-1

12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
Bubble_Sort(K,N)
K: Vector K of N elements
last: position of the last unsorted element
pass: pass counter
l: used to index the vector element
exch: used to count # of exchanges made on any pass
Step 1: last  N
Step 2: Repeat through step 4 for pass  1,2 ,…N – 1
Step 3: Repeat for I=1,2,…….last – 1
If K[i]>K[i+1] then
Interchange K[i] and K[i+1]
Step 4: last last - 1
Step 5: Return
Bubble_sort(K,N) Bubble Sort Algorithm
K: Vector K of N elements, last: position of the last unsorted element , pass:
pass counter, l: used to index the vector element, exch: used to count # of
exchanges made on any pass
Step 1: last  N
Step 2: Repeat through step 5 for pass  1,2 ,…N – 1
Step 3: exch  0
Step 4: Repeat for I=1,2,…….last – 1
If K[i]>K[i+1] then
Interchange K[i] and K[i+1]
exch  exch + 1
Step 5: if exch=0 then
Return
else
last last - 1
Step 6: Return
Quiz time
1. Which number is definitely in its correct position at the end of the first pass?
The last number must be the largest.

2. How does the number of comparisons required change as the pass number
increases?
Each pass requires one fewer comparison than the last.

3. How does the algorithm know when the list is sorted?


When a pass with no exchanges occurs.

4. What is the maximum number of comparisons required for a list of 10


numbers?
9 comparisons, then 8, 7, 6, 5, 4, 3, 2, 1 so total 45

21
Self Assessment
What is the best time complexity of bubble sort?
(A) N^2
(B) NlogN
(C) N
(D) N(logN)^2

Answer: (C)

22
ISRO CS 2017 – May
The number of swappings needed to sort the numbers 8, 22, 7, 9, 31, 5, 13 in ascending order,
using bubble sort is
(A) 11
(B) 12
(C) 13
(D) 10

Answer: (D)

23
Self Assessment
 Assume that we use Bubble Sort to sort n distinct elements in ascending order. When does
the best case of Bubble Sort occur?
(A) When elements are sorted in ascending order
(B) When elements are sorted in descending order
(C) When elements are not sorted by any order
(D) There is no best case for Bubble Sort. It always takes O(n*n) time

Answer: (A)

24
Selection Sort
• Idea:
– Find the smallest element in the array
– Exchange it with the element in the first position
– Find the second smallest element and exchange it with the element
in the second position
– Continue until the array is sorted

25
Selection Sort

8 4 6 9 2 3 1 1 2 3 4 9 6 8

1 4 6 9 2 3 8 1 2 3 4 6 9 8

1 2 6 9 4 3 8 1 2 3 4 6 8 9

1 2 3 9 4 6 8 1 2 3 4 6 8 9
Selection Sort Example(ascending)

• 70 75 89 61 37 • 37 61 70 75 89
– Smallest is 37 – Smallest is 75
– Swap with index 0 – Swap with index 3
• 37 75 89 61 70 • Swap with itself

– Smallest is 61 • 37 61 70 75 89
– Swap with index 1 – Don’t need to do last element
because there’s only one left
• 37 61 89 75 70
– Smallest is 70
• 37 61 70 75 89
– Swap with index 2
Selection Sort Algorithm

Selection_Sort(K,N)
K: Vector of N elements

Step 1: Repeat through step 4 for pass=1,2,....N-1


Step 2: min_index=pass
Step 3: Repeat for I=pass+1 ,....N
If K[I] < K[min_index] then
min_index I
Step 4: If min_index ≠ pass then
swap K[pass] and K[min_index]
Step 5: Return
Selection Sort Quiz
Q-1. Maximum Comparison require for finding minimum element.
Ans. List – 1

Q-2. 8 4 6 9 2 3 1 position of element 6 after 3rd pass


Ans. 5Th
Selection Sort Quiz

Q-3. What is the number of swaps required to sort n elements using


selection sort, in the worst case?
(A) O(n) (B) O(n log n) (C) O(n^2 ) (D) O(n^2 log n)
Answer (A)
Here is Selection Sort algorithm for sorting in ascending order.
1. Find the minimum value in the list
2. Swap it with the value in the first position
3. Repeat the steps above for the remainder of the list (starting at the
second position and advancing each time) As we can see from the algorithm,
selection sort performs swap only after finding the appropriate position of
the current picked element. So there are O(n) swaps performed in selection
sort.
Self Assessment

Q-4. Calculate total number of comparisons and exchanges


required to arrange following data into ascending order using
Selection and Bubble sort.
99, 56, 12, 16, 98, 34, 22, 10, 0, 10
Selection sort : Total comparisons: 45
Total Exchanges: 8
Bubble sort : Total comparisons: 45
Total Exchanges:36
Selection Sort is not stable sorting algorithm.

32
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left
if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 2.78 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71

Iteration 0: step 0.

33
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 2.78 7.42 0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71

Iteration 1: step 0.

34
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 2.78 0.56
7.42 7.42
0.56 1.12 1.17 0.32 6.21 4.42 3.14 7.71

Iteration 2: step 0.

35
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56
2.78 2.78
0.56 7.42 1.12 1.17 0.32 6.21 4.42 3.14 7.71

Iteration 2: step 1.

36
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left
if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 2.78 7.42 1.12 1.17 0.32 6.21 4.42 3.14 7.71

Iteration 2: step 2.

37
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 2.78 1.12
7.42 7.42
1.12 1.17 0.32 6.21 4.42 3.14 7.71

Iteration 3: step 0.

38
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12
2.78 2.78
1.12 7.42 1.17 0.32 6.21 4.42 3.14 7.71

Iteration 3: step 1.

39
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 2.78 7.42 1.17 0.32 6.21 4.42 3.14 7.71

Iteration 3: step 2.

40
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 2.78 1.17
7.42 7.42
1.17 0.32 6.21 4.42 3.14 7.71

Iteration 4: step 0.

41
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 1.17
2.78 2.78
1.17 7.42 0.32 6.21 4.42 3.14 7.71

Iteration 4: step 1.

42
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 1.17 2.78 7.42 0.32 6.21 4.42 3.14 7.71

Iteration 4: step 2.

43
Insertion Sort
• Iteration i. Repeatedly swap element i with the one to its left
if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 1.17 2.78 0.32
7.42 7.42
0.32 6.21 4.42 3.14 7.71

Iteration 5: step 0.

44
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 1.17 0.32
2.78 2.78
0.32 7.42 6.21 4.42 3.14 7.71

Iteration 5: step 1.

45
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 1.12 0.32
1.17 1.17
0.32 2.78 7.42 6.21 4.42 3.14 7.71

Iteration 5: step 2.

46
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.56 0.32
1.12 1.12
0.32 1.17 2.78 7.42 6.21 4.42 3.14 7.71

Iteration 5: step 3.

47
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32
0.56 0.56
0.32 1.12 1.17 2.78 7.42 6.21 4.42 3.14 7.71

Iteration 5: step 4.

48
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 7.42 6.21 4.42 3.14 7.71

Iteration 5: step 5.

49
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 6.21
7.42 7.42
6.21 4.42 3.14 7.71

Iteration 6: step 0.

50
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 6.21 7.42 4.42 3.14 7.71

Iteration 6: step 1.

51
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 6.21 4.42
7.42 7.42
4.42 3.14 7.71

Iteration 7: step 0.

52
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 4.42
6.21 6.21
4.42 7.42 3.14 7.71

Iteration 7: step 1.

53
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21 7.42 3.14 7.71

Iteration 7: step 2.

54
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 4.42 6.21 3.14
7.42 7.42
3.14 7.71

Iteration 8: step 0.

55
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 4.42 3.14
6.21 6.21
3.14 7.42 7.71

Iteration 8: step 1.

56
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 3.14
4.42 4.42
3.14 6.21 7.42 7.71

Iteration 8: step 2.

57
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71

Iteration 8: step 3.

58
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71

Iteration 9: step 0.

59
Insertion Sort

• Iteration i. Repeatedly swap element i with the one to its left


if smaller.

• Property. After ith iteration, a[0] through a[i] contain first


i+1 elements in ascending order.

Array index 0 1 2 3 4 5 6 7 8 9
Value 0.32 0.56 1.12 1.17 2.78 3.14 4.42 6.21 7.42 7.71

Iteration 10: DONE.

60
Insertion Sort
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}

61
Self Assessment-II
Consider the following lists of partially sorted numbers. The double bars represent the sort
marker. For each list state how many comparisons and swaps are needed to sort the next
number.
(a) [1 3 4 8 9 || 5 2] (b)[1 3 4 5 8 9 || 2]
(c) [1 3 || 4 9 2 8 ] (d) [1 2 3 8 || 7 6]
(e) [2 3 4 6 7 8 9 || 1]

62
Divide and Conquer
 Divide
If the problem size is small, solve this problem directly; otherwise, split the original problem
into sub-problems with equal sizes.
 Conquer
Recursively solve all sub problem(s)
 Combine
Take the solutions to the sub problems and combine these solutions into a solution for the
original problem

63
Quicksort
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
64
Quicksort Algorithm
Quicksort(A, p, r)
if p < r then
q⟵ partition (A, p, r)
Quicksort(A,p,q-1)
Quicksort(A,q+1,r)

65
Quicksort Algorithm (continue)

66
Quicksort Algorithm (continue)

67
68
Self Assessment
Suppose we are sorting an array of eight integers using
quick sort, and we have just finished the first partitioning
with the array looking like this: 2 5 1 7 9 12 11 10. Which
statement is correct?
(a)The pivot could be either the 7 or the 9.
(b)The pivot could be the 7, but it is not the 9
(c)The pivot is not the 7, but it could be the 9
(d)Neither the 7 nor the 9 is the pivot.

69
Gate 2014
Let P be a QuickSort Program to sort numbers in ascending order using the
first element as pivot. Let t1 and t2 be the number of comparisons made by
P for the inputs {1, 2, 3, 4, 5} and {4, 1, 5, 3, 2} respectively. Which one of the
following holds?

(A) t1 = 5
(B) t1 < t2
(C) t1 > t2
(D) t1 = t2

Answer: (C)

70
Merge Sort: Idea
Given a list L with a length k:
 If k == 1  the list is sorted
 Else:
• Merge Sort the left side (1 to k/2)
• Merge Sort the right side (k/2 +1 to k)
• Merge the right side with the left side

71
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

23 98 14 45

14 23 45 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6 67

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98
98 23 45 14 6 67 33 42

6 14 23 33 42 45 67 98
Merge Sort

113
Merge Sort (Continue)
Merge-Sort(A, p, r)
if p < r then
q¬(p+r)/2
Merge-Sort(A, p, q)
Merge-Sort(A, q+1, r)
Merge(A, p, q, r)

Merge(A, p, q, r)
Take the smallest of the two topmost elements of sequences
A[p..q] and A[q+1..r] and put into the resulting sequence.
Repeat this, until both sequences are empty. Copy the
resulting sequence into A[p..r].
114
GATE CSE 2019
 An array of 25 distinct elements is to be sorted using quicksort. Assume that the pivot
element is chosen uniformly at random. The probability that the pivot element gets placed
in the worst possible location in the first round of partitioning (rounded off to 2 decimal
places) is ______.

 Answer
Correct answer is 0.08 to 0.08
 Explanation
Given an array of 25 distinct elements, and pivot element is chosen uniformly randomly. So,
there are only 2 worst case position in the pivot element is either first (or) last.

∴ Probability of placing pivot element in worst possible location = 2/25 = 0.08

115
GATE CSE 2016
 Assume that the algorithms considered here sort the input sequences in ascending order. If
the input is already in ascending order, which of the following are TRUE?
I. Quicksort runs in Θ(n2) time
II. Bubblesort runs in Θ(n2) time
III. Mergesort runs in Θ(n) time
IV. Insertion sort runs in Θ(n) time
(a) I and II only
(b) I and III only
(c) II and IV only
(d) I and IV only

Answer: (d)

116
GATE-CS-2016
The worst case running times of Insertion sort, Merge sort and Quick sort, respectively, are:
(A) Θ(n log n), Θ(n log n) and Θ(n2)
(B) Θ(n2), Θ(n2) and Θ(n Log n)
(C) Θ(n2), Θ(n log n) and Θ(n log n)
(D) Θ(n2), Θ(n log n) and Θ(n2)

Answer: (D)

117
GATE-CS-2015
An unordered list contains n distinct elements. The number of comparisons to find an element
in this list that is neither maximum nor minimum is
(A) Θ(nlogn)
(B) Θ(n)
(C) Θ(logn)
(D) Θ(1)

Answer: (D)

118
GATE-CS-2015 (Continue)
 Explanation: We only need to consider any 3 elements and compare them. So the number
of comparisons is constants, that makes time complexity as Θ(1)
 The catch here is, we need to return any element that is neither maximum not minimum.
 Let us take an array {10, 20, 15, 7, 90}. Output can be 10 or 15 or 20
 Pick any three elements from given liar. Let the three elements be 10, 20 and 7.
 Using 3 comparisons, we can find that the middle element is 10.

119
Gate 2007
Which of the following sorting algorithms has the lowest worst-case complexity?
a)Merge sort
b)Bubble sort
c)Quicksort
d)Selection sort

Answer: (a)

120
GATE CS 1998

121
GATE CS 1998 (Continue)
(A) a
(B) b
(C) c
(D) d

 Answer: (B)

122

You might also like