The document discusses various sorting and searching algorithms:
- Bubble sort, selection sort, and insertion sort are described along with their time complexities of O(n^2).
- Linear search is discussed, with the number of comparisons growing linearly with the size of the list.
- Applications of sorting and searching are mentioned but not detailed.
20. Adjacent elements are compared
For N number of elements (8 elements)
N-1 number of passes (7 elements)
In Pass 1, Number of comparisons N-1 (7)
In Pass 2, Number of comparisons N-2 (6)
In Pass 3, Number of comparisons N-3 (5)
……
In Pass N-3 (5), Number of comparisons 3
In Pass N-2 (6), Number of comparisons 2
In Pass N-1 (7), Number of comparisons 1
BUBBLE SORT
Total Number of comparisons for 8 elements
7+6+5+4+3+2+1 = (8 * 7)/2
Total Number of comparisons for N elements
(N * N-1)/2
29. Select the maximum element and place it at the end
max – is the Max element seen so far
maxi – is the index of the same
In Pass 1, Number of comparisons N-1 (7)
In Pass 2, Number of comparisons N-2 (6)
In Pass 3, Number of comparisons N-3 (5)
……
In Pass N-3 (5), Number of comparisons 3
In Pass N-2 (6), Number of comparisons 2
In Pass N-1 (7), Number of comparisons 1
SELECTION SORT
Total Number of comparisons for 8 elements
7+6+5+4+3+2+1 = (8 * 7)/2
Total Number of comparisons for N elements
(N * N-1)/2
31. Select the minimum element and place it at the beginning
min – is the Min element seen so far
mini – is the index of the same
In Pass 1, Number of comparisons N-1 (7)
In Pass 2, Number of comparisons N-2 (6)
In Pass 3, Number of comparisons N-3 (5)
……
In Pass N-3 (5), Number of comparisons 3
In Pass N-2 (6), Number of comparisons 2
In Pass N-1 (7), Number of comparisons 1
SELECTION SORT
45. Array is divided into sorted and unsorted part
Pick the first element from the unsorted array and find its
place it in the sorted array and place it there
In Pass 1, Number of comparisons 1
In Pass 2, Number of comparisons 2
In Pass 3, Number of comparisons 3
……
In Pass N-3 (5), Number of comparisons N-3 (5)
In Pass N-2 (6), Number of comparisons N-2 (6)
In Pass N-1 (7), Number of comparisons N-3 (7)
Total Number of comparisons for 8 elements
1+2+3+4+5+6+7 = (8 * 7)/2
Total Number of comparisons for N elements
(N * N-1)/2
INSERTION SORT
46. Searching is the process used to find the location of a target
among a list of objects.
SEARCHING
50. For 1 elements, No. of comparisons = 1
For 2 elements, No. of comparisons = 2
…..
For N-1 elements, No. of comparisons = N-1
For N elements, No. of comparisons = N
LINEAR SEARCH
51. int el,i;
printf("nEnter element to be searched? ");
scanf("%d",&el);
for(i=0;i<SIZE;i++)
{
if(a[i]==el)
return i;
}
return -1;
LINEAR SEARCH
53. Input array must be a sorted array with elements in ascending
order
BINARY SEARCH
For 16 elements
1 Comparison will divide the array in two parts of size 8
2 Comparisons will divide the array in four parts of size 4
3 Comparisons will divide the array in eight parts of size 2
4 Comparisons will divide the array in sixteen parts of size 1
For 32 elements
1 Comparison will divide the array in two parts of size 16
2 Comparisons will divide the array in four parts of size 8
3 Comparisons will divide the array in eight parts of size 4
4 Comparisons will divide the array in sixteen parts of size 2
5 Comparisons will divide the array in thirty two parts of size 1
54. 8 elements -> 3 comparisons
16 elements -> 4 comparisons
32 elements -> 5 comparisons
64 elements -> 6 comparisons
……
In general for 2N elements there will be N comparisons
BINARY SEARCH
N elements -> Log2N comparisons
56. In Linear search, search complexity is O(n)
In Binary Search, search complexity is O(log2n)
HASHING
Hashing makes it possible in O(1)
57. A hash table is a collection of elements which are stored in
such a way as to make it easy to find them later.
Each position of the hash table, often called a slot, can hold
an element and is named by an integer value starting at 0.
The mapping between an element and the slot where that
element belongs in the hash table is called the hash function.
HASH FUNCTION
59. HASH FUNCTION (DIVISION)
0 1 2 3 4 5 6 7 8 9 10 11
Element: 68 68 Modulo N
N = 12
8
68
Element: 72 72 Modulo N 0
72
Element: 122 122 Modulo N 2
122
Element: 46 46 Modulo N 10
46
Element: 15 15 Modulo N 3
15
Element: 50 50 Modulo N 2
50
60. The value of N is chosen to be a Prime Number
Given a collection of items, a hash function that maps each
item into a unique slot is referred to as a perfect hash
function.
HASH FUNCTION (DIVISION)
61. Step 1: Choose a constant A such that 0 < A < 1
Step 2: Multiply the element k by A.
Step 3: Extract the fractional part of k*A
Step 4: Multiply Extracted fractional by the size of hash table
The multiplication method works with any value of A, but
Knuth has suggested that the best choice of A is
0.6180339887
HASH FUNCTION (MULTIPLICATION)
The value of N is chosen to be some power of 2
63. Step 1: Square the value of the key
Step 2: Extract middle R digits
The value of R depends on the size of the hash table
HASH FUNCTION (MID SQUARE)
Key=116
Square=13456
For R=1, Hash value=4
For R=2, Hash value=34 or 45
For R=3, Hash value=345
R=1 if Hash Table size is 10 then 0-9
R=2 if Hash Table size is 100 then 0-99
R=3 if Hash Table size is 1000 then 0-999
……
64. The Truncation Method truncates a part of the given keys,
depending upon the size of the hash table.
HASH FUNCTION (TRUNCATION)
R=1 if Hash Table size is 10 then 0-9
R=2 if Hash Table size is 100 then 0-99
R=3 if Hash Table size is 1000 then 0-999
……
Key=2643
For R=1, Hash value=2 or 3
For R=2, Hash value=26 or 43
For R=3, Hash value=264 or 643
Any part of the key can be truncated
It should be Consistent across all keys
65. The folding method for constructing hash functions begins by
dividing the item into equal-size pieces (the last piece may
not be of equal size).
These pieces are then added together to give the resulting
hash value.
HASH FUNCTION (FOLDING)
Key= 4357821235
4+3+5+7+8+2+1+2+3+5 = 40
Hash table size = 10 [0-9] Hash key = 40%10=0
Hash table size = 100 [0-99] Hash key = 40%100=40
67. Collision?????
With Limited size of the Hash table, multiple keys map to the same
slot
COLLISION RESOLUTION
Collision Avoidance
Size of the Hash table must be greater than or at least equal to the
total number of keys
Size of the Hash table, if it is a Prime Number, then there will be
comparatively less collisions
71. Linearly probe for next available/free slot
LINEAR PROBING
0 1 2 3 4 5 6 7 8 9 10 11 12
Key : 68 68 Modulo N 3
68
N=13
Key : 100 100 Modulo N 9
100
Key : 56 56 Modulo N 4
56
Key : 35 35 Modulo N 9
35
35
Key : 41 41 Modulo N 2
41
Key : 106 106 Modulo N 2
106
106
Key : 151 151 Modulo N 8
151
10
3 4 5
Key : 49 49 Modulo N 10 11
49
49
72. Slot = Hash(key)
If Slot is Full then
Slot = (Hash(key) + 1) % N
If Slot is Full then
Slot = (Hash(key) + 2) % N
If Slot is Full then
Slot = (Hash(key) + 3) % N
If Slot is Full then
Slot = (Hash(key) + 4) % N
………
……..
Till there is free slot available
Else Error
LINEAR PROBING
73. Slot = Hash(key)
If Slot is Full then
Slot = Hash(key) + 12 % N OR Slot = Hash(key) + 1*1 % N ..... i=1
If Slot is Full then
Slot = Hash(key) + 22 % N OR Slot = Hash(key) + 2*2 % N ..... i=2
If Slot is Full then
Slot = Hash(key) + 32 % N OR Slot = Hash(key) + 3*3 % N ..... i=3
If Slot is Full then
Slot = Hash(key) + 42 % N OR Slot = Hash(key) + 4*4 % N ..... i=4
………
……..
Till there is free slot available
Else Error
QUADRATIC PROBING
74. Search for i2 th Slot in ith Iteration
QUADRATIC PROBING
0 1 2 3 4 5 6 7 8 9 10 11 12
Key : 68 68 Modulo N 3
68
N=13
Key : 100 100 Modulo N 9
100
Key : 56 56 Modulo N 4
56
Key : 35 35 Modulo N 9
35
35
Key : 41 41 Modulo N 2
41
Key : 106 106 Modulo N 2
106
106
Key : 151 151 Modulo N 8
151
i=1 9 +12 % N = 10
i=1 2 +12 % N = 3
Key : 93 93 Modulo N 2
93
93
i=2 2 +22 % N = 6
i=1 2 +12 % N = 3 i=2 2 +22 % N = 6
i=3 2 +32 % N = 11
75. DOUBLE HASHING
Two different hash functions
(hash(key) + i * hash1(key)) % N
Slot = Hash(key)
If Slot is Full then
Slot = (Hash(key) + 1 * Hash1(key)) % N
If Slot is Full then
Slot = (Hash(key) + 2 * Hash1(key)) % N
If Slot is Full then
Slot = (Hash(key) + 3 * Hash1(key)) % N
………
……..
Till there is free slot available
Else Error
77. Linear probing is simple but starts forming clusters in initial
stages only
Double hashing and Quadratic probing create relatively
uniform distribution of keys but are having complex
calculations
ANALYSIS OF OPEN ADDRESSING
TECHNIQUES
78. The M slots of the hash table are divided into B buckets, with
each bucket consisting of M/B slots.
The hash function assigns each record to the first slot within
one of the buckets. If this slot is already occupied, then the
bucket slots are searched sequentially until an open slot is
found.
If a bucket is entirely full, then the record is stored in
an overflow bucket of infinite capacity at the end of the table.
All buckets share the same overflow bucket.
A good implementation will use a hash function that
distributes the records evenly among the buckets so that as
few records as possible go into the overflow bucket.
BUCKET HASHING
80. Creates a linked list to the slot for which collision occurs.
The new key is then inserted in the linked list.
These linked lists to the slots appear like chains.
SEPARATE CHAINING
No idea about the number of keys