Docsity

Sorting Algorithms: Analysis and Comparison of Insertion Sort and Merge Sort - Prof. R. Sh, Study notes of Computer Science

Partial preview of the text

Download Sorting Algorithms: Analysis and Comparison of Insertion Sort and Merge Sort - Prof. R. Sh and more Study notes Computer Science in PDF only on Docsity!

1

CSC 3102

Adv Data Structures &

Algorithm Analysis

Lecture 2: Sorting

2

Previously,

  • (^) We saw that if an array is sorted,

we can search faster using a

binary search

  • (^) Binary Search (A, i, j, x)
    • (^) k = midpoint of range A[i..j]
    • (^) If A[k] >= x, search in A[1..k]
    • (^) Else search in A[k+1,j]
  • (^) But this needs us to sort the array

4

  • (^) Study a few simple algorithms for

sorting

  • (^) Insertion Sort
  • (^) Selection Sort
  • (^) Merge Sort
  • (^) Show why these algorithms are correct
  • (^) Try to analyze the efficiency of these

algorithms (how fast they run)

About this lecture

5

Input: A list of n numbers

Output: Arrange the numbers in

increasing order

Remark: Sorting has many applications.

E.g., if the list is already sorted, we can

search a number in the list faster

The Sorting Problem

7

Insertion Sort

  • (^) (it 1) 8,9,4,3,6 -> 8, 9, 4, 3, 6
  • (^) (it 2)-> 8, 4, 9, 3, 6 -> 4, 8, 9, 3, 6
  • (^) (it 3)-> 4,8,3,9,6 -> 4,3,8,9,6-
  • (^) (it 4)-> 3, 4, 8, 6, 9 -> 3, 4, 6, 8, 9

8

Correctness

  • (^) If array length is 1 this is true.
  • (^) Now, assume that before kth^ round A[1..k] is sorted, then we can say that after kth^ round A[1..k+1] will be sorted (this is known as Induction step)
  • (^) The above is true because we take A[k+1]th item and insert it in its right place. All the items previously in A[1..k] remain in the same order.
  • (^) So at the end of n-1 steps we get correctly sorted array

10

  • (^) Divide a big problem into smaller problems

 solve smaller problems separately

 combine the results to solve original one

  • (^) This idea is called Divide-and-Conquer
  • (^) Smart idea to solve complex problems (why?)
  • (^) Can we apply this idea for sorting?

Divide and Conquer

11

  • (^) What is a smaller problem?  E.g., sorting fewer numbers  Let’s divide the list to two shorter lists
  • (^) Next, solve smaller problems (how?)
  • (^) Finally, combine the results  “merging” two sorted lists into a single sorted list (how?)

Divide-and-Conquer for

Sorting

13 Merge Sort 13

Merge-Sort Tree

  • (^) An execution of merge-sort is depicted by a binary tree - each node represents a recursive call of merge-sort and stores - unsorted sequence before the execution and its partition - (^) sorted sequence at the end of the execution - (^) the root is the initial call - (^) the leaves are calls on subsequences of size 0 or 1 7 2  9 4  2 4 7 9 7  2  2 7 9  4  4 9 7  7 2  2 9  9 4  4

14 Merge Sort 14

Execution Example

  • (^) Partition 7 2 9 4  2 4 7 9 3 8 6 1  1 3 8 6 7 2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1

16 Merge Sort 16

Execution Example (cont.)

  • (^) Recursive call, partition 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1

17 Merge Sort 17

Execution Example (cont.)

  • (^) Recursive call, base case 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1

19 Merge Sort 19

Execution Example (cont.)

  • (^) Merge 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1

20 Merge Sort 20

Execution Example (cont.)

  • (^) Recursive call, …, base case, merge 7 2  9 4  2 4 7 9 3 8 6 1  1 3 8 6 7  2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 3  3 8  8 6  6 1  1