Download presentation
Presentation is loading. Please wait.
1
Lecture No.45 Data Structures Dr. Sohail Aslam
2
Divide and Conquer What if we split the list into two parts? 10 10 12
8 4 2 11 7 5 12 8 4 2 11 7 5
3
Divide and Conquer Sort the two parts: 4 8 10 12 2 5 7 11 10 12 8 4 2
4
Divide and Conquer Then merge the two parts together: 2 4 5 7 8 10 11
12 4 8 10 12 2 5 7 11 End of lecture 44.
5
Analysis To sort the halves (n/2)2+(n/2)2
To merge the two halves n So, for n=100, divide and conquer takes: = (100/2)2 + (100/2) = = (n2 = 10,000) Start of lecture 45
6
Divide and Conquer Why not divide the halves in half?
The quarters in half? And so on . . . When should we stop? At n = 1
7
Divide and Conquer Recall: Binary Search Search Search Search
8
Divide and Conquer Sort Sort Sort Sort Sort Sort Sort
9
Divide and Conquer Combine Combine Combine
10
Mergesort Mergesort is a divide and conquer algorithm that does exactly that. It splits the list in half Mergesorts the two halves Then merges the two sorted halves together Mergesort can be implemented recursively
11
Mergesort The mergesort algorithm involves three steps:
If the number of items to sort is 0 or 1, return Recursively sort the first and second halves separately Merge the two sorted halves into a sorted group
12
Merging: animation 4 8 10 12 2 5 7 11 2
13
Merging: animation 4 8 10 12 2 5 7 11 2 4
14
Merging: animation 4 8 10 12 2 5 7 11 2 4 5
15
Merging 4 8 10 12 2 5 7 11 2 4 5 7
16
Mergesort Split the list in half. Mergesort the left half. 8 12 11 2 7
5 4 10 Split the list in half. Mergesort the left half. 8 12 4 10 Split the list in half. Mergesort the left half. 4 10 Mergesort the right. 10 4
17
Mergesort the right half.
8 12 11 2 7 5 4 10 10 4 8 12 Mergesort the right half. Merge the two halves. 8 12 10 4 4 10 8 12 Merge the two halves. 8 12
18
Mergesort the right half.
8 12 11 2 7 5 4 10 Merge the two halves. 10 12 8 4 10 4 8 12 Mergesort the right half. Merge the two halves. 10 4 4 10 10 4 8 12 8 12
19
Mergesort the right half.
4 8 10 12 11 2 7 5 11 2 7 5 11 2 11 2
20
Mergesort the right half.
4 8 10 12 11 2 7 5 2 11 11 2 7 5 2 11 2 11
21
Mergesort the right half.
4 8 10 12 11 2 7 5 2 11 2 11 7 5 5 7 7 5
22
Mergesort the right half.
4 8 10 12 11 2 7 5 2 11 11 2 7 5
23
Mergesort the right half.
4 8 10 12 2 5 7 11
24
Mergesort Merge the two halves. 2 4 5 7 8 10 11 12
25
Mergesort void mergeSort(float array[], int size) {
int* tmpArrayPtr = new int[size]; if (tmpArrayPtr != NULL) mergeSortRec(array, size, tmpArrayPtr); else cout << “Not enough memory to sort list.\n”); return; } delete [] tmpArrayPtr;
26
Mergesort void mergeSortRec(int array[],int size,int tmp[]) { int i;
int mid = size/2; if (size > 1){ mergeSortRec(array, mid, tmp); mergeSortRec(array+mid, size-mid, tmp); mergeArrays(array, mid, array+mid, size-mid, tmp); for (i = 0; i < size; i++) array[i] = tmp[i]; }
27
mergeArrays a: b: 3 5 15 28 30 6 10 14 22 43 50 aSize: 5 bSize: 6 tmp:
28
mergeArrays a: b: 3 5 15 28 30 6 10 14 22 43 50 i=0 j=0 tmp: k=0
29
mergeArrays a: b: 3 5 15 28 30 6 10 14 22 43 50 i=0 j=0 tmp: 3 k=0
30
mergeArrays a: b: 3 5 15 28 30 6 10 14 22 43 50 i=1 j=0 tmp: 3 5 k=1
31
mergeArrays a: b: 3 5 15 28 30 6 10 14 22 43 50 i=2 j=0 tmp: 3 5 6 k=2
32
mergeArrays a: b: i=2 j=1 tmp: k=3 3 5 15 28 30 6 10 14 22 43 50 3 5 6
33
mergeArrays a: b: i=2 j=2 tmp: k=4 3 5 15 28 30 6 10 14 22 43 50 3 5 6
34
mergeArrays a: b: i=2 j=3 tmp: k=5 3 5 15 28 30 6 10 14 22 43 50 3 5 6
35
mergeArrays a: b: i=3 j=3 tmp: k=6 3 5 15 28 30 6 10 14 22 43 50 3 5 6
36
mergeArrays a: b: i=3 j=4 tmp: k=7 3 5 15 28 30 6 10 14 22 43 50 3 5 6
37
mergeArrays a: b: i=4 j=4 tmp: k=8 3 5 15 28 30 6 10 14 22 43 50 3 5 6
38
mergeArrays Done. a: b: i=5 j=4 tmp: k=9 3 5 15 28 30 6 10 14 22 43 50
39
Merge Sort and Linked Lists
40
Mergesort Analysis . Merging the two lists of size n/2: O(n)
Merging the four lists of size n/4: O(n) . O (lg n) times Merging the n lists of size 1: O(n) Mergesort is O(n lg n) Space? The other sorts we have looked at (insertion, selection) are in-place (only require a constant amount of extra space) Mergesort requires O(n) extra space for merging
41
Mergesort Analysis Mergesort is O(n lg n) Space?
The other sorts we have looked at (insertion, selection) are in-place (only require a constant amount of extra space) Mergesort requires O(n) extra space for merging
42
Quicksort Quicksort is another divide and conquer algorithm
Quicksort is based on the idea of partitioning (splitting) the list around a pivot or split value
43
Quicksort First the list is partitioned around a pivot value. Pivot can be chosen from the beginning, end or middle of list): 4 12 4 10 8 5 5 2 11 7 3 5 pivot value
44
Quicksort The pivot is swapped to the last position and the
remaining elements are compared starting at the ends. 4 12 4 10 8 3 2 11 7 5 5 low high 5 pivot value
45
Quicksort Then the low index moves right until it is at an element
that is larger than the pivot value (i.e., it is on the wrong side) 4 12 12 10 8 3 6 6 2 11 7 5 low high 5 pivot value
46
Quicksort Then the high index moves left until it is at an
element that is smaller than the pivot value (i.e., it is on the wrong side) 4 12 4 10 8 3 6 6 2 2 11 7 5 low high 5 pivot value
47
Quicksort Then the two values are swapped and the index values are updated: 4 2 4 10 12 8 6 3 6 12 2 11 7 5 low high 5 pivot value
48
Quicksort This continues until the two index values pass each other: 4
2 10 3 8 10 3 6 6 12 11 7 5 low high 5 pivot value
49
Quicksort This continues until the two index values pass each other: 4
2 4 3 8 10 6 6 12 11 7 5 high low 5 pivot value
50
Quicksort Then the pivot value is swapped into position: 4 2 4 3 5 8
10 6 6 12 11 7 5 8 high low
51
Quicksort Recursively quicksort the two parts: 4 2 4 3 5 5 6 10 6 12
11 7 8 Quicksort the left part Quicksort the right part
52
Quicksort void quickSort(int array[], int size) { int index;
if (size > 1) index = partition(array, size); quickSort(array, index); quickSort(array+index+1, size - index-1); } End of lecture 45. At last!
53
Quicksort int partition(int array[], int size) { int k;
int mid = size/2; int index = 0; swap(array, array+mid); for (k = 1; k < size; k++){ if (array[k] < array[0]){ index++; swap(array+k, array+index); } swap(array, array+index); return index;
54
Data Structures-Course Recap
Arrays Link Lists Stacks Queues Binary Trees Sorting
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.