0% found this document useful (0 votes)
15 views

Data Structures-Unit 5 Notes (2) - Compressed

Uploaded by

Ns
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Data Structures-Unit 5 Notes (2) - Compressed

Uploaded by

Ns
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 112

TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge


Array
0 1 2 3 4 5 6 7
– 11 33 37 42 45 99 19
40 0

Sub list 1 Sub list 2


Middle
eleme
nt

Arra
y 6 7
4 5
45 99 19
0
Sub Sub
list list
Middle
element

Array
4 5 6 7
45 99 190
Key = 99
a 045
1 –40
2 19
0
3
99
4
11
a
0 45
 1 –
2 40
19
3
0
 4 99
a
0 –40
1 45
 19
2
3 0
99
4
11
a
0 –40
1 45
 19
2
0
3
99
4 11

a –40
0 45
 1
99
2 190
3 11
4
a
0 –
1 40
45
2
99
3 11
4 19
a
0 –
1 40
45
2
99
3
11
4
19
a
0 –40
1 45
2 99
3 11
4 19
0
 a
0 –40
1 45
2 99
11
3 19
0
4
a
0 –40
1 45
 2 11
3 99
19
 4 0

a
0 –40
1 45
2 11
3 99
4 19
0
a
0 –
1 40
2 11
45
3
99
4 19

a
0 –
1 40
2 11
45
3
99
4 19




0 1 2 3 4 5 6 7 A s 1 < 52
 Interchange 52 1 27 85 66 23 13 57

0 1 2 3 4 5 6 7 As 27 < 52
 Interchange 1 52 27 85 66 23 13 57

0 1 2 3 4 5 6 7 As 52 < 85
 No Interchange 1 27 52 85 66 23 13 57

0 1 2 3 4 5 6 7 As 66 < 85
 Interchange 1 27 52 85 66 23 13 57

0 1 2 3 4 5 6 7 As 23 < 85
 Interchange 1 27 52 66 85 23 13 57

0 1 2 3 4 5 6 7 As 13 < 85
 Interchange 1 27 52 66 23 85 13 57

0 1 2 3 4 5 6
7 As 57 < 85
1 27 52 66 23 13 85 57 Interchange

0 1 2 3 4 5 6 7
1 27 52 66 23 13 57 85
0 1 2 3 4 5 6 7 As 23 < 66
 Interchange 1 27 52 66 23 13 57 85

0 1 2 3 4 5 6 7 As 13 < 66
 Interchange 1 27 52 23 66 13 57 85

0 1 2 3 4 5 6 7 As 57 < 66
 Interchange 1 27 52 23 13 66 57 85

0 1 2 3 4 5 6 7
1 27 52 23 13 57 66 85 No Interchange

0 1 2 3 4 5 6 7 As 23 < 52
 Interchange 1 27 52 23 13 57 66 85

0 1 2 3 4 5 6 7 As 13 < 52
 Interchange 1 27 23 52 13 57 66 85

0 1 2 3 4 5 6 7
1 27 23 13 52 57 66 85

0 1 2 3 4 5 6 7 As 23 < 27
1 27 23 13 52 57 66 85  Interchange

0 1 2 3 4 5 6 7 As 13 < 27
1 23 27 13 52 57 66 85  Interchange

0 1 2 3 4 5 6 7
1 23 13 27 52 57 66 85
0 1 2 3 4 5 6 7 As 13 < 23
 Interchange 1 23 13 27 52 57 66 85

0 1 2 3 4 5 6 7
1 13 23 27 52 57 66 85

0 1 2 3 4 5 6 7
1 13 23 27 52 57 66 85

1 13 23 27 52 57 66 85

 

Last ni elements


A[0]  A[1]  A[i 1] A[i], , A[k], , A[n 1]
 

1 st


Scan the array for finding smallest element

 

2 nd


Scan the array for finding
smallest element

 
3 rd


Smallest element
is searched in
this list

4 th


Smallest element is
searched in this list

 

5 th

 Search
smallest
element in this
list

 
6 th

 

0 1 2 3 4 5 6 Compare 70
30 70 20 50 40 10 60 with 30
and insert
Sorte it at its
Unsorted zone
d position
zone
0 1 2 3 4 5 6
30 70 20 50 40 10 60 Compare
20 with
Unsorted zone the
Sorte
d elements
zone in sorted
zone and
insert it
0 1 2 3 4 5 6 in that zone at
appropriate
20 30 70 50 40 10 60 position

Sorte Unsorted zone


d
zone

0 1 2 3 4 5 6
20 30 50 70 40 10 60

Sorted zone Unsorted zone

0 1 2 3 4 5 6
20 30 40 50 70 10 60
Sorted zone Unsorte
d
zone
0 1 2 3 4 5 6
60

Sorted Unsorte
zone d
zone
0 1 2 3 4 5 6
10 20 30 40 50 70

10 20 30 40 50 60 70

Sorted list
of
elements




O (n 2 )
0 1 2 3 4
30 20 10 40 50 Initially it
enters in outer
j i for loop temp =
A [i]
temp j=i–1
20

0 1 2 3 4
30 30 10 40 50
j = –1 i A[j+1]=
A[j]
tem
p
20

0 1 2 3 4
20 30 10 40 50
j = –1 i

temp it gets
20 copied
at
A[j+1
0 1 2 3 4
20 30 10 40 50 Again for loop
gets executed
This and
much list set i =2, temp = A[i]
gets a n d j = i –1
sorted.

It moves to while
20 30 10 40 50 loop
j As j > = 0 and
(A [ j] > temp) is true, the while loop
i gets executed.
temp
10

i
20 30 A[j30 +1]=40A [ j ] 50
I
j j = j –1
j gets decremented
temp
10

0 1 2 3
4
20 20 30 40 50 A[j +1]= A [ j ]
II
j = j –1
j
temp
10

0 1 2 3
4 As j < 0, it
10 20 30 40 50
comes out of
III while
j i loop
A[ j +1] =
temp
temp gets
10 copie
d

Thus,
0 1 2 3 This much
10 4
20 30 40 50
l
i
s
t Again for loop
gets
g executed.
e set i =
t 3, temp
s =A[i]
j = i –1
s
o
r
t
e
d
0 1 2 3 4
10 20 30 40 50 It moves to while loop
As A [ j] >
j i temp is false ,
temp while loop
will not get executed.
40

0 1 2 3 4
10 20 30 40 50
j
tem gets
p copie
10 d
(i.e. no change!)

Then
, 0 1 2 3 4
Again for loop
10 20 30 40 50
gets executed
set i = 4
This much temp = A [
list gets i ] j = i –1
sorted

Then 0 1 2 3 4
, It moves to while
10 20 30 40 50
loop As A[ j ] >
j i temp is false, while
loop
temp
will not
50 get
executed
.

0 1 2 3 4
10 20 30 40 50

temp gets
copied
50

0 1 2 3 4
10 20 30 40 50

This much
list is
sorted
0 1 2 3 4 5 6 7
3 10 4 2 8 6 5 1

0 1 2 3 4 5 6 7
Compare 3 and 10
3 10 4 2 8 6 5 1
and insert the elements orderly

0 1 2 3 4 5 6 7
Compare 4
3 10 4 2 8 6 5 1

0 1 2 3 4 5 6 7
3 4 10 2 8 6 5 1

0 1 2 3 4 5 6 7
2 3 4 10 8 6 5 1

0 1 2 3 4 5 6 7
2 3 4 8 10 6 5 1

0 1 2 3 4 5 6 7
2 3 4 6 8 10 5 1

0 1 2 3 4 5 6 7
2 3 4 5 6 8 10 1

0 1 2 3 4 5 6 7

1 2 3 4 5 6 8 10
A[0] A[1 A[2 A[3] A[4 A[5 A[6] A[7
] ] ] ] ]
25 57 48 37 12 92 86 33


A[0] A[1 A[2 A[3] A[4 A[5 A[6] A[7
] ] ] ] ]
25 57 33 37 12 92 86 48

        
 


70 20 30 40 10 50 60

70Divide
20 30 40 Divide
10 50 60
Divide

70 20
Divide
Divide
30 40 10 50 60
70 20
Divide
Merge
30 40 10 50 60
20 70
Merge
Merge
30 10 40 50 60
20 30 70 10 40 50 60
Merge Merge
Merge

10 20 30 40 50 60 70












  

low high
0 1 2 3 4 5 6
70 20 30 40 10 50 60 Merge sort (A, low,
mid)
4
1 This list is further subdivided
This list can be subdivided

0 1 23
70 20 30 40

2 This list is subdivided


This 3list can be subdivided
Merge sort (A, mid+1, high)

70 20 30 40 10 50 60
7 This list
Combine 5can be subdivide
Combine two sublists in temp array

6 10 50 10
Combine
two
sublists
20 70 30 40 9 Combine two sublists

8 Combine these two sublists


10 50

20 30 40 70

10 50 60

11 Combine two
sublists

10 20 30 40 50 60 70
Array A (left Array A (right sublist)
Applicable part
sublist) of
10 50 60
20 30 40 70  Algorithm

i j

Initially k = 0. Then k will be incremented


temp 

10 
0
k


A (left sublist) A (right sublist) 
10 50 60
20
 30 40 70 
i j

Array A (right sublist)


Array A (left
Applicable part of Algorithm
sublist)
20 30 40 70 10 50 60

i j
k = 1. It is advanced later on temp

10 20 

if part of algorithm gets executed
01 
k
moves ahead

Array A (left sublist)Array A (right sublist) 




20 30
 40 70 10 50
 60
i j
Array A (left sublist)
Array A (right sublist)
20 30 40 70 10 50 60
  Applicable part of Algorithm
i j
Now k = 2
temp
10 20 30 
if part 
012 
gets executed
k
moves ahead

A (left sublist) A (right sublist) 


20 30 40 70 
10 50 60 
 
i j

A (left sublist) A (right sublist)


Applicable part of Algorithm
20 30 40 70 10 50 60
 
i j

temp 
10 20 30 40 


k


A (left sublist) A (right sublist) 
10 50 60 
20 30 40 70
 
i j
(left sublist) A (left sublist)
A (right sublist)
A (right sublist)
20 30 40 70 Applicable part of Algorithm
Applicable part of Algorithm
10 50 60
 
i  j 
i j
temp
temp s

10 20 30 40 50 


k


A (left sublist) A (right sublist) 
20 30 40 70 
10 50 60
 
i j

A (left sublist)
A (right sublist) Applicable part of Algorithm
20 30 40 70
10 50 60
 
i j
temp

10 20 30 40 50 60 
 
k


A (left sublist) A (right sublist) 

20 30 40 70
 
i j

A
10 50 60

20 30 40 70 10 50 60

10 20 30 40 50 60 70
We compare elements from left sublist and right sublist. If element in the left sublist is lesser than the element in the rig
We compare elements from left sublist and right sublist. If element in the right sublist is lesser than the elem

Reached at the end of right sublist and elements of left sublist are
remaining, then copy the remaining elements of left sublist to temp

Reached at the end of left sublist and elements of right sublist are remaining, then copy the remaining
elements of right sublist to temp
0 1 2 3 4 5 6 7 8
11 8 55 22 33 27 62 35 71

11 8 55 22 33 27 62 35 71

11 8 55 22 33 27 62 35 71

33 27 62
11 8 55 22 35 71

8 11 22 55 33 27
62 35 71

27
32 62

27 32 62
8 11 22 55 27 32 35 62 71

8 11 22 27 32 35 55 62 71
n  2
k

 2k  n 2k
2T  
 c 2 k
2
 
T (2 k) 2T (2 k  1)  c2 k

2T (2 k  1)  c2 k
T (2 k)

2 [2T (2 k  2 )  c 2 k  1]  c 2 k

2 2 T (2 k  2 )  2 c 2 k  1
 c 2 k

2k
2 2 T (2 k  2 )  2 c  c 2 k

2 2 T (2 k  2 )  c 2 k  c 2 k
T (2
k
) 2 2 T (2 k  2 )  2c 2 k

T(2 k) 2 3 T (2 k  3 )  3c 2 k

2 4 T (2 k  4 )  4c 2 k
2 k T (2 k  k)  kc 2 k

2 k T (20 )  kc 2 k
T(2 k) 2 k T (1)  kc 2 k

T (2 k) 2 k  0  k c 2 k
T (2 k) k c 2
k
2k
2 n

 log 2 n  cn

 (n  log 2 n
(n log 2 n)

n2 n2 n2

n2 n2 n2

n2 n2 n2

n2

n2
TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge


Po sition Key Record
496700 0 496700
0 0
1
2 842100
2
Position 3

395
495739 396 4618396
7
397 4957397
398
Position 399 1286399
in Hash 400
Table 401
402
403
404
405
406
407
408

990 0000990
991 0000991
992 1200992
993 0047993

994
9846995
995
4618996
4967997
996
997 0001999
998
999
Step 1: Insert 33 0 25 Step 3: Insert
25
33 mod 5 1
Hash =3
function 2 25 mod 5 = 0
Step 2: Insert 54 3 33 Bucket
54 4 54
mod5=4

If we want to insert 55 then


0 25
55 now
But at 0th location 25 is already placed and mod 55
5 =is0 demanding the same location.
1 Hence we say
2 ''collision occurs''
33
3
54
4
25%5=0 0 25
1 31
31% 5 = 1
2 42
42% 5 = 2 63
3
49
63% 5 = 3 4

49% 5 = 4
33% 5 = 3

Hash functions

Folding and universal method


Division Multiplication method
Extraction
Mid square method
method method
Hash Table
Hash Function
h(key) = record%table size 0
4 = 54%10
1

2 72

2 = 72%10
3
54
4
9 = 89%10
5

6
37
7 = 37%10
7

8 89
9

h(k) = m { kA }

Fractional part

h(k) = m 107 

66.1

0.12 Fractional part

h(k) = 50  0.12
=6
h(k) = 6
4 9 7 8 24

478 at 478 location in the


hash table of size 1000
the key can be stored.

2
 (3111)

9 6 7 8 3 2 1
Collision resolution techniques

Rehashing Extendible hashing


Separate chaining Open addressing

Chaining with replacement LinearQuadraticDouble probingprobinghashing


Chaining without replacement
0 39
Cluster
19%10 = 1 29
9
18%10 = 2 8
8
39%10 = 3
9
29%10 = 4
9
8%10 = 8 5

8 18

9 19
0 90
1 11

2 22

3
4
55
5
6 37
7 1
7
8
4
9 9
 0 90
1 11

2 22

3
4
55
5
8
6 7
7 37
17
8
49
9

H1

H1

H1

H1

H1
0 90

1 1
7
2 22
3
4
5 45
6
37
7
8
49
9

Index Keys Inde Keys


0 x
1 437 0
1 1 4371
2 2
3 3 1323
4 4
5 5
6 6
7 7
8 8
9 9

4371 mod 10 = 1 1323 mod 10 = 3

6173 mod 10 = 3 Index Keys


As collision occurs we will apply quadratic probing.
0 2
1 4371  H(key) = (H(key) + i )
2 Consider i = 0 2
3 1323  H (6173) = (3 + 0 ) % 10
= 3 collision
4 617
Hence consideri=1 2
3
H (6173) = (3 +1 ) % 10
5 = (3+ 1) % 10
6 H (6173) = 4
7 As index 4 is an
8 empty slot, we will
9 place 6173 at index
4.
Index Keys
0
1 4371
4199 mod 10 = 9 2
3 1323
4 6173 As this slot is empty
5 we will place 4199 at index 9
6
7
8
9 4199

4344 mod 10 = 4
KeysHence collision occurs.
Index slot.
But index 4 shows an occupied
Therefore we will place 4344
0 using quadratic probing.
2
1 4371  H(key) = (H(key) + i ) % m
2 H(key) = 4, i = 0, 1, 2, ...
3 1323 m = 10 2
4 6173  H (key) = (4 +0 ) % 10 = 4 collision
2
5 434 H (key) = (4 +1 ) % 10 = 5
4 The index 5 is an
6 empty slot. Hence we
7 will place 4344 at
8 index 5.
9 4199

9679 mod 10 = 9 collisionIndex Key


occurs.
0 967
nce we will place the element ing quadratic
9 H probing. 2
e
1 437 u H(key) = (H(key) + i ) % m
1 s H(key) = 9, i will be 0 or 1 or 2 ...
2 m = 10 2
3 132 H (key) = (9 +0 ) i=0
3 %7
4 617 =9
3 collision
2
5 434  H (key) = (9 + 1 ) % 10 = 0
4  Place 9679 at index 0
6
1989 mod 10 = 9 collision Index
occurs.Key
0 using
Hence we will place the element 9679 quadratic probing.
1 4371 2
2  H(key) = (H(key) + i ) % m.
H(key) = 9, hence
3 1323 2
4 6173  H (key) = (9 +0 ) i=0
% 10
5 4344
= 9 collision
6 2
7  H (key) = (9 +1 ) i=
% 10
8 1989
= 0 collision
9 4199 2 1
 H (key) = (9 +2 )
% 10
= 3 collison i=

2
 H (key) = (9 +3 ) % 10 i = 3
=8
Insert 1989 at index 8
0 20

2 22

3 13

24
4
15
5
16
6
14
7 Place 14 here
because 14 % 10 = 4.
8 The index 4 contain
19 the element 24.
9 This is a proper record at its place.
Hence to place 14 we have to move down
10 in search of an empty slot.

11

0 20

2 22

3 13

24
4
15
5
16
6
17
7 17 % 10 = 7. This position was
14 occupied by 14. But 14 is not the
proper record
8 19 at this place. Hence replace 14
by 17. Then place 14 at next
empty slot.
9

10

11
0 20

2 22

3 13

24
4
15
5
16
6 26 % 10 = 6. But 16 is a proper record at this place.
17
7
14
8
19
9
26
10 Hence at next empty location 26 is placed.

11

0 20

2 22

3 13

24
4 84 % 10 = 4. But 24 is occupying its own
15
location 5
16
6
17
7
14
8
19
9
26
10
84
11 Then by moving linearly down we can
place 84 at the empty location found.
0 20

1 96 As 96 % 10 = 6. But index 6 holds a record


16 which is correct for that location. By
22 moving down linearly
2 we get no empty slot. Hence we
13 roll back and get the empty slot at index 1.
3 Hence 96 will be replaced at index 1.
24
4
15
5 16
6 17
7 14

8 19

9 26

10 84

11

0 20

2 22

13
3
24
4
15
5
16
6
14 Collision occurs at
7 17 index 4. Hence
probing 14 at
8 19 the next empty slot.
9 At index 7, the 14 is already
26 placed.
10 Hence at next empty slot 17 is
placed.

11
26 % 10 = 6. The collision occurs.
Hence 26 is placed at next empty
slot.
0 20

1 96 96 % 10 = 6. But at location 6, the


element 16 is placed. Hence we go
22 linearly down in search
2 of an empty slot. But since table
13 gets full, we may not get an empty
slot. Therefore
3 24 roll back to search an empty
slot. At index 1, we can then
15 place 96.
4
16
5
14
6 17
7 19
8 26

9 84

10

11 84 % 10 = 4. But index 4 contains key


element
24. Hence by linear probing, at empty slot
11, the element 84 is placed.

1 28 19

10

2 20

3 12

4

5 5

6 15 33

8 17
0 10

1 28

2 19

3 20

4 12

5 5

6 15

33
7
17
8

1 28

3 19

4
5
5

8
0
10

1
28

2 20

19
3
17
4
5 
5
15
6
12
7
33

0
Transferring 1
the contents 2

9
Old
table

22
New table
Collision solved
by linear
probing,
by placing it at 8
2d
Local depth
Globa 2
l dep
th 4 8 12 Bucket
2 #1

00 2
01 1 5 21
10
Bucket
11
#2

2
Directory
6 10 Bucket
#3

2
7 15 19 Bucket
#4

Buckets
2
4 8 12 Bucket #1
2

00 2

1 5 21 Bucket #2
01

10
2
11
6 10 Bucket
#3

2
7 15 19 Bucket
#4

2
4 8 12 Bucket #1
2

00 2

1 5 21 Bucket #2
01

10
2
11
6 10 14 Bucket
#3

2
7 15 19 Bucket
#4
Note that depth
value is incremented by 1

8 24 Bucket #1
3
101
000 2
110
001
111
010

011

100

101

110 This is the split image of Bucket#1


Hence named it as Bucket#1
111

Directory

00
0

00
1

01
0

01
1

10
0
Directory
1 5 21 Bucket
#2

2
6 10 14 Bucket
#3

2
7 15 19 Bucket
#4

3
4 12 20 Bucket

8 24
3

2
1 5 21 Bucket
#2

2
6 10 14 18 Bucket
#3

2
7 15 19 Bucket
#4

3
4 12 20 Bucket
#1'
3

3 8 24 Bucket
#1

000 2

001 1 5 21 Bucket
#2
010
3
011 10 18 Bucket
#3
100
2
101
7 15 19 Bucket
110 #4

111 3

Directory 4 12 20 Bucket
#1'

3
2

2 4 8 12

00 2

01 1

10 2
11 6 10

2
7 15

2
2
4 8 12 Bucket # 1
00
01 2
10 6 10
Bucket # 2
11
2

7 15
Bucket # 3
Directory

00 4 8 12

01
2
10
7 15
11

2
48 12
0
2
1 7 15

You might also like