Class No.39 Data Structures http://ecomputernotes.com

Divide and Conquer

What if we split the list into two parts?

10

12

8

4

2

11

7

5

http://ecomputernotes.com

Divide and Conquer

Sort the two parts: 10 4

12 8

10 8

12 4

2

11 5

7

11 5

http://ecomputernotes.com

Divide and Conquer

Then merge the two parts together: 24

48

10 5

12 7

82

10 5

11 7

11 12

http://ecomputernotes.com

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)2 + 100 = 2500 + 2500 + 100 = 5100 (n2 = 10,000)

http://ecomputernotes.com

Divide and Conquer    

Why not divide the halves in half? The quarters in half? And so on . . . When should we stop? At n = 1

http://ecomputernotes.com

Divide and Conquer Recall: Binary Search Search Search

Search

http://ecomputernotes.com

Divide and Conquer

Sort Sort

Sort

Sort

Sort

Sort

http://ecomputernotes.com

Sort

Divide and Conquer

Combine Combine

Combine

http://ecomputernotes.com

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

http://ecomputernotes.com

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

http://ecomputernotes.com

Merging: animation

4

8

10

12

2

5

7

2

http://ecomputernotes.com

11

Merging: animation

4

8

10

2

4

12

2

5

7

http://ecomputernotes.com

11

Merging: animation

4

8

10

12

2

4

5

2

5

7

http://ecomputernotes.com

11

Merging

4

8

10

12

2

4

5

2

5

7

7

http://ecomputernotes.com

11

Mergesort Split the list in half. 10

Mergesort the left half.

4

8

Split the list in half. 10

11

2

7

5

Mergesort the left half.

4

8

Split the list in half. 10

12

12

Mergesort the left half.

4

Mergesort the right. 10

4

http://ecomputernotes.com

Mergesort

http://ecomputernotes.com

10

4

8

12

10

4

8

12

11

2

7

Mergesort the right half. Merge the two halves. 10 4

10 4

8

12

Merge the two halves. 8

12

5

Mergesort 10

4

8

12

10 8

12

11

2

7

5

Merge the two halves. 10 4

84

Mergesort the right half. Merge the two halves. 10 4

10 4

8

12

http://ecomputernotes.com

Mergesort Mergesort the right half. 4

8

10

12

11

2

7

5

11

2

7

5

11

11

2

2

http://ecomputernotes.com

Mergesort Mergesort the right half. 4

8

10

12

11

2

7

5

11 2

11 2

7

5

11 2

11 2

http://ecomputernotes.com

Mergesort Mergesort the right half. 4

8

10

12

11

2

7

5

11 2

11 2

7

5

5

7

7

http://ecomputernotes.com

5

Mergesort Mergesort the right half. 4

8

10

12

11

2

7

5

11 2

11 2

7

5

http://ecomputernotes.com

Mergesort Mergesort the right half. 4

8

10

12

2

5

7

11

http://ecomputernotes.com

Mergesort Merge the two halves. 2

4

5

7

8

10

11

12

http://ecomputernotes.com

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; }

http://ecomputernotes.com

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]; } }

http://ecomputernotes.com

mergeArrays

a: 3

5

15 28 30

aSize: 5

b: 6

10 14 22 43 50 bSize: 6

tmp:

http://ecomputernotes.com

mergeArrays

a: 3

5

15 28 30

i=0

b: 6

10 14 22 43 50

j=0

tmp: k=0

http://ecomputernotes.com

mergeArrays

a: 3

5

15 28 30

i=0

b: 6

10 14 22 43 50

j=0

tmp: 3 k=0

http://ecomputernotes.com

mergeArrays

a: 3

5

15 28 30

i=1

b: 6

10 14 22 43 50

j=0

tmp: 3

5

k=1

http://ecomputernotes.com

mergeArrays

a: 3

5

15 28 30

i=2

b: 6

10 14 22 43 50

j=0

tmp: 3

5

6

k=2

http://ecomputernotes.com

mergeArrays

a: 3

5

b: 6

15 28 30

i=2

10 14 22 43 50

j=1

tmp: 3

5

6

10

k=3

http://ecomputernotes.com

mergeArrays

a: 3

5

15 28 30

b: 6

i=2

10 14 22 43 50

j=2

tmp: 3

5

6

10 14

k=4

http://ecomputernotes.com

mergeArrays

a: 3

5

15 28 30

i=2

b: 6

10 14 22 43 50

j=3

tmp: 3

5

6

10 14 15

k=5

http://ecomputernotes.com

mergeArrays

a: 3

5

15 28 30

i=3

b: 6

10 14 22 43 50

j=3

tmp: 3

5

6

10 14 15 22

k=6

http://ecomputernotes.com

mergeArrays

a: 3

5

15 28 30

i=3

b: 6

10 14 22 43 50

j=4

tmp: 3

5

6

10 14 15 22 28

k=7

http://ecomputernotes.com

mergeArrays

a: 3

5

15 28 30

i=4

b: 6

10 14 22 43 50

j=4

tmp: 3

5

6

10 14 15 22 28 30

k=8

http://ecomputernotes.com

mergeArrays

a: 3

5

15 28 30

i=5

b: 6

10 14 22 43 50

j=4

Done.

tmp: 3

5

6

10 14 15 22 28 30 43 50

k=9

http://ecomputernotes.com

Sort

Sort

Merge

http://ecomputernotes.com

Mergesort Analysis Merging the two lists of size n/2: O(n) Merging the four lists of size n/4: O(n) 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

O (lg n) times

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

http://ecomputernotes.com

Quicksort ď&#x201A;§ Quicksort is another divide and conquer algorithm ď&#x201A;§ Quicksort is based on the idea of partitioning (splitting) the list around a pivot or split value

http://ecomputernotes.com

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

2

11

7

3

5 pivot value

http://ecomputernotes.com

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

high

low 5 pivot value

http://ecomputernotes.com

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

10

low

8

36

2

11

7

5

high

5 pivot value

http://ecomputernotes.com

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

low

8

36

2

11

7

5

high

5 pivot value

http://ecomputernotes.com

Quicksort Then the two values are swapped and the index values are updated:

4

12 24

10 low

8

36

12 2

11

7

5

high

5 pivot value

http://ecomputernotes.com

Quicksort This continues until the two index values pass each other:

4

42

10 3 low

8

10 36

12

11

7

5

high

5 pivot value

http://ecomputernotes.com

Quicksort This continues until the two index values pass each other:

4

42

3

8

10 6

12

11

7

5

high low 5 pivot value

http://ecomputernotes.com

Quicksort Then the pivot value is swapped into position:

4

42

3

58

10 6

12

11

7

85

high low

http://ecomputernotes.com

Quicksort Recursively quicksort the two parts:

4

42

3

5

Quicksort the left part

10 6

12

11

7

8

Quicksort the right part

http://ecomputernotes.com

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); } }

http://ecomputernotes.com

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; }

Data Structures-Course Recap      

Arrays Link Lists Stacks Queues Binary Trees Sorting

http://ecomputernotes.com

computer notes - Data Structures - 39

Class No.39 Data Structures http://ecomputernotes.com What if we split the list into two parts? 10 12 8 4 2 11 7 510128 4 2 11 7 5 http://ec...