1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community...
-
Upload
colin-garrett -
Category
Documents
-
view
241 -
download
0
Transcript of 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community...
![Page 1: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/1.jpg)
1
Data Structures and Algorithms
Sorting and Searching Algorithms
Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus
and Robert Moyer, Montgomery County Community College
![Page 2: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/2.jpg)
2
Sorting means . . .
The values stored in an array have keys of a type for which the relational operators are defined. (We also assume unique keys.)
Sorting rearranges the elements into either ascending or descending order within the array. (We’ll use ascending order.)
![Page 3: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/3.jpg)
3
Divides the array into two parts: already sorted, and not yet sorted.
On each pass, finds the smallest of the unsorted elements, and swaps it into its correct place, thereby increasing the number of sorted elements by one.
Straight Selection Sort
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
36
24
10
6
12
![Page 4: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/4.jpg)
4
Selection Sort: Pass One
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
36
24
10
6
12
UNSORTED
![Page 5: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/5.jpg)
5
Selection Sort: End Pass One
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
6
24
10
36
12
UNSORTED
SORTED
![Page 6: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/6.jpg)
6
SORTED
Selection Sort: Pass Two
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
6
24
10
36
12
UNSORTED
![Page 7: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/7.jpg)
7
Selection Sort: End Pass Two
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
6
10
24
36
12
UNSORTED
SORTED
![Page 8: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/8.jpg)
8
Selection Sort: Pass Three
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
6
10
24
36
12
UNSORTED
SORTED
![Page 9: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/9.jpg)
9
Selection Sort: End Pass Three
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
6
10
12
36
24
SORTED
UNSORTED
![Page 10: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/10.jpg)
10
Selection Sort: Pass Four
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
6
10
12
36
24
SORTED
UNSORTED
![Page 11: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/11.jpg)
11
Selection Sort: End Pass Four
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
6
10
12
24
36
SORTED
![Page 12: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/12.jpg)
12
Selection Sort: How many comparisons?
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
6
10
12
24
36
4 compares for values[0]
3 compares for values[1]
2 compares for values[2]
1 compare for values[3]
= 4 + 3 + 2 + 1
![Page 13: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/13.jpg)
13
For selection sort in general
The number of comparisons when the array contains N elements is
Sum = (N-1) + (N-2) + . . . + 2 + 1
![Page 14: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/14.jpg)
14
Notice that . . . Sum = (N-1) + (N-2) + . . . + 2 + 1
+ Sum = 1 + 2 + . . . + (N-2) + (N-1)
2* Sum = N + N + . . . + N + N
2 * Sum = N * (N-1)
Sum = N * (N-1)
2
![Page 15: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/15.jpg)
15
For selection sort in general
The number of comparisons when the array contains N elements is
Sum = (N-1) + (N-2) + . . . + 2 + 1
Sum = N * (N-1) /2
Sum = .5 N2 - .5 N
Sum = O(N2)
![Page 16: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/16.jpg)
template <class ItemType >int MinIndex ( ItemType values [ ] , int start , int end )
// Post: Function value = index of the smallest value in// values [start] . . values [end].{
int indexOfMin = start ;
for ( int index = start + 1 ; index <= end ; index++ )
if ( values [ index ] < values [ indexOfMin ] )
indexOfMin = index ;
return indexOfMin;
}
16
![Page 17: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/17.jpg)
template <class ItemType >void SelectionSort ( ItemType values [ ] , int numValues )
// Post: Sorts array values[0 . . numValues-1 ] into ascending // order by key{
int endIndex = numValues - 1 ;
for ( int current = 0 ; current < endIndex ; current++ )
Swap ( values [ current ] , values [ MinIndex ( values, current, endIndex ) ] ) ;
}
17
![Page 18: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/18.jpg)
18
Compares neighboring pairs of array elements, starting with the last array element, and swaps neighbors whenever they are not in correct order.
On each pass, this causes the smallest element to “bubble up” to its correct place in the array.
Bubble Sort
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
36
24
10
6
12
![Page 19: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/19.jpg)
template <class ItemType >void BubbleUp ( ItemType values [ ] , int start , int end )
// Post: Neighboring elements that were out of order have been// swapped between values [start] and values [end],// beginning at values [end].{
for ( int index = end ; index > start ; index-- )
if (values [ index ] < values [ index - 1 ] )
Swap ( values [ index ], values [ index - 1 ] ) ;
}
19
![Page 20: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/20.jpg)
template <class ItemType >void BubbleSort ( ItemType values [ ] , int numValues )
// Post: Sorts array values[0 . . numValues-1 ] into ascending // order by key{
int current = 0 ;
while ( current < numValues - 1 )
BubbleUp ( values , current , numValues - 1 ) ;
current++ ;
}
20
![Page 21: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/21.jpg)
21
One by one, each as yet unsorted array element is inserted into its proper place with respect to the already sorted elements.
On each pass, this causes the number of already sorted elements to increase by one.
Insertion Sort
values [ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
36
24
10
6
12
![Page 22: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/22.jpg)
22
Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted.
To insert 12, we need to make room for it by moving first 36 and then 24.
Insertion Sort
6 10 24
12
36
![Page 23: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/23.jpg)
23
6 10 24
Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted.
To insert 12, we need to make room for it by moving first 36 and then 24.
Insertion Sort
36
12
![Page 24: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/24.jpg)
24
Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted.
To insert 12, we need to make room for it by moving first 36 and then 24.
Insertion Sort
6 10 24 36
12
![Page 25: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/25.jpg)
25
Works like someone who “inserts” one more card at a time into a hand of cards that are already sorted.
To insert 12, we need to make room for it by moving first 36 and then 24.
Insertion Sort
6 10 12 24 36
![Page 26: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/26.jpg)
template <class ItemType >void InsertItem ( ItemType values [ ] , int start , int end )
// Post: Elements between values [start] and values [end]// have been sorted into ascending order by key.{
bool finished = false ;int current = end ;bool moreToSearch = ( current != start ) ;
while ( moreToSearch && !finished ){
if (values [ current ] < values [ current - 1 ] ) {
Swap ( values [ current ], values [ current - 1 ] ) ; current-- ; moreToSearch = ( current != start );}else finished = true ;
}
} 26
![Page 27: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/27.jpg)
template <class ItemType >void InsertionSort ( ItemType values [ ] , int numValues )
// Post: Sorts array values[0 . . numValues-1 ] into ascending // order by key{
for ( int count = 0 ; count < numValues ; count++ )
InsertItem ( values , 0 , count ) ;
}
27
![Page 28: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/28.jpg)
Sorting Algorithms and Average Case Number of Comparisons
Simple Sorts Straight Selection Sort Bubble Sort Insertion Sort
More Complex Sorts Quick Sort Merge Sort Heap Sort
O(N2)
O(N*log N)
28
![Page 29: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/29.jpg)
29
Recall that . . .
A heap is a binary tree that satisfies these
special SHAPE and ORDER properties:
Its shape must be a complete binary tree.
For each node in the heap, the value stored in that node is greater than or equal to the value in each of its children.
![Page 30: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/30.jpg)
30
70
60
40 30
12
8
root
The largest element
in a heap is always found in the root node
10
![Page 31: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/31.jpg)
31
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
70
60
12
40
30
8
10
values
70
0
60
1
40
3
30
4
12
2
8
5
root
10
6
The heap can be stored in an array
![Page 32: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/32.jpg)
32
Heap Sort Approach
First, make the unsorted array into a heap by satisfying the order property. Then repeat the steps below until there are no more unsorted elements.
Take the root (maximum) element off the heap by swapping it into its correct place in the array at the end of the unsorted elements.
Reheap the remaining unsorted elements. (This puts the next-largest element into the root position).
![Page 33: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/33.jpg)
33
After creating the original heap
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
70
60
12
40
30
8
10
values
70
0
60
1
40
3
30
4
12
2
8
5
root
10
6
![Page 34: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/34.jpg)
34
Swap root element into last place in unsorted array
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
70
60
12
40
30
8
10
values
70
0
60
1
40
3
30
4
12
2
8
5
root
10
6
![Page 35: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/35.jpg)
35
After swapping root element into its place
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
10
0
60
1
40
3
30
4
12
2
8
5
root
70
6
10
60
12
40
30
8
70 NO NEED TO CONSIDER AGAIN
![Page 36: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/36.jpg)
36
After reheaping remaining unsorted elements
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
60
0
40
1
10
3
30
4
12
2
8
5
root
70
6
60
40
12
10
30
8
70
![Page 37: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/37.jpg)
37
Swap root element into last place in unsorted array
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
60
0
40
1
10
3
30
4
12
2
8
5
root
70
6
60
40
12
10
30
8
70
![Page 38: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/38.jpg)
38
After swapping root element into its place
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
8
0
40
1
10
3
30
4
12
2
root
70
6
8
40
12
10
30
60
70 NO NEED TO CONSIDER AGAIN
60
5
![Page 39: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/39.jpg)
39
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
40
0
30
1
10
3
6
4
12
2
root
70
6
40
30
12
10
6
60
70
60
5
After reheaping remaining unsorted elements
![Page 40: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/40.jpg)
40
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
40
0
30
1
10
3
6
4
12
2
root
70
6
40
30
12
10
6
60
70
60
5
Swap root element into last place in unsorted array
![Page 41: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/41.jpg)
41
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
6
0
30
1
10
3
12
2
root
70
6
60
5
After swapping root element into its place
40
4
6
30
12
10
40
60
70 NO NEED TO CONSIDER AGAIN
![Page 42: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/42.jpg)
42
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
30
0
10
1
6
3
12
2
root
70
6
60
5
40
4
30
10
12
6
40
60
70
After reheaping remaining unsorted elements
![Page 43: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/43.jpg)
43
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
30
0
10
1
6
3
12
2
root
70
6
60
5
40
4
30
10
12
6
40
60
70
Swap root element into last place in unsorted array
![Page 44: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/44.jpg)
44
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
6
0
10
1
12
2
root
70
6
60
5
40
4
After swapping root element into its place
6
10
12
30
40
60
70
30
3 NO NEED TO CONSIDER AGAIN
![Page 45: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/45.jpg)
45
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
12
0
10
1
6
2
root
70
6
60
5
40
4
12
10
6
30
40
60
70
30
3
After reheaping remaining unsorted elements
![Page 46: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/46.jpg)
46
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
12
0
10
1
6
2
root
70
6
60
5
40
4
12
10
6
30
40
60
70
30
3
Swap root element into last place in unsorted array
![Page 47: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/47.jpg)
47
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
6
0
10
1
root
70
6
60
5
40
4
30
3
After swapping root element into its place
NO NEED TO CONSIDER AGAIN
12
2
6
10
12
30
40
60
70
![Page 48: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/48.jpg)
48
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
10
0
6
1
root
70
6
60
5
40
4
30
3
12
2
10
6
12
30
40
60
70
After reheaping remaining unsorted elements
![Page 49: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/49.jpg)
49
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
10
0
6
1
root
70
6
60
5
40
4
30
3
12
2
10
6
12
30
40
60
70
Swap root element into last place in unsorted array
![Page 50: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/50.jpg)
50
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
values
root
70
6
60
5
40
4
30
3
12
2
After swapping root element into its place
6
10
12
30
40
60
70
10
1
6
0
ALL ELEMENTS ARE SORTED
![Page 51: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/51.jpg)
template <class ItemType >void HeapSort ( ItemType values [ ] , int numValues )
// Post: Sorts array values[ 0 . . numValues-1 ] into ascending // order by key{
int index ; // Convert array values[ 0 . . numValues-1 ] into a heap.
for ( index = numValues/2 - 1 ; index >= 0 ; index-- )
ReheapDown ( values , index , numValues - 1 ) ;
// Sort the array.
for ( index = numValues - 1 ; index >= 1 ; index-- ){
Swap ( values [0] , values [index] );
ReheapDown ( values , 0 , index - 1 ) ;}
}
51
![Page 52: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/52.jpg)
52
template< class ItemType >void ReheapDown ( ItemType values [ ], int root, int bottom )
// Pre: root is the index of a node that may violate the heap // order property// Post: Heap order property is restored between root and bottom
{ int maxChild ; int rightChild ; int leftChild ;
leftChild = root * 2 + 1 ; rightChild = root * 2 + 2 ;
52
ReheapDown
![Page 53: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/53.jpg)
if ( leftChild <= bottom ) // ReheapDown continued {
if ( leftChild == bottom ) maxChild = leftChild;else{ if (values [ leftChild ] <= values [ rightChild ] )
maxChild = rightChild ; else
maxChild = leftChild ;}if ( values [ root ] < values [ maxChild ] ){ Swap ( values [ root ] , values [ maxChild ] ) ; ReheapDown ( maxChild, bottom ) ;}
}}
53
![Page 54: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/54.jpg)
54
Heap Sort: How many comparisons?
24
0
60
1
30
3
40
4
12
2
8
5
root
10
6
15
7
6
8
18
9
In reheap down, an elementis compared with its 2 children (and swapped with the larger). But only one element ateach level makesthis comparison,and a completebinary tree with N nodes has only O(log2N)levels. 70
10
![Page 55: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/55.jpg)
55
Heap Sort of N elements: How many comparisons?
(N/2) * O(log N) compares to create original heap
(N-1) * O(log N) compares for the sorting loop
= O ( N * log N) compares total
![Page 56: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/56.jpg)
56
Using quick sort algorithm
A . . Z
A . . L M . . Z
A . . F G . . L M . . R S . . Z
![Page 57: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/57.jpg)
// Recursive quick sort algorithm
template <class ItemType >void QuickSort ( ItemType values[ ] , int first , int last )
// Pre: first <= last// Post: Sorts array values[ first . . last ] into ascending order{
if ( first < last ) // general case
{ int splitPoint ;
Split ( values, first, last, splitPoint ) ;
// values [ first ] . . values[splitPoint - 1 ] <= splitVal// values [ splitPoint ] = splitVal// values [ splitPoint + 1 ] . . values[ last ] > splitVal
QuickSort( values, first, splitPoint - 1 ) ;QuickSort( values, splitPoint + 1, last );
}
} ;
57
![Page 58: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/58.jpg)
58
Before call to function Split
values[first] [last]
splitVal = 9
GOAL: place splitVal in its proper position with
all values less than or equal to splitVal on its left
and all larger values on its right
9 20 6 18 14 3 60 11
![Page 59: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/59.jpg)
59
After call to function Split
values[first] [last]
splitVal = 9
smaller values larger values
in left part in right part
6 3 9 18 14 20 60 11
splitVal in correct position
![Page 60: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/60.jpg)
60
Quick Sort of N elements: How many comparisons?
N For first call, when each of N elements is compared to the split value
2 * N/2 For the next pair of calls, when N/2 elements in each “half” of the originalarray are compared to their own split values.
4 * N/4 For the four calls when N/4 elements in each“quarter” of original array are compared to their own split values.
.
.
. HOW MANY SPLITS CAN OCCUR?
![Page 61: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/61.jpg)
61
Quick Sort of N elements:How many splits can occur?
It depends on the order of the original array elements!
If each split divides the subarray approximately in half, there will be only log2N splits, and QuickSort is O(N*log2N).
But, if the original array was sorted to begin with, the recursive calls will split up the array into parts of unequal length, with one part empty, and theother part containing all the rest of the array except for split value itself. In this case, there can be as many as N-1 splits, and QuickSort is O(N2).
![Page 62: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/62.jpg)
62
Before call to function Split
values[first] [last]
splitVal = 9
GOAL: place splitVal in its proper position with
all values less than or equal to splitVal on its left
and all larger values on its right
9 20 26 18 14 53 60 11
![Page 63: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/63.jpg)
63
After call to function Split
values[first] [last]
splitVal in correct position
splitVal = 9
no smaller values larger values
empty left part in right part with N-1 elements
9 20 26 18 14 53 60 11
![Page 64: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/64.jpg)
64
Merge Sort Algorithm
Cut the array in half.
Sort the left half.
Sort the right half.
Merge the two sorted halves into one sorted array.
[first] [middle] [middle + 1] [last]
74 36 . . . 95 75 29 . . . 52
36 74 . . . 95 29 52 . . . 75
![Page 65: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/65.jpg)
// Recursive merge sort algorithm
template <class ItemType >void MergeSort ( ItemType values[ ] , int first , int last )
// Pre: first <= last// Post: Array values[ first . . last ] sorted into ascending order.{
if ( first < last ) // general case
{ int middle = ( first + last ) / 2 ;
MergeSort ( values, first, middle ) ;
MergeSort( values, middle + 1, last ) ;
// now merge two subarrays// values [ first . . . middle ] with // values [ middle + 1, . . . last ].
Merge( values, first, middle, middle + 1, last ) ;}
} 65
![Page 66: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/66.jpg)
66
Using Merge Sort Algorithm with N = 16
16
8 8
4 4 4 4
2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
![Page 67: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/67.jpg)
67
Merge Sort of N elements: How many comparisons?
The entire array can be subdivided into halves only log2N times.
Each time it is subdivided, function Merge is calledto re-combine the halves. Function Merge uses a temporary array to store the merged elements. Merging is O(N) because it compares each element in the subarrays.
Copying elements back from the temporary array to the values array is also O(N).
MERGE SORT IS O(N*log2N).
![Page 68: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/68.jpg)
68
Merge Sort are used on external data
When data is too big to fit into memory, we can still apply thea Merge Sort an algorithm. We often refer to this as the 3-Tapesort, because it uses 3 sequences of numbers and repeatedlyapplies a split/merge strategy.
Since the “3 tapes” are external, the size of the data to be sortedis not limited by the memory available to do an internal sort.
Even though the algorithm gives good O(N*log2N)performance the reading and writing to external files is magnitudes slower so elapsed time is not very good.
![Page 69: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/69.jpg)
69
Merge Sort (3-Tape Sort)
A file stored on a tape, c, has the following keys.
c- 2 12 17 16 14 30 15 7 50 65 20 32 48 58 16 20 19 10 35 45 11
Distribute - each subfile has sorted runs of length 1
a - 2 | 17 | 14 | 15 | 50 | 20 | 48 | 16 | 19 | 35 | 11b - 12 | 16 | 30 | 7 | 65 | 32 | 58 | 20 | 10 | 45 |
Mergec - 2 12|16 17|14 30|7 15|50 65|20 32|48 58|16 20|10 19|35 45|11
![Page 70: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/70.jpg)
70
Merge Sort (3-Tape Sort)
Repeat the distribute/merge step on the new file
c - 2 12|16 17|14 30|7 15|50 65|20 32|48 58|16 20|10 19|35 45|11
Distribute - each subfile has sorted runs of length 2
a - 2 12 | 14 30 | 50 65 | 48 58 | 10 19 | 11 b - 16 17 | 7 15 | 20 32 | 16 20 | 35 45 |
Mergec - 2 12 16 17| 7 14 15 30|20 32 50 65|16 20 48 58|10 19 35 45|11
![Page 71: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/71.jpg)
71
Merge Sort (3-Tape Sort)
Repeat the distribute/merge step on the new file
c - 2 12 16 17| 7 14 15 30|20 32 50 65|16 20 48 58|10 19 35 45|11
Distribute - each subfile has sorted runs of length 4
a - 2 12 16 17 | 20 32 50 65 | 10 19 35 45 b - 7 14 15 30 | 16 20 48 58 | 11
Mergec - 2 7 12 14 15 16 17 30|16 20 20 32 48 50 58 65|10 11 19 35 45
![Page 72: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/72.jpg)
72
Merge Sort (3-Tape Sort)
Repeat the distribute/merge step on the new file
c - 2 7 12 14 15 16 17 30|16 20 20 32 48 50 58 65|10 11 19 35 45
Distribute - each subfile has sorted runs of length 8
a - 2 7 12 14 15 16 17 30 | 10 11 19 35 45 b - 16 20 20 32 48 50 58 65
Merge to tape c2 7 12 14 15 16 16 17 20 20 30 32 48 50 58 65 | 10 11 19 35 45
![Page 73: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/73.jpg)
73
Merge Sort (3-Tape Sort)Repeat the distribute/merge step on the new file
2 7 12 14 15 16 16 17 20 20 30 32 48 50 58 65 | 10 11 19 35 45
Distribute - each subfile has sorted runs of length 16
a - 2 7 12 14 15 16 17 30 16 20 20 32 48 50 58 65 | b - 10 11 19 35 45
Merge to tape c
2 7 10 11 12 14 15 16 16 17 19 20 20 30 32 35 45 48 50 58 65
Done
![Page 74: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/74.jpg)
74
RADIX Sort
In the RADIX sort, keys are order by each character, or digit,in the sort key starting at the left and moving to the right oneposition on each pass.
A bucket is set aside to hold each possible value that can appearanywhere in the key. As each key is processed, it is placed in thebucket corresponding to the value of the current componentof the key.
For example the list:
762, 124, 432, 761, 800, 402, 976, 100, 001, 999
![Page 75: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/75.jpg)
75
RADIX Sort
0
1
2
4
6
9
001761
402762 432
100800
124
976
999
762, 124, 432, 761, 800, 402, 976, 100, 001, 999
![Page 76: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/76.jpg)
76
RADIX Sort Reconnect and distribute on second character of key 800, 100, 761, 001, 762, 432, 402, 124, 976, 999
0
2
3
6
7
9
001
761
402
762
432
100800
124
976
999
![Page 77: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/77.jpg)
77
RADIX Sort Reconnect and distribute on second character of key 800, 100, 001, 402, 124, 432, 761, 762, 976, 999
0
1
4
7
8
9
432
761
999
762
402
124
001
100
800
976
![Page 78: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/78.jpg)
78
RADIX Sort Reconnect and we are Done001,100,124, 402, 432, 761, 762, 800, 976, 999
0
1
4
7
8
9
432
761
999
762
402
124
001
100
800
976
![Page 79: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/79.jpg)
79
Searching and Performance
Linear Searching is O(N).
BinarySearch is O(log2N).
Hash Search is O(1).
![Page 80: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/80.jpg)
80
Function LinearSearch( )
LinearSearch takes an array (may be unsorted) info, and searches from the first location until the item is found or the last element is reached
LinearSearch is O(N).
![Page 81: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/81.jpg)
81
found = LinearSearch(info, 20, 14);
item sizeindexes
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
info 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28
NOTE: denotes element examined
![Page 82: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/82.jpg)
82
Function BinarySearch( )
BinarySearch takes sorted array info, and two subscripts, fromLoc and toLoc, and item as arguments. It returns false if item is not found in the elements info[fromLoc…toLoc]. Otherwise, it returns true.
BinarySearch is O(log2N).
![Page 83: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/83.jpg)
83
found = BinarySearch(info, 25, 0, 14 );
item fromLoc toLocindexes
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
info 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28
16 18 20 22 24 26 28
24 26 28
24 NOTE: denotes element examined
![Page 84: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/84.jpg)
template<class ItemType>bool BinarySearch ( ItemType info[ ] , ItemType item , int fromLoc , int toLoc )
// Pre: info [ fromLoc . . toLoc ] sorted in ascending order // Post: Function value = ( item in info [ fromLoc . . toLoc] )
{ int mid ;if ( fromLoc > toLoc ) // base case -- not found
return false ; else {
mid = ( fromLoc + toLoc ) / 2 ;
if ( info [ mid ] == item ) // base case-- found at mid
return true ;
else if ( item < info [ mid ] ) // search lower half return BinarySearch ( info, item, fromLoc, mid-1 ) ; else // search upper half
return BinarySearch( info, item, mid + 1, toLoc ) ; }
} 84
![Page 85: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/85.jpg)
85
Hashing
is a means used to order and access elements in a list quickly -- the goal is O(1) time -- by using a function of the key value to identify its location in the list.
The function of the key value is called a hash function.
FOR EXAMPLE . . .
![Page 86: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/86.jpg)
86
Using a hash function
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
. . .
Empty
4501
Empty
8903
8
10
values
[ 97]
[ 98]
[ 99]
7803
Empty
.
.
.
Empty
2298
3699
HandyParts company makes no more than 100 different parts. But theparts all have four digit numbers.
This hash function can be used tostore and retrieve parts in an array.
Hash(key) = partNum % 100
![Page 87: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/87.jpg)
87
Placing elements in the array
Use the hash function
Hash(key) = partNum % 100
to place the element with
part number 5502 in the
array.
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
. . .
Empty
4501
Empty
8903
8
10
values
[ 97]
[ 98]
[ 99]
7803
Empty
.
.
.
Empty
2298
3699
![Page 88: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/88.jpg)
88
Placing elements in the array
Next place part number6702 in the array.
Hash(key) = partNum % 100
6702 % 100 = 2
But values[2] is already occupied.
COLLISION OCCURS
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
. . .
values
[ 97]
[ 98]
[ 99]
7803
Empty
.
.
.
Empty 2298
3699
Empty
4501
5502
![Page 89: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/89.jpg)
89
How to resolve the collision?
One way is by linear probing.This uses the rehash function
(HashValue + 1) % 100
repeatedly until an empty locationis found for part number 6702.
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
. . .
values
[ 97]
[ 98]
[ 99]
7803
Empty
.
.
.
Empty
2298
3699
Empty
4501
5502
![Page 90: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/90.jpg)
90
Resolving the collision
Still looking for a place for 6702using the function
(HashValue + 1) % 100
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
. . .
values
[ 97]
[ 98]
[ 99]
7803
Empty
.
.
.
Empty
2298
3699
Empty
4501
5502
![Page 91: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/91.jpg)
91
Collision resolved
Part 6702 is placed atthe location with index 4.
Where would the part withnumber 4598 be placed usinglinear probing?
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
. . .
values
[ 97]
[ 98]
[ 99]
7803
6702
.
.
.
Empty
2298
3699
Empty
4501
5502
![Page 92: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/92.jpg)
92
Collision resolved
There are many Collision resolution strategies.
Examples:
Rehashing - applies a second (or more) Hash Function(s)to the previous hash result.
Folding - breaking the key into parts, combining theparts, and then applying the Hash Function.
Chains and buckets - Create a linked list headed by a arrayof hash values.
![Page 93: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/93.jpg)
93
Folding
Key is social security number
200-12-1234 200
121234--------1446
hash value = H(1446)
![Page 94: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/94.jpg)
94
Chains and Buckets
Create a linked list headedby an array of hash values.
This provides one of the best overallCollision Resolution strategies.
![Page 95: 1 Data Structures and Algorithms Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert.](https://reader036.fdocuments.net/reader036/viewer/2022062308/56649e745503460f94b74565/html5/thumbnails/95.jpg)
95
Collision resolved
Part 6702 can be placed atthe location with index 4.
[ 0 ]
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
. . .
values
[ 97]
[ 98]
[ 99]
6702 2008
Create new node and add to linked list.