Presentation is loading. Please wait.

Presentation is loading. Please wait.

Merge Sort: Taught By Example CO1406: Algorithms and Data Structures Module Lecturer: Dr. Nearchos Paspallis Week 10 Lab - Practice with Merge sort and.

Similar presentations


Presentation on theme: "Merge Sort: Taught By Example CO1406: Algorithms and Data Structures Module Lecturer: Dr. Nearchos Paspallis Week 10 Lab - Practice with Merge sort and."— Presentation transcript:

1 Merge Sort: Taught By Example CO1406: Algorithms and Data Structures Module Lecturer: Dr. Nearchos Paspallis Week 10 Lab - Practice with Merge sort and Quick sort 1

2 Sorting with Merge sort Sorting with merge is a procedure based on Divide and Conquer: – Recursive procedure where the problem is divided to parts that are sorted/resolved differently, and then the solutions are combined. Merge Sort Description 1.Divide: Recursively we split the array in two until we end up with arrays of size 1 – (DIVIDE) 2.Merge: Sort recursively these arrays by merging neigbouring arrays (using a temp array) – (CONQUER) 2

3 The basic idea of the Merge Sort Algorithm Divide Conquer (merge) n=7 (#elements to the left) (#elements to the right) 3

4 Merging 2 Sorted Lists Assume that you want to merge 2 sorted lists L1, L2 and create a new sorted list TEMP. Procedure 1.Place the indexes i, j at the head/start of every list. 2.Pick the smallest number from the lists and place it in the new array TEMP at position k. 3.Move the index i (if the smallest element was in list L1) or the index j (if the smallest element was in list L2). 4.Repeat steps 2-4 until all the elements of L1, L2 are placed in TEMP. (The i,j indexes begin from the start of each list) κ After 6 iterations: 4

5 Merge Sort – Interactive Demo 84843352110 848433 848 84 mid=(0+8)/2=4 low=0high=8 (0,8) (0,4) low=0 high=4 (0,2) low=0high=2 (0,1) low=0 high=2 8 (0,0) 4 (1,1) 48 8 (2,2) 48843 (3,3) 3 (4,4) 3 (3,3) 43 (4,4) 348843 void _mergesort_divide(int A[], int temp[], int low, int high){ if(low<high){ int mid = (low+high)/2; _mergesort_divide(A, temp, low, mid); _mergesort_divide(A, temp, mid+1, high); _mergesort_conquer(A, temp, low, mid, high); } 5

6 Merge Sort: Execution Example BEFORE:[8,4,8,43,3,5,2,1,10,] 0,8: [8,4,8,43,3,5,2,1,10,] 0,4: [8,4,8,43,3,] 0,2: [8,4,8,] 0,1: [8,4,] 0,0: [8,] 1,1: [4,] Merging: [A0,A0] [A1,A1] => [4,8,] 2,2: [8,] Merging: [A0,A1] [A2,A2] => [4,8,8,] 3,4: [43,3,] 3,3: [43,] 4,4: [3,] Merging: [A3,A3] [A4,A4] => [3,43,] Merging: [A0,A2] [A3,A4] => [3,4,8,8,43,] 5,8: [5,2,1,10,] 5,6: [5,2,] 5,5: [5,] 6,6: [2,] Merging: [A5,A5] [A6,A6] => [2,5,] 7,8: [1,10,] 7,7: [1,] 8,8: [10,] Merging: [A7,A7] [A8,A8] => [1,10,] Merging: [A5,A6] [A7,A8] => [1,2,5,10,] Merging: [A0,A4] [A5,A8] => [1,2,3,4,5,8,8,10,43,] AFTER:[1,2,3,4,5,8,8,10,43,] divide Index: 0 1 2 3 4 5 6 7 8 divide 6


Download ppt "Merge Sort: Taught By Example CO1406: Algorithms and Data Structures Module Lecturer: Dr. Nearchos Paspallis Week 10 Lab - Practice with Merge sort and."

Similar presentations


Ads by Google