Assignment 4
Assignment 4
1)Sort English words in alphabetical order using heap sort (C++ language):
#include <iostream>
#include <string>
int x = -1;
string heap[1000];
void heapForm(string k)
x++;
heap[x] = k;
int child = x;
string tmp;
int index = x / 2;
tmp = heap[index];
heap[index] = heap[child];
heap[child] = tmp;
child = index;
index = index / 2;
else {
break;
void heapSort()
while (x >= 0) {
string k;
k = heap[0];
heap[0] = heap[x];
x = x - 1;
string tmp;
int index = 0;
int length = x;
left1 = 1;
right1 = left1 + 1;
while (left1 <= length) {
break;
else {
tmp = heap[index];
heap[index] = heap[left1];
heap[left1] = tmp;
index = left1;
else {
tmp = heap[index];
heap[index] = heap[right1];
heap[right1] = tmp;
index = right1;
left1 = 2 * left1;
right1 = left1 + 1;
}
void sort(string k[], int n)
heapForm(k[i]);
heapSort();
int main()
sort(arr, n);
#include<iostream>
#include<climits>
class MinHeap
public:
// Constructor
MinHeap(int capacity);
void MinHeapify(int );
int extractMin();
};
MinHeap::MinHeap(int cap)
heap_size = 0;
capacity = cap;
harr = new int[cap];
void MinHeap::insertKey(int k)
if (heap_size == capacity)
return;
heap_size++;
int i = heap_size - 1;
harr[i] = k;
swap(&harr[i], &harr[parent(i)]);
i = parent(i);
}
// Decreases value of key at index 'i' to new_val. It is assumed that
harr[i] = new_val;
swap(&harr[i], &harr[parent(i)]);
i = parent(i);
int MinHeap::extractMin()
if (heap_size <= 0)
return INT_MAX;
if (heap_size == 1)
heap_size--;
return harr[0];
heap_size--;
MinHeapify(0);
return root;
void MinHeap::deleteKey(int i)
decreaseKey(i, INT_MIN);
extractMin();
void MinHeap::MinHeapify(int i)
int l = left(i);
int r = right(i);
int smallest = i;
smallest = l;
if (r < heap_size && harr[r] < harr[smallest])
smallest = r;
if (smallest != i)
swap(&harr[i], &harr[smallest]);
MinHeapify(smallest);
*x = *y;
*y = temp;
int main()
MinHeap h(11);
h.insertKey(3);
h.insertKey(2);
h.deleteKey(1);
h.insertKey(15);
h.insertKey(5);
h.insertKey(4);
h.insertKey(45);
h.decreaseKey(2, 1);
return 0;
}
4) Write counting sort to sort upper case English characters (C++ language):
#include <bits/stdc++.h>
#define MAX 26
void alternateSort(string& s)
int n = s.length();
int lCount[MAX] = { 0 }, uCount[MAX] = { 0 };
if (isupper(s[i]))
uCount[s[i] - 'A']++;
else
lCount[s[i] - 'a']++;
int i = 0, j = 0, k = 0;
while (k < n) {
i++;
if (i < MAX) {
s[k++] = 'A' + i;
uCount[i]--;
j++;
if (j < MAX) {
s[k++] = 'a' + j;
lCount[j]--;
}
}
int main()
alternateSort(str);
#include<stdio.h>
max = a[i];
return max;
NOP++;
lar /= 10;
bucket_cnt[i] = 0;
bucket[r][bucket_cnt[r]] = a[i];
bucket_cnt[r] += 1;
i = 0;
a[i] = bucket[k][j];
i++;
divisor *= 10;
}
}
int i, n, a[10000];
return 0;
}
6) Separate chaining:
The idea behind separate chaining is to implement the array as a linked list called a chain.
Separate chaining is one of the most popular and commonly used techniques in order to handle
collisions.
Here, all those elements that hash into the same slot index are inserted into a linked list. Now, we
can use a key K to search in the linked list by just linearly traversing. If the intrinsic key for any
entry is equal to K then it means that we have found our entry. If we have reached the end of the
linked list and yet we haven’t found our entry then it means that the entry does not exist. Hence,
the conclusion is that in separate chaining, if two different elements have the same hash value
then we store both the elements in the same linked list one after the other.
Linear probing:
The simplest approach to resolve a collision is linear probing. In this technique, if a value is already
stored at a location generated by h(k), it means collision occurred then we do a sequential search to
find the empty location.
Here the idea is to place a value in the next available position. Because in this approach searches are
performed sequentially so it’s known as linear probing.
Here array or hash table is considered circular because when the last slot reached an empty location
not found then the search proceeds to the first location of the array. Clustering is a major drawback
of linear probing.
Quadratic probing:
Quadratic probing is an open addressing scheme in computer programming for resolving the hash
collisions in hash tables. Quadratic probing operates by taking the original hash index and adding
successive values of an arbitrary quadratic polynomial until an open slot is found. An example
sequence using quadratic probing is:
Load factor:
For the first step, the time taken depends on the K and the hash function.
For example, if the key is a string “abcd”, then it’s hash function may depend on the length of the
string. But for very large values of n, the number of entries into the map, and length of the keys is
almost negligible in comparison to n so hash computation can be considered to take place in
constant time, i.e, O(1).
For the second step, traversal of the list of K-V pairs present at that index needs to be done. For
this, the worst case may be that all the n entries are at the same index. So, time complexity would
be O(n). But, enough research has been done to make hash functions uniformly distribute the keys
in the array so this almost never happens.
So, on an average, if there are n entries and b is the size of the array there would be n/b entries
on each index. This value n/b is called the load factor that represents the load that is there on our
map.
This Load Factor needs to be kept low, so that number of entries at one index is less and so is the
complexity almost constant, i.e., O(1).
Rehashing:
As the name suggests, rehashing means hashing again. Basically, when the load factor increases
to more than its pre-defined value (default value of load factor is 0.75), the complexity increases.
So to overcome this, the size of the array is increased (doubled) and all the values are hashed
again and stored in the new double-sized array to maintain a low load factor and low complexity.
Rehashing is done because whenever key value pairs are inserted into the map, the load factor
increases, which implies that the time complexity also increases as explained above. This might
not give the required time complexity of O(1).
Hence, rehash must be done, increasing the size of the bucketArray so as to reduce the load factor
and the time complexity.
Program:
#include <bits/stdc++.h>
int Arr[MAX_VERTEX];
int size[MAX_VERTEX];
void initialize(int n)
Arr[i] = i;
size[i] = 1;
}
int find(int i)
while (Arr[i] != i)
Arr[i] = Arr[Arr[i]];
i = Arr[i];
return i;
Arr[xr] = Arr[yr];
size[yr] += size[xr];
else
Arr[yr] = Arr[xr];
size[xr] += size[yr];
}
int isCycle(vector<int> adj[], int V)
int x = find(i);
int y = find(adj[i][j]);
if (x == y)
return 1;
_union(x, y);
return 0;
int main()
int V = 3;
initialize(V);
vector<int> adj[V];
adj[0].push_back(1);
adj[0].push_back(2);
adj[1].push_back(2);
if (isCycle(adj, V))
else
return 0;
}
9)