Unit 5 Data Structures
Unit 5 Data Structures
Searching – Linear Search – Binary Search. Sorting – Bubble sort– Selection sort – Insertion
sort – Shell sort –. Merge Sort – Hashing – Hash Functions – Separate Chaining – Open
Addressing – Rehashing – Extendible Hashing.
1.Write and explain non-recursive algorithm for linear search with example.
[AU-NOV/DEC 2022, 2023].
LINEAR SEARCH
Linear search is made over all items one by one. Every item is checked and if a match is
found then that particular item is returned, otherwise the search continues till the end of
the data collection.
Program:
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
Search a sorted array by repeatedly dividing the search interval in half. Begin with an
interval covering the whole array. If the value of the search key is less than the item in the
middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the
upper half. Repeatedly check until the value is found or the interval is empty.
Example:
#include <stdio.h>
// A recursive binary search function. It returns
// location of x in given array arr[l..r] is present,
// otherwise -1
intbinarySearch(intarr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
// If the element is present at the middle
// itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
returnbinarySearch(arr, l, mid-1, x);
// Else the element can only be present
// in right subarray
returnbinarySearch(arr, mid+1, r, x);
}
// We reach here when element is not
// present in array
return -1;
}
3. Describe about Bubble sort with suitable example.
BUBBLE SORT [AU NOV/DEC-2022, APR/MAY-2023]
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based
algorithm in which each pair of adjacent elements is compared and the elements are
swapped if they are not in order. This algorithm is not suitable for large data sets as its
average and worst case complexity are of Ο(n2) where n is the number of items.
Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since
5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm
does not swap them.
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The
algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
C Program To Sort data in ascending order using bubble sort
#include <stdio.h>
int main()
{
int data[100],i,n,step,temp;
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d. Enter element: ",i+1);
scanf("%d",&data[i]);
}
for(step=0;step<n-1;++step)
for(i=0;i<n-step-1;++i)
{
if(data[i]>data[i+1]) /* To sort in descending order, change > to < in this line. */
{
temp=data[i]; data[i]=data[i+1];
data[i+1]=te
mp;
}
}
printf("In ascending order: "); for(i=0;i<n;++i)
printf("%d ",data[i]); return 0;
}
4. Describe about selection sort with suitable example.
SELECTION SORT
In selection sort, the first element in the list is selected and it is compared repeatedly with
remaining all the elements in the list. If any element is smaller than the selected element (for
Ascending order), then both are swapped. Then we select the element at second position in
the list and it is compared with remaining all elements in the list. If any element is smaller
than the selected element, then both are swapped. This procedure is repeated till the entire
list is sorted.
INSERTION SORT:[AU-NOV/DEC-2023]
Insertion sort is a simple sorting algorithm that works the way we sort playing cards in
our hands. Insertion sorts works by taking element from the list one by one and inserting
them in their current position into a new sorted list.
Example:
Consider an unsorted array as follows,
20 10 60 40 30 15
SHELL SORT
The shell sort, sometimes called the “diminishing increment sort,” improves on the insertion
sort by breaking the original list into a number of smaller sub lists, each of which is sorted
using an insertion sort. The unique way that these sub lists are chosen is the key to the shell
sort. Instead of breaking the list into sub lists of contiguous items, the shell sort uses an
increment i, sometimes called the gap, to create a sub list by choosing all items that
are i items apart.
Example:
Consider an unsorted array as follows.
81 94 11 96 12 35 17 95 28 58
Here N=10, the first pass as K = 5 ( 10/2)
81 94 11 96 12 35 17 95 28 58
35 17 11 28 12 81 94 95 96 58
voidshellsort(intarr[], intnum)
{
int i, j, k, tmp;
for (i = num / 2; i > 0; i = i / 2)
{
for (j = i; j <num; j++)
{
for(k = j - i; k >= 0; k = k - i)
{
if (arr[k+i] >= arr[k])
break;
else
{ tmp =
arr[k]; arr[k] =
arr[k+i]; arr[k+i]
= tmp;
}
}
}
}
voidcountsort(intarr[],intn,int place)
{
inti,freq[range]={0}; //range for integers is 10 as digits range from 0-
9 int output[n];
for(i=0;i<n;i++)
freq[(arr[i]/place)%range]++;
for(i=1;i<range;i++) freq[i]
+=freq[i-1];
for(i=n-1;i>=0;i--)
{
output[freq[(arr[i]/place)%range]-1]=arr[i];
freq[(arr[i]/place)%range]--;
}
for(i=0;i<n;i++)
arr[i]=output[i];
}
voidradixsort(llarr[],intn,intmaxx) //maxx is the maximum element in the array
{ intmul=1;
while(maxx)
{
countsort(arr,n,mul);
mul*=10;
maxx/=10;
}
}
8.List the different types of hashing techniques and collision? Explain them in
detail with example.
HASHING [AU-NOV/DEC-2022] [APR/MAY-2023]
Hashing is a technique used for performing insertion, deletions, and finds in constant
average time.
The Hash table datastructure is an array of some fixed size, containing the keys. A key is a
value associated with each record.
Hashing Function
A Hashing function is a key – to – address transformation, which acts upon a given key to
compute the relative position of the key in an array.
intHashVal = 0;
HashVal +=*key++;
}
Some of the methods of Hashing Function
1. Module Division
2. Mid – square Method
3. Folding Method
4. PSEUDO Random Method
5. Digit or character Extraction Method
6. Radix Transformation
COLLISIONS
Collision occurs when a hash value of a record being inserted hashes to an address ( i.e.
Relative position) that already contain a different record. (ie) When two key values hash to the
same position.
Collision Resolution
In this method , the table can never overflow, since the linked list are only extended upon
the arrival of new keys.
Insertion
To perform the insertion of an element, traverse down the appropriate list to check whether
the element is already in place.
If the element turns to be a new one, it is inserted either at the front of the list or at the end
of the list.If it is duplicate element, an extra field is kept a d placed.
routine to perform insertion
--
Find Routine
Position P;
List L;
P= L --> Next;
P= P --> Next;
return P;
Advantages:
1) Simple to implement.
2) Hash table never fills up, we can always add more elements to chain.
3) Less sensitive to the hash function or load factors.
4) It is mostly used when it is unknown how many and how frequently keys may be
inserted or deleted.
Disadvantages:
1) Cache performance of chaining is not good as keys are stored using linked list.
Open addressing provides better cache performance as everything is stored in same
table.
2) Wastage of Space (Some Parts of hash table are never used)
3) If the chain becomes long, then search time can become O(n) in worst case.
4) Uses extra space for links
Example:
Let us consider a simple hash function as “key mod 7” and sequence of keys as 50, 700, 76,
85, 92, 73, 101.
9.Discuss the various open addressing techniques in hashing with an example.
OPEN ADDRESSING:
Like separate chaining, open addressing is a method for handling collisions. In Open
Addressing, all elements are stored in the hash table itself. So at any point, size of the
table must be greater than or equal to the total number of keys (Note that we can
increase table size by copying old data if needed).
Insert(k): Keep probing until an empty slot is found. Once an empty slot is found, insert k.
Search(k): Keep probing until slot‟s key doesn‟t become equal to k or an empty slot is
reached.
Delete(k): Delete operation is interesting. If we simply delete a key, then search may fail. So
slots of deleted keys are marked specially as “deleted”.
Insert can insert an item in a deleted slot, but the search doesn‟t stop at a deleted slot.
LINEAR PROBING:
In linear probing, we linearly probe for next slot. For example, typical gap between two
probes is 1 as taken in below example also.
let hash(x) be the slot index computed using hash function and S be the table
size If slot hash(x) % S is full, then we try (hash(x) + 1) % S
If (hash(x) + 1) % S is also full, then we try (hash(x) + 2) % S
If (hash(x) + 2) % S is also full, then we try (hash(x) + 3) % S
Let us consider a simple hash function as “key mod 7” and sequence of keys as 50, 700, 76,
85, 92, 73, 101.
if(hashArray[hashIndex]->key == key)
returnhashArray[hashIndex];
Routine for
//go to next cell
Insertion
++hashIndex;
hashArray[hashIndex] = item;
}
DOUBLE HASHING
Double hashing is similar to linear probing and the only difference is the interval between
successive probes. Here, the interval between probes is computed by using two hash
functions.
Let us say that the hashed index for an entry record is an index that is computed by one
hashing function and the slot at that index is already occupied. You must start traversing
in a specific probing sequence to look for an unoccupied slot.
The Double Hashing is:
f(i) = i . hash2 (x)
Where hash2(X) =R – ( X mod R)
To choose a prime R < size
The probing sequence will be
Example:
HashtableRehash(Hashtable H)
{
Int I, oldsize;
Cell *oldcells;
Oldcells = H Thecells; Oldsize
– H Tablesize;
H = InitializeTable (2 * oldsize);
For(i=0; i<oldsize; i++)
If(oldcells [i]. Info == Legitimate)
Insert(oldcells [i]. Element, H);
Free(oldcells);
Return H;
}
EXTENDIBLE HASHING
Used when the amount of data is too large to fit in main memory and external storage is
used.
N records in total to store, M records in one disk block
The problem: in ordinary hashing several disk blocks may be examined to find an element
- a time consuming process.
Extendible hashing: no more than two blocks are examined.
Idea:
Keys are grouped according to the first m bits in their
code. Each group is stored in one disk block.
If the block becomes full and no more records can be inserted, each group is split into
two, and m+1 bits are considered to determine the location of a record.
Extendible Hashing Example • Suppose that g=2 and bucket size = 4. • Suppose that we
have records with these keys and hash function h(key) = key mod 64:
Bucket and directory split
1. Define sorting
Sorting arranges the numerical and alphabetical data present in a list in a specific order
or sequence. There are a number of sorting techniques available. The algorithms can be
chosen based on the following factors
Size of the data structure
Algorithm efficiency
Programmer’s knowledge of the technique.
2. Mention the types of sorting
Internal sorting
External sorting
23.Define Collision.
When two different keys compute in the same location or address in the
hash table through any one of the hashing function then it is termed as collision.