Professional Documents
Culture Documents
height=2 BF=1-0=1
1 6 0
4 9
0 0
1 5
4
44
2 3
17 78
1 2 1
32 50 88
1 1
48 62
Insert operation may cause balance factor to
become 2 or –2 for some node. We resort to
techniques called Rotations to restore balance
of the search tree.
• h(k) = k mod 4
k: 3205 7148 2345
k2 : 10 272 025 51 093 904 5 499 025
H(k) 72 93 99
Final array
0 1 2
3
The key is partitioned into a number of parts, k 1
….kr
Each part is to have the same number of digits.
These parts are added together,the last carry is
ignored and we get the hash function,
K
H(k)=k + k 3205 ;
+….+k 7148 2345
1 2 3
K1 32 71 23
K2 05 48 45
H(K) 37 19 68
• Again doing division method
• Let us take::
• h(k) = k mod 4
0 1 2
3
Suppose we have two keys k1, and k2 such that
H(k1) equals H(k2).When k1 is entered in the table,
it gets inserted in the position H(k1). But when k2 is
hashed, an attempt maybe made to insert at the
same position as k1.But that is not possible.
31
73
44 32
41 18 44 59 32 22 31 73
h(k) = k mod 8
Insert keys:41,51,66,74
7
4
4 6 5 7
1 6 1 4
0 1 2 3 4 5
6 7
4 6 5 7
1 6 1 4
0 1 2 3 4 5
6 7
• h1(K) = K mod 13
• h2(K) = 3 + K mod 4
– we want h2 to be an offset to add
– 18 41 22 44 59 32 45 47
44 45
41 18 32 59 47 22
0 1 2 3 4 5 6 7 8 9 10 11 12
41 44 45 18 32 59 47 22
0 1 2 3 4 5 6 7 8 9 10 11 12
• In its simplest
form each slot in
the array is a
linked list, or the
head cell of a
linked list, where
the list contains
the elements that
are hashed to the
same location.
h(k) = k mod 10
Insert 2 3 5 7 8 122 65 88 12 25 15
2 2 122 12
3 3
5 5 65 25 15
7 7
8 8 88
h(k) = (mid k2) mod 5
Insert 16 13 15 17 12 19
1 13 19
2 15
H(19) = mid(361) mod 5 = 6 mod 5 =1
3 17
4 12
SORTING
25 17 31 13 2
FIRST
ITERATION
25 17 31 13 2
INSERTION SORT CONTD…
SECOND ITERATION
17 25 31 13 2
THIRD ITERATION
17 25 31 13 2
INSERTION SORT CONTD…
THIRD
ITERATION
13 17 25 31 2
FOURTH
ITERATION
13 17 25 31 2
INSERTION SORT CONTD…
SORTED ----ARRAY------
2 13 17 25 31
INSERTION SORT CONTD…
Analysis Insertion Sort
int i, j, temp;
for (i=1; i<n; i++)
{ j=i;
temp=a[j];
while (j>0 && a[j-1]>temp)
{ a[j]=a[j-1];
j--;
}
a[j]=t;
}
Analysis Insertion Sort
The worst case occurs when in every step the proper position for
the element that is inserted is found at the beginning of the sorted
part of the sequence. I.e. in the while-loop sequences of length 1, 2,
3, ..., n-1 are scanned. Altogether, these are (n-1)·n / 2 Θ(n2)
operations. This case occurs when the original sequence is sorted
in decreasing order.
It is possible to find the inserting position of element ai faster,
namely by binary search. However, moving the elements to the
right in order to make room for the element to be inserted takes
linear time anyway.
The exact number of steps required by insertion sort is given by
the number of inversions of the sequence.
Definition: Let a = a0, ..., an-1 be a finite sequence. An
inversion is a pair of index positions, where the elements of the
sequence are out of order. Formally, an inversion is a pair (i, j),
where i < j and ai > aj.
Analysis Insertion Sort
Example: Let a = 5, 7, 4, 9, 7. Then, (0, 2) is an inversion, since
a0 > a2, namely 5 > 4. Also, (1, 2) is an inversion, since 7 > 4, and
(3, 4) is an inversion, since 9 > 7. There are no other inversions in
this sequence.
We now count the inversions (i, j) of a sequence a separately for each
position j. The resulting value of vj gives the number of elements ai to the
left of aj which are greater than aj.
For instance, for the sequence a = 5, 7, 4, 9, 7 we have v2 = 2, since the
two inversions (0, 2) and (1, 2) have 2 as their second component. Here, 5
and 7 are greater than 4.
T(a) = i = 0, ..., n-1 vi
vi00222416
Thus, insertion sort requires 17 steps to sort this sequence
T(a) = i = 0, ..., n-1 vi
25 17 31 13 2
17 25 31 13 2
FIRST ITERATION cont.
17 25 31 13 2
13 25 31 17 2
AFTER FIRST ITERATION
2 25 31 17 13
2 13 31 25 17
AFTER THIRD ITERATION
2 13 17 31 25
2 13 17 25 31
SELECTION SORT
Analysis of SELECTION SORT
Algorithm
for outer in (list start..list end-1) loop
highest := outer; -- starting highest value
for inner in (outer .. list end) loop
if (list(inner) > list(highest) then
highest := inner -- new highest value
end if
end loop
swap list(highest) and list(outer)
end loop
ALGORITHM: SELECTION SORT
void selection_sort (int arr[], int size)
{
int temp;
for (int i=0;i<(size-1) ;i++)
{
for (int j=i+1;j<size;j++)
{
if (arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
Analysis of SELECTION SORT
n = 6
# of comparisons = (n-1) + (n-2) + (n-3) + (n-4) + (n-5)
# of comparisons = (6-1) + (6-2) + (6-3) + (6-4) + (6-5)
= 5 + 4 + 3 + 2 + 1
= 15
Compare 1st two elements, if out of
order, exchange them to put in order.
Move down one element, compare 2nd
and 3rd elements, exchange if
necessary. Continue until end of array.
Pass through array again, exchanging
as necessary.
Bubble Sort
Compare
3
2nd Pass
16
32
5
48
For our second pass through the List, we
start by comparing these first two elements
in the List.
A Bubble Sort Example
3
Compare
16
32
5
48
A Bubble Sort Example
3
16
32
Compare
5
48
A Bubble Sort Example
3
16
5
Swap
32
48
At the end of the second pass, we stop at element
number n - 1, (n=5,here) because the largest element
in the list is already in the last position. This places the
second largest element in the second to last spot.
A Bubble Sort Example
3rd Pass
3
Compare
16
5
32
48
A Bubble Sort Example
3
Compare
16
5
32
48
A Bubble Sort Example
3
Swap
5
16
32
48
At the end of the third pass, we stop comparing
and swapping at element number n - 2.
A Bubble Sort Example
4th Pass
3
Compare
5
16
The end of the fourth pass 32
stops at element number n - 3.
48
The last pass (the 4th pass here ) compares only the first
two elements of the List. After this comparison and
possible swap, the smallest element has “bubbled” to the
top.
void bubble_sort (int arr[], int size)
{
int temp;
for (int i=0;i<size ;i++)
{
for (int j=0;j<(size-1)-i;j++)
{
if (arr[j]<arr[j+1])
{ temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
Traditionally,the
time for a sorting algorithm is
measured in terms of comparisons.
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort – Example
18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43
18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9
18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
MERGE SORT
How do we divide the list?
to divide A[left..Right],
we compute:
center=(left+right)/2
and obtain:
function merge_sort(m)
{
int list left, right, result
if length(m) ≤ 1
return m
left = merge_sort(left)
right = merge_sort(right)
result = merge(left, right)
return result
}
Continue….
function merge(left,right)
{
int result
• Time, divide
n
n
log n levels
n/2 × 2 = n
Total time for divide: n log n
Merge-Sort Analysis
• Time, merging
n n
log n levels
n/2 × 2 = n
order of nlogn
QUICK SORT
Divide and Conquer
Quick sort is a very efficient sorting algorithm
invented by C.A.R. Hoare. It works on the
Divide and Conquer rule.
Quick-sort is a randomized
sorting algorithm based on the x
divide-and-conquer paradigm:
◦ Divide: pick a random
element x (called pivot) and
partition S into
x
L elements less than x
E elements equal x L E G
G elements greater than x
◦ Recur: sort L and G
◦ Conquer: join L, E and G
x
function quicksort(array)
var list less, greater
if length(array) ≤ 1
return array
select and remove a pivot value pivot
from array
for each x in array
if x ≤ pivot then append x to less
else append x to greater
return
Concatenate(quicksort(less),pivot,quicksort(gr
eater))
It requires extra storage space, which
is bad.
The additional memory allocations
When a complete
binary tree is built,
its first node must be
the root.
Complete
binary tree. Left child
of the
root
27 21 22 4
19
Each node in a heap
contains a key that
can be compared to
other nodes' keys.
A heap is a 45
certain kind of
complete
binary tree. 35 23
27 21 22 4
19
The "heap property"
requires that each
node's key is >= the
keys of its children
Put the new node in the 45
next available spot.
Push the new node
upward, swapping with its
35 23
parent until the new node
reaches an acceptable
location. 27 21 22 4
19 42
Put the new node in the 45
next available spot.
Push the new node
upward, swapping with its
35 23
parent until the new node
reaches an acceptable
location. 42 21 22 4
19 27
Put the new node in the 45
next available spot.
Push the new node
upward, swapping with its
42 23
parent until the new node
reaches an acceptable
location. 35 21 22 4
19 27
The parent has a key that 45
is >= new node, or
The node reaches the
root.
42 23
The process of pushing
the new node upward
is called 35 21 22 4
reheapification
upward.
19 27
Move the last node onto 45
the root.
42 23
35 21 22 4
19 27
Move the last node onto 27
the root.
42 23
35 21 22 4
19
Move the last node onto 27
the root.
Push the out-of-place
node downward, swapping
42 23
with its larger child until
the new node reaches an
acceptable location. 35 21 22 4
19
Move the last node onto 42
the root.
Push the out-of-place
node downward, swapping
27 23
with its larger child until
the new node reaches an
acceptable location. 35 21 22 4
19
Move the last node onto 42
the root.
Push the out-of-place
node downward, swapping
35 23
with its larger child until
the new node reaches an
acceptable location. 27 21 22 4
19
The children all have keys 42
<= the out-of-place
node, or
The node reaches the leaf.
35 23
The process of pushing
the new node downward
is called 27 21 22 4
reheapification
downward.
19
We will store the 42
data from the nodes
in a partially-filled
array. 35 23
27 21
An array of data
Data from the root 42
goes in the first
location
of the 35 23
array.
27 21
42
An array of data
Data from the next 42
row goes in the next
two array locations.
35 23
27 21
42 35 23
An array of data
Data from the next 42
row goes in the next
two array locations.
35 23
27 21
42 35 23 27 21
An array of data
Data from the next 42
row goes in the next
two array locations.
35 23
27 21
42 35 23 27 21
An array of data
We don't care what's in
this part of the array.
The links between the tree's 42
nodes are not actually stored as
pointers, or in any other way.
The only way we "know" that "the
35 23
array is a tree" is from the way we
manipulate the data.
27 21
42 35 23 27 21
An array of data
If you know the index of a node, 42
then it is easy to figure out the
indexes of that node's parent and
children.
35 23
27 21
42 35 23 27 21
2 9
13 57 25 17
1 90 3
11 2 9 13 57 25 17 1 90 3
HEAP FROM BINARY TREE
90
57 25
13 11 9 17
1 2 3
90 57 25 13 11 9 17 1 2 3
RESULT OF HEAP SORT
1 2 3 9 1 1 1 2 5 9
1 3 7 5 7 0
COMPLEXITY OF HEAP
SORT
O(nlogn)
HEAP SORT
HeapSort(size) {
for (i = size/2; i >= 0; i--)
ReHeap(size, i);
for (i = size-1; i > 0; i--)
{
swap a[i] and a[0];
ReHeap(i, 0);
}
}
ReHeap(len, parent) {
temp = a[parent];
HEAP SORT
ReHeap(len, parent) {
temp = a[parent];
child = 2*parent + 1;
while (child < len)
{
if (child<len-1 && a[child]<a[child+1])
child++;
if (temp >= a[child]) break;
a[parent] = a[child];
parent = child;
child = 2*parent + 1;
}
a[parent] = temp;
return;
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
i
i
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
parent
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
parent child
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
i
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
parent
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
parent child
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
i
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
parent
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
parent child
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
i
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
parent
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
parent child
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
i
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
parent
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
parentchild
9 6 7 1 15 12 8 3 10 14 13 11 4 5 2
parent child
9 6 7 10 15 12 8 3 10 14 13 11 4 5 2
parent child
9 6 7 10 15 12 8 3 10 14 13 11 4 5 2
child
parent
9 6 7 10 15 12 8 3 1 14 13 11 4 5 2
parent child
9 6 7 10 15 12 8 3 1 14 13 11 4 5 2
i
9 6 7 10 15 12 8 3 1 14 13 11 4 5 2
parent
9 6 7 10 15 12 8 3 1 14 13 11 4 5 2
parent child
9 6 12 10 15 12 8 3 1 14 13 11 4 5 2
parent child
9 6 7 10 15 12 8 3 1 14 13 11 4 5 2
child
parent
9 6 12 10 15 11 8 3 1 14 13 11 4 5 2
parent child
9 6 12 10 15 11 8 3 1 14 13 11 4 5 2
child
parent
9 6 12 10 15 11 8 3 1 14 13 11 4 5 2
parent
9 6 12 10 15 11 8 3 1 14 13 7 4 5 2
parent
9 6 12 10 15 11 8 3 1 14 13 7 4 5 2
i
9 6 12 10 15 11 8 3 1 14 13 7 4 5 2
parent child
9 6 12 10 15 11 8 3 1 14 13 7 4 5 2
parent child
9 15 12 10 15 11 8 3 1 14 13 7 4 5 2
parent child
9 15 12 10 15 11 8 3 1 14 13 7 4 5 2
child
parent
9 15 12 10 14 11 8 3 1 14 13 7 4 5 2
parent child
9 15 12 10 14 11 8 3 1 14 13 7 4 5 2
child
parent
9 15 12 10 14 11 8 3 1 14 13 7 4 5 2
parent child
9 15 12 10 14 11 8 3 1 6 13 7 4 5 2
parent child
9 15 12 10 14 11 8 3 1 6 13 7 4 5 2
i
9 15 12 10 14 11 8 3 1 6 13 7 4 5 2
parent
9 15 12 10 14 11 8 3 1 6 13 7 4 5 2
parent child
15 15 12 10 14 11 8 3 1 6 13 7 4 5 2
parent child
15 15 12 10 14 11 8 3 1 6 13 7 4 5 2
child
parent
15 15 12 10 14 11 8 3 1 6 13 7 4 5 2
parent child
15 15 12 10 14 11 8 3 1 6 13 7 4 5 2
parent child
15 14 12 10 14 11 8 3 1 6 13 7 4 5 2
parent child
15 14 12 10 14 11 8 3 1 6 13 7 4 5 2
child
parent
15 14 12 10 14 11 8 3 1 6 13 7 4 5 2
parent child
15 14 12 10 14 11 8 3 1 6 13 7 4 5 2
parent child
15 14 12 10 13 11 8 3 1 6 13 7 4 5 2
parent child
15 14 12 10 13 11 8 3 1 6 13 7 4 5 2
child
parent
15 14 12 10 13 11 8 3 1 6 9 7 4 5 2
parent
15 14 12 10 13 11 8 3 1 6 9 7 4 5 2
i
15 14 12 10 13 11 8 3 1 6 9 7 4 5 2
i
2 14 12 10 13 11 8 3 1 6 9 7 4 5 15
i
2 14 12 10 13 11 8 3 1 6 9 7 4 5 15
parent
2 14 12 10 13 11 8 3 1 6 9 7 4 5 15
Parent child
2 14 12 10 13 11 8 3 1 6 9 7 4 5 15
Parent child
14 14 12 10 13 11 8 3 1 6 9 7 4 5 15
Child
parent
14 14 12 10 13 11 8 3 1 6 9 7 4 5 15
Child
parent
14 14 12 10 13 11 8 3 1 6 9 7 4 5 15
Child
parent
2 13 12 10 13 11 8 3 1 6 9 7 4 5 15
Parent child
14 13 12 10 13 11 8 3 1 6 9 7 4 5 15
Child
parent
14 13 12 10 13 11 8 3 1 6 9 7 4 5 15
Child
parent
14 13 12 10 13 11 8 3 1 6 9 7 4 5 15
Child
parent
14 13 12 10 13 11 8 3 1 6 9 7 4 5 15
Parent child
14 13 12 10 9 11 8 3 1 6 9 7 4 5 15
Child
parent
14 13 12 10 9 11 8 3 1 6 2 7 4 5 15
parent
14 13 12 10 9 11 8 3 1 6 2 7 4 5 15
i
14 13 12 10 9 11 8 3 1 6 2 7 4 5 15
i
5 13 12 10 9 11 8 3 1 6 2 7 4 14 15
i
5 13 12 10 9 11 8 3 1 6 2 7 4 14 15
parent
5 13 12 10 9 11 8 3 1 6 2 7 4 14 15
Parent child
13 13 12 10 9 11 8 3 1 6 2 7 4 14 15
child
Parent
13 13 12 10 9 11 8 3 1 6 2 7 4 14 15
child
Parent
13 10 12 10 9 11 8 3 1 6 2 7 4 14 15
child
Parent
13 10 12 10 9 11 8 3 1 6 2 7 4 14 15
Parent child
13 10 12 5 9 11 8 3 1 6 2 7 4 14 15
child
Parent
13 10 12 5 9 11 8 3 1 6 2 7 4 14 15
i
4 10 12 5 9 11 8 3 1 6 2 7 13 14 15
Parent child
4 10 12 5 9 11 8 3 1 6 2 7 13 14 15
Parent child
4 10 12 5 9 11 8 3 1 6 2 7 13 14 15
Parent Child
12 10 12 5 9 11 8 3 1 6 2 7 13 14 15
Child
Parent
12 10 12 5 9 11 8 3 1 6 2 7 13 14 15
Parent child
12 10 11 5 9 11 8 3 1 6 2 7 13 14 15
child
Parent
12 10 11 5 9 11 8 3 1 6 2 7 13 14 15
Parent Child
12 10 7 5 9 11 8 3 1 6 2 7 13 14 15
Child
Parent
12 10 7 5 9 11 8 3 1 6 2 4 13 14 15
Parent
12 10 7 5 9 11 8 3 1 6 2 4 13 14 15
i
12 10 7 5 9 11 8 3 1 6 2 4 13 14 15
i
4 10 7 5 9 11 8 3 1 6 2 12 13 14 15
Parent child
4 10 7 5 9 11 8 3 1 6 2 12 13 14 15
Parent child
4 10 7 5 9 11 8 3 1 6 2 12 13 14 15
i