cscs-200 data structure and algorithms lecture-26-27-28
TRANSCRIPT
![Page 1: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/1.jpg)
CSCS-200 Data Structure and Algorithms
Lecture-26-27-28
![Page 2: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/2.jpg)
2
Inserting into a Heap
insert(15) with exchange
13
16
21
3126
24
65 32
6819
13 2116 24 3119 68 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
14
14
11
15
1512
![Page 3: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/3.jpg)
3
Inserting into a Heap
insert(15) with exchange
13
16
21
3126
24
65 32
68
19
13 2116 24 31 1968 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
14
14
11
15
15
12
![Page 4: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/4.jpg)
4
Inserting into a Heap
insert(15) with exchange
13
1621
3126
24
65 32
68
19
13 21 1624 31 1968 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
14
14
11
15
15
12
![Page 5: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/5.jpg)
5
Inserting into a Heap
insert(15) with exchange
13
1621
3126
24
65 32
68
19
13 21 1624 31 1968 65 26 32
1 2 3 4 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
14
14
11
15
15
12
![Page 6: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/6.jpg)
6
DeleteMin
• Finding the minimum is easy; it is at the top of the heap.• Deleting it (or removing it) causes a hole which needs to
be filled.
13
16
21
3126
24
65 32
6819
1
2 3
7654
8 9 10
14
11
![Page 7: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/7.jpg)
7
DeleteMin
deleteMin()
16
21
3126
24
65 32
6819
1
2 3
7654
8 9 10
14
11
![Page 8: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/8.jpg)
8
DeleteMin
deleteMin()
14
16
21
3126
24
65 32
6819
1
2 3
7654
8 9 10 11
![Page 9: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/9.jpg)
9
DeleteMin
deleteMin()
14
16
3126
24
65 32
6819
1
2 3
7654
8 9 10
21
11
![Page 10: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/10.jpg)
10
DeleteMin
deleteMin()
14
16
31
26
24
65 32
6819
1
2 3
7654
8 9 10
21
11
![Page 11: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/11.jpg)
11
DeleteMin
deleteMin(): heap size is reduced by 1.
14
16
31
26
24
65 32
6819
1
2 3
7654
8 9 10
21
![Page 12: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/12.jpg)
12
BuildHeap
• Suppose we are given as input N keys (or items) and we want to build a heap of the keys.
• Obviously, this can be done with N successive inserts.
• Each call to insert will either take unit time (leaf node) or log2N (if new key percolates all the way up to the root).
![Page 13: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/13.jpg)
13
BuildHeap
• The worst time for building a heap of N keys could be Nlog2N.
• It turns out that we can build a heap in linear time.
![Page 14: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/14.jpg)
14
BuildHeap
• Suppose we have a method percolateDown(p) which moves down the key in node p downwards.
• This is what was happening in deleteMin.
![Page 15: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/15.jpg)
15
BuildHeap
Initial data (N=15)
65 21 1926 14 1668 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 140
31 32
154
5 70 12
![Page 16: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/16.jpg)
16
BuildHeap
Initial data (N=15) 65
1921
1424
26
13 15
68
16
65 21 1926 14 1668 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
31
31
11
32
32
12
154
513 7014 1215
5 70 12
![Page 17: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/17.jpg)
17
BuildHeap
• The general algorithm is to place the N keys in an array and consider it to be an unordered binary tree.
• The following algorithm will build a heap out of N keys.
for( i = N/2; i > 0; i-- )
percolateDown(i);
![Page 18: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/18.jpg)
18
BuildHeap
i = 15/2 = 7 65
1921
1424
26
13 15
68
16
65 21 1926 14 1668 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
31
31
11
32
32
12
154
513 7014 1215
5 70 12
i
i
Why I=n/2?
![Page 19: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/19.jpg)
19
BuildHeap
i = 15/2 = 7 65
1921
1424
26
13 15
12
16
65 21 1926 14 1612 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
31
31
11
32
32
12
154
513 7014 6815
5 70 68
i
i
![Page 20: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/20.jpg)
20
BuildHeap
i = 6 65
1921
1424
26
13 15
12
16
65 21 1926 14 1612 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
31
31
11
32
32
12
154
513 7014 6815
5 70 68
i
i
![Page 21: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/21.jpg)
21
BuildHeap
i = 5 65
521
1424
26
13 15
12
16
65 21 526 14 1612 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
31
31
11
32
32
12
154
1913 7014 6815
19 70 68
i
i
![Page 22: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/22.jpg)
22
BuildHeap
i = 4 65
514
2124
26
13 15
12
16
65 14 526 21 1612 13 24 15
1 2 3 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
31
31
11
32
32
12
154
1913 7014 6815
19 70 68
i
i
![Page 23: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/23.jpg)
23
BuildHeap
i = 3 65
514
2124
13
26 15
12
16
65 14 513 21 1612 26 24 15
1 2 3 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
31
31
11
32
32
12
154
1913 7014 6815
19 70 68
i
i
![Page 24: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/24.jpg)
24
BuildHeap
i = 2 65
1614
2124
13
26 15
12
32
65 14 1613 21 3212 26 24 15
1 2 3 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
31
31
11
5
5
12
154
1913 7014 6815
19 70 68
i
i
![Page 25: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/25.jpg)
25
BuildHeap
i = 1 65
1614
2131
24
26 15
12
32
65 14 1624 21 3212 26 31 15
1 2 3 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
13
13
11
5
5
12
154
1913 7014 6815
19 70 68
i
i
![Page 26: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/26.jpg)
26
BuildHeap
Min heap 5
1614
2131
24
26 15
65
32
5 14 1624 21 3265 26 31 15
1 2 3 5 6 7 8 9 10 11 12 13 140
1
2 3
7654
8 9 10
13
13
11
12
12
12
154
1913 7014 6815
19 70 68
![Page 27: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/27.jpg)
27
Other Heap Operations
• decreaseKey(p, delta): lowers the value of the key at position ‘p’ by the amount ‘delta’. Since this might violate the heap order, the heap must be reorganized with percolate up (in min heap) or down (in max heap).
• increaseKey(p, delta): opposite of decreaseKey.
• remove(p): removes the node at position p from the heap. This is done by first decreaseKey(p, ) and then performing deleteMin().
![Page 28: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/28.jpg)
28
Heap code in C++
template <class eType>class Heap{ public: Heap( int capacity = 100 );
void insert( const eType & x ); void deleteMin( eType & minItem ); const eType & getMin( );
bool isEmpty( ); bool isFull( ); int Heap<eType>::getSize( );
![Page 29: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/29.jpg)
29
Heap code in C++
private: int currentSize; // Number of elements in heap eType* array; // The heap array int capacity; void percolateDown( int hole );};
![Page 30: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/30.jpg)
30
Heap code in C++
#include "Heap.h“
template <class eType>Heap<eType>::Heap( int capacity ){ array = new etype[capacity + 1]; currentSize=0;}
![Page 31: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/31.jpg)
31
Heap code in C++
// Insert item x into the heap, maintaining heap// order. Duplicates are allowed.template <class eType>bool Heap<eType>::insert( const eType & x ){ if( isFull( ) ) { cout << "insert - Heap is full." << endl; return 0; } // Percolate up int hole = ++currentSize; for(; hole > 1 && x < array[hole/2 ]; hole /= 2) array[ hole ] = array[ hole / 2 ]; array[hole] = x;}
![Page 32: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/32.jpg)
32
Heap code in C++
template <class eType>void Heap<eType>::deleteMin( eType & minItem ){ if( isEmpty( ) ) { cout << "heap is empty.“ << endl;
return; }
minItem = array[ 1 ]; array[ 1 ] = array[ currentSize-- ]; percolateDown( 1 );}
![Page 33: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/33.jpg)
33
Heap code in C++
// hole is the index at which the percolate begins.template <class eType>void Heap<eType>::percolateDown( int hole ){ int child; eType tmp = array[ hole ]; for( ; hole * 2 <= currentSize; hole = child ) { child = hole * 2; if( child != currentSize && array[child+1] < array[ child ] ) child++; // right child is smaller if( array[ child ] < tmp ) array[ hole ] = array[ child ]; else break; } array[ hole ] = tmp;}
![Page 34: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/34.jpg)
34
Heap code in C++
template <class eType>const eType& Heap<eType>::getMin( ){ if( !isEmpty( ) ) return array[ 1 ];}
template <class eType>void Heap<eType>::buildHeap(eType* anArray, int n ){ for(int i = 1; i <= n; i++) array[i] = anArray[i-1]; currentSize = n; for( int i = currentSize / 2; i > 0; i-- ) percolateDown( i );}
![Page 35: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/35.jpg)
35
Heap code in C++
template <class eType>bool Heap<eType>::isEmpty( ){ return currentSize == 0;}template <class eType>bool Heap<eType>::isFull( ){ return currentSize == capacity;}template <class eType>int Heap<eType>::getSize( ){ return currentSize;}
![Page 36: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/36.jpg)
36
BuildHeap in Linear Time
• How is buildHeap a linear time algorithm? I.e., better than Nlog2N?
• We need to show that the sum of heights is a linear function of N (number of nodes).
Theorem: For a perfect binary tree of height h containing 2h +1 – 1 nodes, the sum of the heights of nodes is 2h +1 – 1 – (h +1), or N-h-1.
![Page 37: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/37.jpg)
37
BuildHeap in Linear Time
It is easy to see that this tree consists of (20) node at height h, 21 nodes at height h –1, 22 at h-2 and, in general, 2i nodes at h – i.
![Page 38: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/38.jpg)
38
Complete Binary Tree
A
B
h : 20 nodes
H
D
I
E
J K
C
L
F
M
G
N O
h -1: 21 nodes
h -2: 22 nodes
h -3: 23 nodes
![Page 39: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/39.jpg)
39
BuildHeap in Linear Time
The sum of the heights of all the nodes is then
S = 2i(h – i), for i = 0 to h-1
= h + 2(h-1) + 4(h-2) + 8(h-3)+ ….. + 2h-1 (1)
Multiplying by 2 gives the equation2S = 2h + 4(h-1) + 8(h-2) + 16(h-3)+ ….. + 2h (2)
Subtract the two equations to getS = -h + 2 + 4 + 8 + 16+ ….. + 2h-1 +2h
= (2h+1 – 1) - (h+1)
Which proves the theorem.
![Page 40: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/40.jpg)
40
BuildHeap in Linear Time
Since a complete binary tree has between 2h
and 2h+1 nodes
S = (2h+1 – 1) - (h+1) N - log2(N+1)
Clearly, as N gets larger, the log2(N +1) term becomes insignificant and S becomes a function of N.
![Page 41: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/41.jpg)
41
BuildHeap in Linear Time
• Another way to prove the theorem.• The height of a node in the tree = the number of
edges on the longest downward path to a leaf • The height of a tree = the height of its root• For any node in the tree that has some height h,
darken h tree edges– Go down tree by traversing left edge then only
right edges• There are N – 1 tree edges, and h edges on right
path, so number of darkened edges is N – 1 – h, which proves the theorem.
![Page 42: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/42.jpg)
42
Height 1 Nodes
Marking the left edges for height 1 nodes
![Page 43: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/43.jpg)
43
Height 2 Nodes
Marking the first left edge and the subsequent right edge for height 2 nodes
![Page 44: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/44.jpg)
44
Height 3 Nodes
Marking the first left edge and the subsequent two right edges for height 3 nodes
![Page 45: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/45.jpg)
45
Height 4 Nodes
Marking the first left edge and the subsequent three right edges for height 4 nodes
![Page 46: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/46.jpg)
46
Theorem
N=31, treeEdges=30, H=4, dottedEdges=4 (H).Darkened Edges = 26 = N-H-1 (31-4-1)
![Page 47: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/47.jpg)
47
The Selection Problem
• Given a list of N elements (numbers, names etc.), which can be totally ordered, and an integer k, find the kth smallest (or largest) element.
• One way is to put these N elements in an array an sort it. The kth smallest of these is at the kth position.
![Page 48: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/48.jpg)
48
The Selection Problem
• A faster way is to put the N elements into an array and apply the buildHeap algorithm on this array.
• Finally, we perform k deleteMin operations. The last element extracted from the heap is our answer.
• The interesting case is k = N/2, since this is known as the median.
![Page 49: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/49.jpg)
49
HeapSort
• If k = N, and we record the deleteMin elements as they come off the heap, we will have essentially sorted the N elements.
• Later in the course, we will refine this idea to obtain a fast sorting algorithm called heapsort.
![Page 50: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/50.jpg)
50
• An array in which TableNodes are not stored consecutively
• Their place of storage is calculated using the key and a hash function
• Keys and entries are scattered throughout the array.
Implementation 6: Hashing
key entry
Key hash function
array index
4
10
123
![Page 51: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/51.jpg)
51
• insert: calculate place of storage, insert TableNode; (1)
• find: calculate place of storage, retrieve entry; (1)
• remove: calculate place of storage, set it to null; (1)
Hashing
key entry
4
10
123All are constant time (1) !
![Page 52: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/52.jpg)
52
Hashing
• We use an array of some fixed size T to hold the data. T is typically prime.
• Each key is mapped into some number in the range 0 to T-1 using a hash function, which ideally should be efficient to compute.
![Page 53: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/53.jpg)
53
Example: fruits
• Suppose our hash function gave us the following values: hashCode("apple") = 5
hashCode("watermelon") = 3hashCode("grapes") = 8hashCode("cantaloupe") = 7hashCode("kiwi") = 0hashCode("strawberry") = 9hashCode("mango") = 6hashCode("banana") = 2
kiwi
bananawatermelon
applemango
cantaloupegrapes
strawberry
0
1
2
3
4
5
6
7
8
9
![Page 54: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/54.jpg)
54
Example
• Store data in a table array: table[5] = "apple"
table[3] = "watermelon" table[8] = "grapes" table[7] = "cantaloupe" table[0] = "kiwi" table[9] = "strawberry" table[6] = "mango" table[2] = "banana"
kiwi
bananawatermelon
applemango
cantaloupegrapes
strawberry
0
1
2
3
4
5
6
7
8
9
![Page 55: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/55.jpg)
55
Example
• Associative array: table["apple"]
table["watermelon"] table["grapes"] table["cantaloupe"] table["kiwi"] table["strawberry"] table["mango"] table["banana"]
kiwi
bananawatermelon
applemango
cantaloupegrapes
strawberry
0
1
2
3
4
5
6
7
8
9
![Page 56: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/56.jpg)
56
Example Hash Functions
• If the keys are strings the hash function is some function of the characters in the strings.
• One possibility is to simply add the ASCII values of the characters:
TableSizeABChExample
TableSizeistrstrhlength
i
)%676665()(:
%][)(1
0
![Page 57: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/57.jpg)
57
Finding the hash function
int hashCode( char* s ){
int i, sum;sum = 0;for(i=0; i < strlen(s); i++ ) sum = sum + s[i]; // ascii value
return sum % TABLESIZE;
}
![Page 58: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/58.jpg)
58
Example Hash Functions
• Another possibility is to convert the string into some number in some arbitrary base b (b also might be a prime number):
TbbbABChExample
Tbistrstrhlength
i
i
)%676665()(:
%][)(
210
1
0
![Page 59: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/59.jpg)
59
Example Hash Functions
• If the keys are integers then key%T is generally a good hash function, unless the data has some undesirable features.
• For example, if T = 10 and all keys end in zeros, then key%T = 0 for all keys.
• In general, to avoid situations like this, T should be a prime number.
![Page 60: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/60.jpg)
60
Collision
Suppose our hash function gave us the following values:
– hash("apple") = 5hash("watermelon") = 3hash("grapes") = 8hash("cantaloupe") = 7hash("kiwi") = 0hash("strawberry") = 9hash("mango") = 6hash("banana") = 2
kiwi
bananawatermelon
applemango
cantaloupegrapes
strawberry
0
1
2
3
4
5
6
7
8
9• Now what?
hash("honeydew") = 6
![Page 61: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/61.jpg)
61
Collision
• When two values hash to the same array location, this is called a collision
• Collisions are normally treated as “first come, first served”—the first value that hashes to the location gets it
• We have to find something to do with the second and subsequent values that hash to this same location.
![Page 62: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/62.jpg)
62
Solution for Handling collisions
• Solution #1: Search from there for an empty location– Can stop searching when we find the value or
an empty location.– Search must be wrap-around at the end.
![Page 63: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/63.jpg)
63
Solution for Handling collisions
• Solution #2: Use a second hash function– ...and a third, and a fourth, and a fifth, ...
![Page 64: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/64.jpg)
64
Solution for Handling collisions
• Solution #3: Use the array location as the header of a linked list of values that hash to this location
![Page 65: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/65.jpg)
65
Solution 1: Open Addressing
• This approach of handling collisions is called open addressing; it is also known as closed hashing.
• More formally, cells at h0(x), h1(x), h2(x), … are tried in succession where
hi(x) = (hash(x) + f(i)) mod TableSize,
with f(0) = 0.• The function, f, is the collision resolution
strategy.
![Page 66: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/66.jpg)
66
Linear Probing
• We use f(i) = i, i.e., f is a linear function of i. Thus
location(x) = (hash(x) + i) mod TableSize
• The collision resolution strategy is called linear probing because it scans the array sequentially (with wrap around) in search of an empty cell.
![Page 67: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/67.jpg)
67
Linear Probing: insert
• Suppose we want to add seagull to this hash table
• Also suppose:– hashCode(“seagull”) = 143
– table[143] is not empty– table[143] != seagull
– table[144] is not empty– table[144] != seagull
– table[145] is empty
• Therefore, put seagull at location 145
robin
sparrow
hawk
bluejay
owl
. . .
141
142
143
144
145
146
147
148
. . .
seagull
![Page 68: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/68.jpg)
68
Linear Probing: insert
• Suppose you want to add hawk to this hash table
• Also suppose– hashCode(“hawk”) = 143
– table[143] is not empty– table[143] != hawk
– table[144] is not empty– table[144] == hawk
• hawk is already in the table, so do nothing.
robin
sparrow
hawk
seagull
bluejay
owl
. . .
141
142
143
144
145
146
147
148
. . .
![Page 69: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/69.jpg)
69
Linear Probing: insert
• Suppose:– You want to add cardinal to
this hash table– hashCode(“cardinal”) = 147
– The last location is 148– 147 and 148 are occupied
• Solution:– Treat the table as circular;
after 148 comes 0– Hence, cardinal goes in
location 0 (or 1, or 2, or ...)
robin
sparrow
hawk
seagull
bluejay
owl
. . .
141
142
143
144
145
146
147
148
![Page 70: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/70.jpg)
70
Linear Probing: find
• Suppose we want to find hawk in this hash table
• We proceed as follows:– hashCode(“hawk”) = 143– table[143] is not empty– table[143] != hawk– table[144] is not empty– table[144] == hawk (found!)
• We use the same procedure for looking things up in the table as we do for inserting them
robin
sparrow
hawk
seagull
bluejay
owl
. . .
141
142
143
144
145
146
147
148
. . .
![Page 71: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/71.jpg)
71
Linear Probing and Deletion
• If an item is placed in array[hash(key)+4], then the item just before it is deleted
• How will probe determine that the “hole” does not indicate the item is not in the array?
• Have three states for each location– Occupied– Empty (never used)– Deleted (previously used)
![Page 72: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/72.jpg)
72
Clustering
• One problem with linear probing technique is the tendency to form “clusters”.
• A cluster is a group of items not containing any open slots
• The bigger a cluster gets, the more likely it is that new values will hash into the cluster, and make it ever bigger.
• Clusters cause efficiency to degrade.
![Page 73: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/73.jpg)
73
Quadratic Probing
• Quadratic probing uses different formula:– Use F(i) = i2 to resolve collisions– If hash function resolves to H and a search in cell H is
inconclusive, try H + 12, H + 22, H + 32, …
• Probe array[hash(key)+12], thenarray[hash(key)+22], thenarray[hash(key)+32], and so on
– Virtually eliminates primary clusters
![Page 74: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/74.jpg)
74
Collision resolution: chaining
• Each table position is a linked list
• Add the keys and entries anywhere in the list (front easiest)
4
10
123
key entry key entry
key entry key entry
key entry
No need to change position!
![Page 75: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/75.jpg)
75
Collision resolution: chaining
• Advantages over open addressing:– Simpler insertion and
removal– Array size is not a
limitation • Disadvantage
– Memory overhead is large if entries are small.
4
10
123
key entry key entry
key entry key entry
key entry
![Page 76: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/76.jpg)
76
Applications of Hashing
• Compilers use hash tables to keep track of declared variables (symbol table).
• A hash table can be used for on-line spelling checkers — if misspelling detection (rather than correction) is important, an entire dictionary can be hashed and words checked in constant time.
![Page 77: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/77.jpg)
77
Applications of Hashing
• Game playing programs use hash tables to store seen positions, thereby saving computation time if the position is encountered again.
• Hash functions can be used to quickly check for inequality — if two elements hash to different values they must be different.
![Page 78: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/78.jpg)
78
When is hashing suitable?
• Hash tables are very good if there is a need for many searches in a reasonably stable table.
• Hash tables are not so good if there are many insertions and deletions, or if table traversals are needed — in this case, AVL trees are better.
• Also, hashing is very slow for any operations which require the entries to be sorted– e.g. Find the minimum key
![Page 79: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/79.jpg)
SORTING AGAIN !!!
![Page 80: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/80.jpg)
80
Summary
• Insertion, Selection and Bubble sort: – Worst case time complexity is proportional to N2.
Best sorting routines are N log(N)
![Page 81: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/81.jpg)
81
NLogN Algorithms
• Divide and Conquer• Merge Sort• Quick Sort• Heap Sort
![Page 82: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/82.jpg)
82
Divide and Conquer
What if we split the list into two parts?
10 12 8 4 2 11 7 510 12 8 4 2 11 7 5
![Page 83: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/83.jpg)
83
Divide and Conquer
Sort the two parts:
10 12 8 4 2 11 7 54 8 10 12 2 5 7 11
![Page 84: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/84.jpg)
84
Divide and Conquer
Then merge the two parts together:
4 8 10 12 2 5 7 112 4 5 7 8 10 11 12
![Page 85: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/85.jpg)
85
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)
![Page 86: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/86.jpg)
86
Divide and Conquer
• Why not divide the halves in half?• The quarters in half?• And so on . . .• When should we stop?
At n = 1
![Page 87: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/87.jpg)
87
SearchSearch
Divide and Conquer
SearchSearch
SearchSearch
Recall: Binary Search
![Page 88: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/88.jpg)
88
SortSort
Divide and Conquer
SortSort SortSort
SortSort SortSort SortSort SortSort
![Page 89: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/89.jpg)
89
Divide and Conquer
CombineCombine
CombineCombine CombineCombine
![Page 90: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/90.jpg)
90
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
![Page 91: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/91.jpg)
91
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
![Page 92: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/92.jpg)
92
Merging: animation
4 8 10 12 2 5 7 11
2
![Page 93: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/93.jpg)
93
Merging: animation
4 8 10 12 2 5 7 11
2 4
![Page 94: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/94.jpg)
94
Merging: animation
4 8 10 12 2 5 7 11
2 4 5
![Page 95: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/95.jpg)
95
Merging
4 8 10 12 2 5 7 11
2 4 5 7
![Page 96: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/96.jpg)
96
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
![Page 97: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/97.jpg)
97
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
![Page 98: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/98.jpg)
98
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
![Page 99: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/99.jpg)
99
Mergesort
10 12 11 2 7 584
Mergesort the right half.
11 2 7 5
11 2
11 2
![Page 100: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/100.jpg)
100
Mergesort
10 12 11 2 7 584
Mergesort the right half.
11 2 7 5
11 22 11
2 11
![Page 101: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/101.jpg)
101
Mergesort
10 12 11 2 7 584
Mergesort the right half.
11 2 7 52 11
7 5
7 5
5 7
![Page 102: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/102.jpg)
102
Mergesort
10 12 11 2 7 584
Mergesort the right half.
11 2 5 72 11
![Page 103: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/103.jpg)
103
Mergesort
10 12 2 5 7 1184
Mergesort the right half.
![Page 104: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/104.jpg)
104
Mergesort
5 7 8 10 11 1242
Merge the two halves.
![Page 105: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/105.jpg)
105
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
![Page 106: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/106.jpg)
106
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
![Page 107: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/107.jpg)
107
3 5 15 28 30 6 10 14 22 43 50a:a: b:b:
aSize: 5aSize: 5 bSize: 6bSize: 6
mergeArrays
tmp:tmp:
![Page 108: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/108.jpg)
108
mergeArrays
5 15 28 30 10 14 22 43 50a:a: b:b:
tmp:tmp:
i=0i=0
k=0k=0
j=0j=0
3 6
![Page 109: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/109.jpg)
109
mergeArrays
5 15 28 30 10 14 22 43 50a:a: b:b:
tmp:tmp:
i=0i=0
k=0k=0
3
j=0j=0
3 6
![Page 110: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/110.jpg)
110
mergeArrays
3 15 28 30 10 14 22 43 50a:a: b:b:
tmp:tmp:
i=1i=1 j=0j=0
k=1k=1
3 5
5 6
![Page 111: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/111.jpg)
111
mergeArrays
3 5 28 30 10 14 22 43 50a:a: b:b:
3 5tmp:tmp:
i=2i=2 j=0j=0
k=2k=2
6
15 6
![Page 112: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/112.jpg)
112
mergeArrays
3 5 28 30 6 14 22 43 50a:a: b:b:
3 5 6tmp:tmp:
i=2i=2 j=1j=1
k=3k=3
15 10
10
![Page 113: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/113.jpg)
113
10
mergeArrays
3 5 28 30 6 22 43 50a:a: b:b:
3 5 6tmp:tmp:
i=2i=2 j=2j=2
k=4k=4
15 10 14
14
![Page 114: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/114.jpg)
114
1410
mergeArrays
3 5 28 30 6 14 43 50a:a: b:b:
3 5 6tmp:tmp:
i=2i=2 j=3j=3
k=5k=5
15 10 22
15
![Page 115: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/115.jpg)
115
1410
mergeArrays
3 5 30 6 14 43 50a:a: b:b:
3 5 6tmp:tmp:
i=3i=3 j=3j=3
k=6k=6
15 10 22
2215
28
![Page 116: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/116.jpg)
116
1410
mergeArrays
3 5 30 6 14 50a:a: b:b:
3 5 6tmp:tmp:
i=3i=3 j=4j=4
k=7k=7
15 10 22
2815
28 43
22
![Page 117: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/117.jpg)
117
1410
mergeArrays
3 5 6 14 50a:a: b:b:
3 5 6tmp:tmp:
i=4i=4 j=4j=4
k=8k=8
15 10 22
3015
28 43
22
30
28
![Page 118: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/118.jpg)
118
1410
mergeArrays
3 5 6 14 50a:a: b:b:
3 5 6 30tmp:tmp:
i=5i=5 j=4j=4
k=9k=9
15 10 22
15
28 43
22
30
28 43 50
Done.
![Page 119: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/119.jpg)
119
Merge Sort and Linked Lists
Sort Sort
Merge
![Page 120: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/120.jpg)
120
Mergesort AnalysisMerging 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
![Page 121: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/121.jpg)
121
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
![Page 122: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/122.jpg)
122
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
![Page 123: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/123.jpg)
123
Quicksort
First the list is partitioned around a pivot value. Pivot can be chosen from the beginning, end or middle of list):
8 32 11 754 10124 5
5
pivot value
![Page 124: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/124.jpg)
124
Quicksort
The pivot is swapped to the last position and the remaining elements are compared starting at theends.
8 3 2 11 7 54 10124 5
low high
5
pivot value
![Page 125: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/125.jpg)
125
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)
8 6 2 11 7 510124 6
low high
5
pivot value
312
![Page 126: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/126.jpg)
126
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)
8 6 2 11 7 54 10124 6
low high
5
pivot value
3 2
![Page 127: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/127.jpg)
127
Quicksort
Then the two values are swapped and the index values are updated:
8 6 2 11 7 54 10124 6
low high
5
pivot value
32 12
![Page 128: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/128.jpg)
128
Quicksort
This continues until the two index values pass each other:
8 6 12 11 7 5424 6
low high
5
pivot value
3103 10
![Page 129: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/129.jpg)
129
Quicksort
This continues until the two index values pass each other:
8 6 12 11 7 5424 6
lowhigh
5
pivot value
103
![Page 130: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/130.jpg)
130
Quicksort
Then the pivot value is swapped into position:
8 6 12 11 7 5424 6
lowhigh
103 85
![Page 131: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/131.jpg)
131
Quicksort
Recursively quicksort the two parts:
5 6 12 11 7 8424 6103
Quicksort the left part Quicksort the right part
5
![Page 132: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/132.jpg)
132
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
![Page 133: CSCS-200 Data Structure and Algorithms Lecture-26-27-28](https://reader036.vdocuments.site/reader036/viewer/2022062409/5697c02b1a28abf838cd8767/html5/thumbnails/133.jpg)
133
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