computer notes - data structures - 39
TRANSCRIPT
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 1/54
Class No.39
Data Structures
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 2/54
Divide and Conquer
What if we split the list into two parts?
10 12 8 4 2 11 7 5
10 12 8 4 2 11 7 5
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 3/54
Divide and Conquer
Sort the two parts:
10 12 8 4 2 11 7 5
4 8 10 12 2 5 7 11
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 4/54
Divide and Conquer
Then merge the two parts together:
4 8 10 12 2 5 7 11
2 4 5 7 8 10 11 12
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 5/54
Analysis
To sort the halves (n /2)2+(n /2)2
To merge the two halves n
So, for n=100, divide and conquertakes:
= (100/2)2 + (100/2)2 + 100
= 2500 + 2500 + 100
= 5100 (n2 = 10,000)
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 6/54
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
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 7/54
Search
Divide and Conquer
Search
Search
Recall: Binary Search
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 8/54
Sort
Divide and Conquer
Sort Sort
Sort Sort Sort Sort
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 9/54
Divide and Conquer
Combine
Combine Combine
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 10/54
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 halvestogether
Mergesort can be implemented
recursively
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 11/54
Mergesort
The mergesort algorithm involves threesteps:
• If the number of items to sort is 0 or1, return
• Recursively sort the first and secondhalves separately
• Merge the two sorted halves into asorted group
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 12/54
Merging: animation
4 8 10 12 2 5 7 11
2
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 13/54
Merging: animation
4 8 10 12 2 5 7 11
2 4
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 14/54
Merging: animation
4 8 10 12 2 5 7 11
2 4 5
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 15/54
Merging
4 8 10 12 2 5 7 11
2 4 5 7
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 16/54
Mergesort
8 12 11 2 7 5410
Split the list in half.
8 12410
Mergesort the left half.
Split the list in half. Mergesort the left half.
410
Split the list in half. Mergesort the left half.
10
Mergesort the right.
4
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 17/54
Mergesort
8 12 11 2 7 5410
8 12410
410
Mergesort the right half.
Merge the two halves.
104 8 12
128
Merge the two halves.
88 12
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 18/54
Mergesort
8 12 11 2 7 5410
8 12410
Merge the two halves.
410
Mergesort the right half. Merge the two halves.
104 8 12
10 1284
104 8 12
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 19/54
Mergesort
10 12 11 2 7 584
Mergesort the right half.
11 2 7 5
11 2
11 2
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 20/54
Mergesort
10 12 11 2 7 584
Mergesort the right half.
11 2 7 5
11 22 11
2 11
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 21/54
Mergesort
10 12 11 2 7 584
Mergesort the right half.
11 2 7 52 11
75
7 5
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 22/54
Mergesort
10 12 11 2 7 584
Mergesort the right half.
11 2 572 11
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 23/54
Mergesort
10 12 2 5 7 1184
Mergesort the right half.
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 24/54
Mergesort
5 7 8 10 11 1242
Merge the two halves.
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 25/54
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;
}
Mergesort
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 26/54
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];
}
}
Mergesort
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 27/54
3 5 15 28 30 6 10 14 22 43 50a: b:
aSize: 5 bSize: 6
mergeArrays
tmp:
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 28/54
mergeArrays
5 15 28 30 10 14 22 43 50a: b:
tmp:
i=0
k=0
j=0
3 6
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 29/54
mergeArrays
5 15 28 30 10 14 22 43 50a: b:
tmp:
i=0
k=0
3
j=0
3 6
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 30/54
mergeArrays
3 15 28 30 10 14 22 43 50a: b:
tmp:
i=1 j=0
k=1
3 5
5 6
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 31/54
mergeArrays
3 5 28 30 10 14 22 43 50a: b:
3 5tmp:
i=2 j=0
k=2
6
15 6
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 32/54
mergeArrays
3 5 28 30 6 14 22 43 50a: b:
3 5 6tmp:
i=2 j=1
k=3
15 10
10
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 33/54
10
mergeArrays
3 5 28 30 6 22 43 50a: b:
3 5 6tmp:
i=2 j=2
k=4
15 10 14
14
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 34/54
1410
mergeArrays
3 5 28 30 6 14 43 50a: b:
3 5 6tmp:
i=2 j=3
k=5
15 10 22
15
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 35/54
1410
mergeArrays
3 5 30 6 14 43 50a: b:
3 5 6tmp:
i=3 j=3
k=6
15 10 22
2215
28
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 36/54
1410
mergeArrays
3 5 30 6 14 50a: b:
3 5 6tmp:
i=3 j=4
k=7
15 10 22
2815
28 43
22
http://ecomputernotes.com
A
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 37/54
1410
mergeArrays
3 5 6 14 50a: b:
3 5 6tmp:
i=4 j=4
k=8
15 10 22
3015
28 43
22
30
28
http://ecomputernotes.com
A
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 38/54
1410
mergeArrays
3 5 6 14 50a: b:
3 5 6 30tmp:
i=5 j=4
k=9
15 10 22
15
28 43
22
30
28 43 50
Done.
http://ecomputernotes.com
M S t d Li k d Li t
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 39/54
Merge Sort and Linked Lists
Sort Sort
Merge
http://ecomputernotes.com
Mergesort Analysis
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 40/54
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)
O (lg n)
times
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 extraspace)
Mergesort requires O(n) extra space for merging
M t A l i
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 41/54
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 extraspace)
Mergesort requires O(n) extra space for
merging
http://ecomputernotes.com
Q i k t
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 42/54
Quicksort
Quicksort is another divide and conqueralgorithm
Quicksort is based on the idea of partitioning(splitting) the list around a pivot or split
value
http://ecomputernotes.com
Q i k t
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 43/54
Quicksort
First the list is partitioned around a pivot value.Pivot can be chosen from the beginning, end ormiddle of list):
8 32 11 754 10124 5
5
pivot value
http://ecomputernotes.com
Q i k t
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 44/54
Quicksort
The pivot is swapped to the last position and theremaining elements are compared starting at theends.
8 3 2 11 7 54 10124 5
low high
5
pivot value
http://ecomputernotes.com
Q ickso t
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 45/54
Quicksort
Then the low index moves right until it is at an elementthat is larger than the pivot value (i.e., it is on thewrong side)
8 6 2 11 7 510124 6
low high
5
pivot value
312
http://ecomputernotes.com
Quicksort
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 46/54
Quicksort
Then the high index moves left until it is at anelement that is smaller than the pivot value (i.e., itis on the wrong side)
8 6 2 11 7 54 10124 6
low high
5
pivot value
3 2
http://ecomputernotes.com
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 47/54
Quicksort
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 48/54
Quicksort
This continues until the two index values passeach other:
8 6 12 11 7 5424 6
low high
5
pivot value
3103 10
http://ecomputernotes.com
Quicksort
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 49/54
Quicksort
This continues until the two index values passeach other:
8 6 12 11 7 5424 6
lowhigh
5
pivot value
103
http://ecomputernotes.com
Quicksort
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 50/54
Quicksort
Then the pivot value is swapped into position:
8 6 12 11 7 5424 6
lowhigh
103 85
http://ecomputernotes.com
Quicksort
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 51/54
Quicksort
Recursively quicksort the two parts:
5 6 12 11 7 8424 6103
Quicksort the left part Quicksort the right part
5
http://ecomputernotes.com
Quicksort
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 52/54
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);
}}
Quicksort
http://ecomputernotes.com
Quicksort
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 53/54
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;
}
Quicksort
Data Structures Course Recap
8/3/2019 Computer Notes - Data Structures - 39
http://slidepdf.com/reader/full/computer-notes-data-structures-39 54/54
Data Structures-Course Recap
Arrays Link Lists
Stacks
Queues
Binary Trees
Sorting