{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lect22 - DS CS 11002 9 Computer Sc Engg IIT Kharagpur 6 1...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 & $ % Internal Sort by Comparison II Lect 22 Goutam Biswas
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 & $ % Merge Sort Algorithm This internal sorting algorithm by comparison was invented by John von Neumann in 1945 (Wikipedia). Its running time is O ( n log n ) , better than the worst case running times of selection, insertion or bubble sort algorithms. This is stable i.e. the order of the equal input elements are preserved after sorting. It uses divide and conquer algorithmic strategy. It uses O ( n ) extra space. Lect 22 Goutam Biswas
Image of page 2
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3 & $ % Merge Sort Algorithm (Ascending Order) Let there there be n data elements stored in an 1-D array. 1. If n = 1 , it is already sorted! 2. If n > 1 , (a) Split the data set in the middle i.e. find out the mid = low + high 2 . (b) Merge Sort recursively the lower half of the data (within low and mid ). Lect 22 Goutam Biswas
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4 & $ % (c) Merge Sort recursively the upper half of the data (within mid + 1 and high ). (d) Copy both the sorted halves in a second array and merge them in proper order in the original array. Lect 22 Goutam Biswas
Image of page 4
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5 & $ % Merge Sort : An Example Lect 22 Goutam Biswas
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6 & $ % # # # # # # # # # # # # x x x midIndex highIndex lowIndex lowIndex midIndex highIndex lowIndex midIndex highIndex 21 3 13 9 11 5 9 39 213 13 9 11 5 9 39 0 1 2 3 4 5 6 7 0 0 2 2 3 3 4 4 5 5 6 6 7 7 1 1 21 3 13 9 11 5 9 39 Unsorted Array Call 0 Call 1 Call 2 Lect 22 Goutam Biswas
Image of page 6
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7 & $ % # # # # 2 3 4 5 6 7 21 3 13 9 11 5 9 39 0 1 x sorted # # # # 2 3 4 5 6 7 21 3 13 9 11 5 9 39 0 1 x # # # # 2 3 4 5 6 7 13 9 11 5 9 39 0 1 x temp 213 sorted 3 21 copy to both sorted Merge in Call 2 Call 3 Call 4 lowIndex = highIndex lowIndex = highIndex Lect 22 Goutam Biswas
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8 & $ % # # # # 2 3 4 5 6 7 13 9 11 5 9 39 0 1 x sorted 3 21 lowIndex highIndex midIndex Call 5 # # # # 2 3 4 5 6 7 13 9 11 5 9 39 0 1 x 3 21 Call 6 sorted # # # # 2 3 4 5 6 7 13 9 11 5 9 39 0 1 x 3 21 Call 7 sorted sorted sorted sorted lowIndex = highIndex lowIndex = highIndex Lect 22 Goutam Biswas
Image of page 8
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 9 & $ % # # # # 2 3 4 5 6 7 11 5 9 39 0 1 x 3 21 temp 13 9 sorted sorted 9 13 # # # # 2 3 4 5 6 7 11 5 9 39 0 1 x 3 3 2113 9 temp 9 13 21 sorted # # # # 2 3 4 5 6 7 0 1 x 3 9 13 21 sorted 5 9 11 39 sorted Merge in Call 5 Merge in Call 1 Before the Merge of Call 0 Lect 22 Goutam Biswas
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10 & $ % # # # # 2 3 4 5 6 7 0 1 x 3 temp 9 13 213911 9 5 3 5 9 9 1113 2139 sorted After the Merge of Call 0 Lect 22 Goutam Biswas
Image of page 10
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 11 & $ % C Code #include <stdio.h> #define MAXNO 100 void mergeSort(int [], int, int); int main() // mergeSort.c { int n=0, i, data[MAXNO] ; printf("Enter the data, terminate by Ctrl+D: ") ; while(scanf("%d", &data[n]) != EOF) ++n ; printf("\nInpute data: ") ; for(i=0; i<n; ++i) printf("%d ", data[i]) ; putchar(’\n’) ; Lect 22 Goutam Biswas
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}