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

Unit 5 Data Structures

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

Unit 5 Data Structures

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

UNITV SEARCHING,SORTINGANDHASHINGTECHNIQUES 9

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.

A simple approach is to do linear search, i.e


Start from the leftmost element of arr[] and one by one compare x with each element of
arr[] If x matches with an element, return the index.
If x doesn‟t match with any of elements, return -1.
Example:

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;
}

2. Write and explain non-recursive algorithm for binary search with


example.

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:

 We basically ignore half of the elements just after one


comparison. Compare x with the middle element.
 If x matches with middle element, we return the mid index.
 Else If x is greater than the mid element, then x can only lie in right half
subarray after the mid element. So we recur for right half.
 Else (x is smaller) recur for the left half.
program to implement recursive Binary Search

#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.

The selection sort algorithm is performed using following steps...


Step 1: Select the first element of the list (i.e., Element at first position in the list).
Step 2: Compare the selected element with all other elements in the list.
Step 3: For every comparision, if any element is smaller than selected element (for
Ascending order), then these two are swapped.
Step 4: Repeat the same procedure with next position in the list till the entire list is sorted.
Example:
Selection sort
Routine for(i=0; i<size;
i++){ for(j=i+1; j<size; j++)
{ if(list[i] > list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}

5. Describe about Bubble sort with suitable example.

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

ORIGINAL 20 10 60 40 30 10 POSITIONS MOVED


After i = 1 10 20 60 40 30 15 1
After i = 2 10 20 60 40 30 15 0
After i = 3 10 20 40 60 30 15 1
After i = 4 10 20 30 40 60 15 2
After i = 5 10 15 20 30 40 60 4
Sorted
10 15 20 30 40 60
Array

INSERTION SORT ROUTINE

Void insertionSort(int a[], int n)


{
int i, temp, j;
for (i = 1; i < n; i++)
{
temp = a[i];
for( j=i ; j>0 && a[j-1] > temp ; j--)
{
a[j] = a[j-1];
}
a[j] = temp;
}
}

6.Describe about Bubble sort with suitable example.

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

After the first pass


35 17 11 28 12 81 94 95 96 58
In second pass, K is reduced to 3

35 17 11 28 12 81 94 95 96 58

After second pass,


28 12 11 35 17 81 58 95 96 94
In third pass , K is reduced to 1
28 12 11 35 17 81 58 95 96 94

The final sorted array is


11 12 17 28 35 58 81 94 95 98
Program:

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;
}
}
}
}

7. Describe about Bubble sort with suitable example.


RADIX SORT
The idea of Radix Sort is to do digit by digit sort starting from least significant digit to
most significant digit. Radix sort uses counting sort as a subroutine to sort.
The Radix Sort Algorithm
1) Do following for each digit i where i varies from least significant digit to the
most significant digit.
a) Sort input array using counting sort (or any stable sort) according to the i‟th digit.
Example:
Original, unsorted list:
170, 45, 75, 90, 802, 24, 2, 66
Sorting by least significant digit (1s place) gives: [*Notice that we keep 802 before 2, because
802 occurred before 2 in the original list, and similarly for pairs 170 & 90 and 45 & 75.]
170, 90, 802, 2, 24, 45, 75, 66
Sorting by next digit (10s place) gives: [*Notice that 802 again comes before 2 as 802 comes
before 2 in the previous list.]
802, 2, 24, 45, 66, 170, 75, 90
Sorting by most significant digit (100s place) gives:
2, 24, 45, 66, 75, 90, 170, 802
RADIX SORT ROUTINE

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.

A simple Hash function

HASH (KEYVALUE) = KEYVALUE MOD TABLESIZE

Example: Hash (92)

Hash (92) = 92 mod 10 =2

The key value „92‟ is placed in the relative location „2‟.

ROUTINE FOR SIMPLE HASH


FUNCTION:

Hash(const char *key, intTableSize )

intHashVal = 0;

While( *key != „\0‟ )

HashVal +=*key++;

Return HashVal % TableSize;

}
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

The process of finding another position for the collide record.

Some of the collision Resolution Techniques


1. Seperate chaining
2. Open Addressing
3. Double Hashing
Separate chaining
Seperate chaining is an open hashing technique. A pointer field is added to each record
location. When an overflow occurs this pointer is set to point to overflow blocks making a
linked list.

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

Void Insert(int key, Hashtable H)


{
Position pos, newcell;
List L;// Traverse the list to check whether the key is already present Pos= Find (key, H);
If(pos == NULL) //key is not found

{ newcell= malloc (size of (structListNode)); If ( newcell ! = NULL)


{
L= H-->Thelist [Hash (key, H-->Tablesize)];
Newcell --> Next = L --> Next;

--
Find Routine

Position Find ( int key, Hashtable H)

Position P;

List L;

L= H-->Thelist [Hash ( key, H -->Tablesize)];

P= L --> Next;

While (p! = NULL && P -->Element ! = key)

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.

Routine for Searching an Element

structDataItem *search(int key) {


//get the hash
inthashIndex = hash(key);

//move in array until an empty


while(hashArray[hashIndex] != NULL) {

if(hashArray[hashIndex]->key == key)
returnhashArray[hashIndex];

Routine for
//go to next cell
Insertion
++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}
void insert(intkey,int data) {
structDataItem *item = (structDataItem*) malloc(sizeof(structDataItem));
item->data = data;
item->key = key;

//get the hash


inthashIndex = hashCode(key);

//move in array until an empty or deleted cell


while(hashArray[hashIndex] != NULL &&hashArray[hashIndex]->key != -1) {
//go to next cell
++hashIndex;

//wrap around the table


hashIndex %= SIZE;
}

hashArray[hashIndex] = item;
}

10.Explain In Detail About Quadratic Probing and double hashing With


Example.
Quadratic probing is similar to linear probing and the only difference is the interval between
successive probes or entry slots. Here, when the slot at a hashed index for an entry record is
already occupied, you must start traversing until you find an unoccupied slot. The interval
between slots is computed by adding the successive value of an arbitrary polynomial in the
original hashed index.
Let us assume that the hashed index for an entry is index and at index there is an occupied
slot. The probe sequence will be as follows:
index = index % hashTableSize
index = (index + 1) %
hashTableSize index = (index + 4)
% hashTableSize index = (index +
9) % hashTableSize
Quadratic Probing Example

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:

11. Formulate the rehashing technique with suitable example.


REHASHING
If the table is close to full, the search time grows and may become equal to the table size.
When the load factor exceeds a certain value (e.g. greater than 0.5) we do rehashing :
Build a second table twice as large as the original
and rehash there all the keys of the original table.
Rehashing is expensive operation, with running time O(N)
However, once done, the new hash table will have good performance.
Routine for rehashing for open addressing hash tables

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;
}

12. Analyze extendible hashing in brief.

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

Bucket split – no directory split


UNIT-V
PART-A

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

3. What do you mean by internal and external sorting?[AU-APR/MAY 2023]


An internal sort is any data sorting process that takes place entirely within the main memory
of a computer. This is possible whenever the data to be sorted is small enough to all be held in
the main memory.
External sorting is a term for a class of sorting algorithms that can handle massive amounts
of data. External sorting is required when the data being sorted do not fit into the main
memory of a computing device (usually RAM) and instead they must reside in the slower
external memory (usually a hard drive)

4. Define bubble sort


Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list
to be sorted, comparing each pair of adjacent items and swapping them if they are in the
wrong order. The pass through the list is repeated until no swaps are needed, which
indicates that the list is sorted. The algorithm gets its name from the way smaller elements
"bubble" to the top of the list.
5. How the insertion sort is done with the array?
It sorts a list of elements by inserting each successive element in the previously sorted
sub list.
Consider an array to be sorted A[1],A[2],….A[n]
b. Pass 2 : A[3] is compared with both A[1] and A[2] and inserted
at an appropriate place. This makes A[1], A[2],A[3] as a sorted
sub array.
c. Pass n-1 : A[n] is compared with each element
in the sub array A[1],A[2],……A[n-1] and inserted
at an appropriate position.

6. What are the steps for selection sort?


The algorithm divides the input list into two parts: the sublist of items already sorted, which
is built up from left to right at the front (left) of the list, and the sublist of items remaining to
be sorted that occupy the rest of the list.
Initially, the sorted sublist is empty and the unsorted sublist is the entire input list.
The algorithm proceeds by finding the smallest (or largest, depending on sorting order)
element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it
in sorted order), and moving the sublist boundaries one element to the right.
7. What is meant by shell sort?
Shell sort, also known as Shell sort or Shell's method, is an in-place comparison sort. It can
either be seen as a generalization of sorting by exchange (bubble sort) or sorting by
insertion (insertion sort).[1] The method starts by sorting elements far apart from each
other and progressively reducing the gap between them. Starting with far apart elements
can move some out-of-place elements into position faster than a simple nearest neighbor
exchange. Donald Shell published the first version of this sort in 1959. The running time of
Shell sort is heavily dependent on the gap sequence it uses

8. What are the steps in quick sort?


The steps are:
a. Pick an element, called a pivot, from the list.
b. Reorder the list so that all elements with values less than the pivot come
before the pivot, while all elements with values greater than the pivot come
after it (equal values can go either way). After this partitioning, the pivot is
in its final position. This is called the partition operation.
c. Recursively apply the above steps to the sub-list of elements with smaller
values and separately to the sub-list of elements with greater values.

9. Define radix sort


Radix Sort is a clever and intuitive little sorting algorithm. Radix sort is a non-
comparative integer sorting algorithm that sorts data with integer keys by grouping keys
by the individual digits which share the same significant position and value. Radix Sort puts
the elements in order by comparing the digits of the numbers.

10. What are the advantages of insertion sort[au-nov/dec 2022]


Advantages
a. Simplest sorting technique and easy to implement
b. It performs well in the case of smaller lists.
c. It leverages the presence of any existing sort pattern in the list
Disadvantages
Efficiency of O(n ) is not well suited for large sized lists

It requires large number of elements to be shifted


11.Define searching
Searching refers to determining whether an element is present in a given list of elements
or not. If the element is present, the search is considered as successful, otherwise it is
considered as an unsuccessful search. The choice of a searching technique is based on the
following factors
a. Order of elements in the list i.e., random or sorted
b. Size of the list
12. Mention the types of searching
The types are
Linear search
Binary search

13.What is meant by linear search?


Linear search or sequential search is a method for finding a particular value in a list that
consists of checking every one of its elements, one at a time and in sequence, until the
desired one is found.

14.What is binary search?


For binary search, the array should be arranged in ascending or descending order.
In each step, the algorithm compares the search key value with the middle element of the
array. If the key match, then a matching element has been found and its index, or position,
is returned.
Otherwise, if the search key is less than the middle element, then the algorithm repeats its
action on the sub-array to the left of the middle element or, if the search key is greater, on
the sub-array to the right.

15. Define hashing function[au-nov/dec 2022]


A hashing function is a key-to-transformation, which acts upon a given key to compute
the relative position of the key in an array.
A simple hash function
HASH(KEY_Value)= (KEY_Value)mod(Table-size)

16.What is open addressing?


Open addressing is also called closed hashing, which is an alternative to resolve the
collisions with linked lists. In this hashing system, if a collision occurs, alternative cells
are tired until an empty cell is found.
There are three strategies in open addressing:
Linear probing
Quadratic probing
Double hashing

17. What are the collision resolution methods?


The following are the collision resolution methods
Separate chaining
Open addressing
Multiple hashing
18. Define separate chaining
It is an open hashing technique. A pointer field is added to each record location, when an overflow
occurs, this pointer is set to point to overflow blocks making a linked list.
In this method, the table can never overflow, since the linked lists are only extended upon the
arrival of new keys.
19.What are the use of hash table?
1.Compilers can use hash table to keep track of declared variable in source code.
2.A hash table is useful for any graph theory problem where nodes
haver real names instead of numbers
3.A third use of hash table is in program that
play games. 4.On line spell checkers
20. Define Hashing .
Hashing is a technique used to identify the location of an identifier ‘x’ in the memory by some
arithmetic functions like f(x), which gives address of ‘x’ in the table.

21.Explain Hash Function.[AU-NOV/DEC 2023]


Hash Function takes an identifier and computes the address of that identifier in
the hash table.

22.Mention Different types of popular hash function.


1. Division method
2.Square method
3.Folding method

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.

24.Mention Different types of collision resolving techniques.


The collision resolving techniques are:
1.Separate chaining.
2.Open Addressing
3.Linear Probing
4.Quadratic Probing
5.Double Hashing.

You might also like