C Plus Data Structures 4 Ed
C Plus Data Structures 4 Ed
discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/220691201
CITATIONS
READS
1,265
1 author:
Nell B. Dale
University of Texas at Austin
81 PUBLICATIONS 324 CITATIONS
SEE PROFILE
Sorting means . . .
l
36
[1]
24
[2]
10
[3]
[4]
12
36
[1]
24
[2]
10
[3]
[4]
12
U
N
S
O
R
T
E
D
4
[1]
24
[2]
10
[3]
[4]
36
12
SORTED
U
N
S
O
R
T
E
D
5
[1]
24
[2]
10
[3]
[4]
36
12
SORTED
U
N
S
O
R
T
E
D
6
6
10
[2]
24
[3]
36
[4]
SORTED
12
U
N
S
O
R
T
E
D
7
6
10
[2]
24
[3]
36
[4]
SORTED
12
U
N
S
O
R
T
E
D
8
[1]
10
[2]
12
[3]
36
[4]
S
O
R
T
E
D
UNSORTED
24
9
[1]
10
[2]
12
[3]
36
[4]
S
O
R
T
E
D
UNSORTED
24
10
[1]
10
[2]
12
[3]
24
[4]
S
O
R
T
E
D
36
11
Selection Sort:
How many comparisons?
values [ 0 ]
[1]
10
[2]
12
[3]
24
36
= 4 + 3 + 2 + 1
[4]
12
13
Notice that . . .
Sum = (N-1) + (N-2) + . . . +
+ Sum =
+ . . . + (N-2) + (N-1)
2* Sum =
+ ...
2 * Sum =
N * (N-1)
Sum =
N * (N-1)
2
+ N
14
17
Bubble Sort
values [ 0 ]
36
[1]
24
[2]
10
[3]
[4]
6
12
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.
18
19
20
Insertion Sort
values [ 0 ]
36
[1]
24
[2]
10
[3]
[4]
6
12
Insertion Sort
6 10 24 36
12
22
Insertion Sort
6 10 24
36
12
23
Insertion Sort
6 10
24 3
6
12
24
Insertion Sort
12 24
0
1
6
36
25
26
27
Sorting Algorithms
and Average Case Number of Comparisons
Simple Sorts
n
n
n
O(N2)
Quick Sort
Merge Sort
Heap Sort
O(N*log N)
28
Recall that . . .
A heap is a binary tree that satisfies these
special SHAPE and ORDER properties:
n
29
12
60
40
30
10
30
70
[1]
60
[2]
[3]
root
70
0
12
40
60
12
[4]
30
40
30
10
[5]
[6]
10
31
70
[1]
60
[2]
[3]
root
70
0
12
40
60
12
[4]
30
40
30
10
[5]
[6]
10
33
70
[1]
60
[2]
[3]
root
70
0
12
40
60
12
[4]
30
40
30
10
[5]
[6]
10
34
10
[1]
60
[2]
[3]
root
10
0
12
40
60
12
[4]
30
40
30
70
[5]
[6]
70
60
[1]
40
[2]
[3]
root
60
0
12
10
40
12
[4]
30
10
30
70
[5]
[6]
70
36
60
[1]
40
[2]
[3]
root
60
0
12
10
40
12
[4]
30
10
30
70
[5]
[6]
70
37
[1]
40
[2]
[3]
root
8
0
12
10
40
12
[4]
30
10
30
60
70
[5]
60
[6]
70
40
[1]
30
[2]
[3]
root
40
0
12
10
30
12
[4]
10
60
70
[5]
60
[6]
70
39
40
[1]
30
[2]
[3]
root
40
0
12
10
30
12
[4]
10
60
70
[5]
60
[6]
70
40
[1]
30
[2]
[3]
root
6
0
12
10
30
12
[4]
40
10
40
60
70
[5]
60
[6]
70
30
[1]
10
[2]
[3]
root
30
0
12
6
10
12
[4]
40
40
60
70
[5]
60
[6]
70
42
30
[1]
10
[2]
[3]
root
30
0
12
6
10
12
[4]
40
40
60
70
[5]
60
[6]
70
43
[1]
10
[2]
[3]
root
6
0
12
30
10
12
[4]
40
30
40
60
70
[5]
60
[6]
70
12
[1]
10
[2]
[3]
root
12
0
6
30
10
[4]
40
30
40
60
70
[5]
60
[6]
70
45
12
[1]
10
[2]
[3]
root
12
0
6
30
10
[4]
40
30
40
60
70
[5]
60
[6]
70
46
[1]
10
[2]
[3]
root
6
0
12
30
10
12
[4]
40
30
40
60
70
[5]
60
[6]
70
10
[1]
[2]
[3]
root
10
0
12
30
12
[4]
40
30
40
60
70
[5]
60
[6]
70
48
10
[1]
[2]
[3]
root
10
0
12
30
12
[4]
40
30
40
60
70
[5]
60
[6]
70
49
[1]
10
[2]
[3]
root
6
0
12
30
10
12
[4]
40
30
40
60
70
[5]
60
[6]
70
51
ReheapDown
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
Heap Sort:
How many comparisons?
In reheap down, an element
is compared with its 2
children (and swapped
with the larger). But
only one element at
each level makes
this comparison,
and a complete
binary tree with
30
N nodes has
3
only O(log2N)
levels.
15
6
7
root
24
0
60
12
2
40
10
18
70
10
54
55
A..Z
A..L
A..F
M..Z
G..L
M..R
S..Z
56
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
9
values[first]
20
18
14
60
11
[last]
58
larger values
in right part
18
14
20
values[first]
60
11
[last]
2 * N/2
4 * N/4
.
.
.
9
values[first]
20
26
18
14
53
60
11
[last]
62
20
26
larger values
in right part with N-1 elements
18
14
values[first]
53
60
11
[last]
36
...
[first]
36
74
95
75
29
...
[middle] [middle + 1]
...
95
29
52
52
[last]
...
75
64
}
}
65
4
2
1 1
4
2
1 1
2
1 1
4
2
2
1
4
2
2
1
1 1 1
66
Function BinarySearch( )
l
BinarySearch is O(log2N).
68
fromLoc
toLoc
indexes
0
10
11
12
13
14
10
12
14
16
18
20
22
24
26
28
16
18
20
22
24
26
28
24
26
28
info
24
NOTE:
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 )
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 ) ;
}
}
70
Hashing
l
Empty
[1]
4501
[2]
Empty
[3]
8903
7803
[4]
.
.
.
Empty
.8
.
10
.
[ 97]
Empty
[ 98]
2298
[ 99]
3699
HandyParts company
makes no more than 100
different parts. But the
parts all have four digit numbers.
This hash function can be used to
store and retrieve parts in an array.
Hash(key) = partNum % 100
72
Empty
[1]
4501
[2]
Empty
[3]
8903
7803
[4]
.
.
.
Empty
.8
.
10
.
[ 97]
Empty
[ 98]
2298
[ 99]
3699
73
Empty
[1]
4501
[2]
5502
[3]
[4]
.
.
.
7803
Empty
.
.
.
[ 97]
Empty
[ 98]
2298
[ 99]
3699
6702 % 100 = 2
But values[2] is already
occupied.
COLLISION OCCURS
74
Empty
[1]
4501
[2]
5502
[3]
[4]
.
.
.
7803
Empty
.
.
.
[ 97]
Empty
[ 98]
2298
[ 99]
3699
75
Empty
[1]
4501
[2]
5502
[3]
[4]
.
.
.
7803
Empty
.
.
.
[ 97]
Empty
[ 98]
2298
[ 99]
3699
76
Collision resolved
values
[0]
Empty
[1]
4501
[2]
5502
[3]
[4]
.
.
.
7803
Empty
.
.
.
[ 97]
Empty
[ 98]
2298
[ 99]
3699
77
Collision resolved
values
[0]
Empty
[1]
4501
[2]
5502
[3]
[4]
.
.
.
7803
6702
.
.
.
[ 97]
Empty
[ 98]
2298
[ 99]
3699
78