KCS301 Data Structure UNIT 3 Searching Sorting Hashing
KCS301 Data Structure UNIT 3 Searching Sorting Hashing
KCS301 Data Structure UNIT 3 Searching Sorting Hashing
Table of Contents
Searching:
Concept of Searching
Sequential Search
Index Sequential Search
Binary Search
Sorting:
Concept of Sorting
Bubble Sort
Selection Sort
Insertion Sort
Quick Sort
Merge Sort
Heap Sort
Radix Sort
Hashing:
Concept of Hashing
Collision Resolution Techniques used in Hashing
3 Dr. Sunil Kumar, CSE Dept., MIET Meerut
1
10/17/2022
Concept of Searching
Searching in data structure refers to the process
of finding location LOC of an element in a list.
This is one of the important parts of many data
structures algorithms, as one operation can be
performed on an element if and only if we find it.
2
10/17/2022
Types of Searching
Many different searching techniques exist and the
most commonly used searching techniques are:
3
10/18/2022
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.
1
10/18/2022
2
10/18/2022
Note: When the user makes a request for specific records it will
find that index group first where that specific record is recorded.
3
10/18/2022
Note: Indexed Sequential Search actually does the indexing multiple times,
like creating the index of an index.
Advantages:
More efficient
Time required is less
4
10/18/2022
Binary Search
A binary search or half-interval search algorithm
finds the position of a specified input value (the
search "key") within an array sorted by key value.
For binary search, the array should be arranged in
ascending or descending order.
5
10/18/2022
5 12 17 23 38 44 77 84 90
0 1 2 3 4 5 6 7 8
5 12 17 23 38 44 77 84 90
6
10/18/2022
0 1 2 3 4 5 6 7 8
5 12 17 23 38 44 77 84 90
0 1 2 3 4 5 6 7 8
5 12 17 23 38 44 77 84 90
7
10/18/2022
0 1 2 3 4 5 6 7 8
5 12 17 23 38 44 77 84 90
Unsuccessful Search
high low
no more elements to search low > high
8
10/18/2022
Concept of Sorting
Sorting is nothing but arrangement/storage of data in
sorted order, it can be in ascending or descending
order.
The term Sorting comes into picture with the term
Searching.
There are so man y things in our real life that we need
to search, like a particular record in database, roll
numbers in merit list, a particular telephone number,
any particular page in a book etc.
1
10/18/2022
Internal Sorting
Bubble Sort
Selection Sort
Insertion Sort
Quick Sort
Heap Sort
Radix Sort
Bucket Sort
Shell Sort
External Sorting
Any sort algorithm that uses external
memory, such as tape or disk, during the
sorting is called as external sorting.
2
10/18/2022
Bubble Sort
Bubble sort is a simple sorting technique.
This sorting technique 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 sorting is not suitable for large data sets as its
average and worst case complexity are of Ο(n2)
where n is the number of items.
3
10/18/2022
Bubble Sort...
1) Starting with the first element (index = 0), compare
the current element with the next element of the
array.
2) If the current element is greater than the next
element of the array, swap them.
3) If the current element is less than the next element,
move to the next element. Repeat Step 1.
Bubble Sort
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to
greatest number using bubble sort. In each step, elements written in bold are being
compared. Three passes will be required.
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
(14258)→(14258)
( 1 4 2 5 8 ) → ( 1 2 4 5 8 ), Swap since 4 > 2
(12458)→(12458)
(12458)→(12458)
Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs
one whole pass without any swap to know it is sorted.
Third Pass
(12458)→(12458)
(12458)→(12458)
(12458)→(12458)
(12458)→(12458)
33 Dr. Sunil Kumar, CSE Dept., MIET Meerut
4
10/18/2022
Bubble Sort
Bubble Sort
5
10/18/2022
Algorithm
Bubble Sort (A, N)
Here A is an array with N elements. This algorithm
sorts the elements in the array A.
Step 1: Repeat Steps 2 and 3 for k = 1 to N-1.
Step 2: Set PTR=1.
Step 3: Repeat while PTR<= N-k:
a) If A[PTR]> A[PTR+1], then:
Swap A[PTR] and A[PTR+1].
b) Set PTR=PTR+1.
Step 4: EXIT
6
10/19/2022
F(n)=(n-1)+(n-2)+……………+2+1=n(n-1)/2
= O(n 2 )
Selection Sort
Selection sorting is conceptually the simplest
sorting algorithm.
This algorithm first finds the smallest element in
the array and exchanges it with the element in
the first position, then find the second smallest
element and exchange it with the element in the
second position, and continues in this way until
the entire array is sorted.
1
10/19/2022
2
10/19/2022
Algorithm
Selection Sort (A, N)
This algorithm sorts the array A with N elements.
Step 1: Repeat Steps 2 and 3 for K = 1 to N-1:
Step 2: CALL MIN(A, K, N, LOC)
Step 3: SWAP A[K] with A[LOC]
Set TEMP= A[K],
A[K]= A[LOC],
A[LOC]=TEMP.
Step 4: EXIT
Algorithm...
Procedure MIN (A, K, N, LOC)
This procedure finds the location LOC of the smallest element
A[K], A[K+1],..., A[N], where A is an array.
Step 1: Set MIN= A[K] and LOC=K.
Step 2: Repeat Steps 2 for j=K+1, K+2,...,N:
If MIN> A[j], then set MIN=A[j] and LOC=j;
Step 3: Return.
3
10/19/2022
4
10/31/2022
Quick Sort
Quick Sort, as the name suggests, sorts any list very
quickly.
Quick sort is not stable search, but it is very fast
and requires very less additional space.
It is based on the rule of Divide and Conquer
(also called partition-exchange sort).
This algorithm divides the list into three main
parts:
Elements less than the Pivot
Pivot element
Elements greater than the pivot element
1
10/31/2022
Quick Sort
2
10/31/2022
Algorithm
QUICKSORT(A, p, r)
1. if p < r
2. then q PARTITION(A, p, r)
3. QUICKSORT(A, p, q-1)
4. QUICKSORT(A, q + 1,r)
3
10/31/2022
4
10/31/2022
Insertion Sort
• It is a simple sorting algorithm that builds the final
sorted array (or list) one item at a time.
• This algorithm is less efficient on large lists than more
advanced algorithms such as quicksort, heap sort, or
merge sort.
• However, insertion sort provides several advantages:
• Simple implementation
• Efficient for small data sets
• Stable; i.e., does not change the relative order of
elements with equal keys.
• In-place; i.e., only requires a constant amount O(1) of
additional m e m o r y space.
44 Dr. Sunil Kumar, CSE Dept., MIET Meerut
Insertion Sort
1
10/31/2022
Insertion Sort
1. We start by making the second element of the given array,
i.e. element at index 1, the key. The key element here is
the new card that we need to add to our existing sorted set
of cards(remember the example with cards above).
2. We compare the key element with the element(s) before
it, in this case, element at index 0:
If the key element is less than the first element, we insert
the key element before the first element.
If the key element is greater than the first element, then we insert it
after the first element.
3. Then, we make the third element of the array as key and
will compare it with elements to it's left and insert it at
the right position.
4. And we go on repeating this, until the array is sorted.
Insertion Sort
2
10/31/2022
Algorithm
Algorithm : Insertion Sort (A, N)
Step 1: Repeat Steps 2 to 5 for K = 1 to N-1
Step 2: SET TEMP = A[K]
Step 3: SET J = K-1
Step 4: Repeat while TEMP <=A[J]
(a) SET A[J + 1] = A[J]
(b) SET J = J - 1
Step 5: SET A[J + 1] = TEMP
Step 6: EXIT
3
10/31/2022
4
11/1/2022
Merge Sort
Merge Sort is based on the rule of Divide and
Conquer. But it doesn't divide the list into two
halves.
In merge sort, the unsorted list is divided into
N sub-lists, each having one element, because
a list of one element is considered sorted.
Then, it repeatedly merge these sub lists, to
produce new sorted sub lists, and at lasts one
sorted list is produced.
Merge Sort…
• DIVIDE: Divide the unsorted list into two sub
lists of about half the size.
• CONQUER: Sort each of the two sub-lists
recursively. If they are small enough just solve
them in a straight forward manner.
• COMBINE: Merge the two-sorted sub-lists back
into one sorted list.
1
11/1/2022
Merge Sort...
Merge Sort is quite fast, and has a time
complexity of O(n log n).
It is also a stable sort, which means the
equal elements are ordered in the same order
in the sorted list.
2
11/1/2022
Another Example
Algorithm
ALGO.: Merge-Sort(A, p, r)
1. if p < r // check for base case
2. then q (p + r)/2 // divide step
3. Merge-Sort (A, p, q) // conquer step
4. Merge-Sort (A, q + 1, r) // conquer step
5. Merge (A, p, q, r) // conquer step
3
11/1/2022
Algorithm...
MERGE (A, p, q, r ) 8. L[n1 + 1] ← ∞
1. n1 ← q − p + 1 9. R[n2 + 1] ← ∞
2. n2 ← r − q 10. i←1
3. Create arrays L[1 . . n1 + 1] 11. j←1
12. for k ← p to r
and R[1 . . n2 + 1]
13. do if L[i ] ≤ R[ j]
4. for i ← 1 to n1
14. then A[k] ← L[i]
5. do L[i] ← A[p + i − 1]
15. i←i+1
6. for j ← 1 to n2
16. else A[k] ← R[j]
7. do R[j] ← A[q + j ]
17. j←j+1
1 2 3 4 5 6 7 8
L 1 5 7 8 infinity 2 4 6 9 infinity
R
i=1 2 3 4 5 j=1 2 3 4 5
K 1
4
11/1/2022
L 1 5 7 8 infinity 2 4 6 9 infinity
R
i=1 2 3 4 5 j=1 2 3 4 5
K 1 2
L 1 5 7 8 infinity 2 4 6 9 infinity
R
i=1 2 3 4 5 j=1 2 3 4 5
K 1 2 4
5
11/1/2022
L 1 5 7 8 infinity 2 4 6 9 infinity
R
i=1 2 3 4 5 j=1 2 3 4 5
K 1 2 4 5
L 1 5 7 8 infinity 2 4 6 9 infinity
R
i=1 2 3 4 5 j=1 2 3 4 5
K 1 2 4 5 6
6
11/1/2022
L 1 5 7 8 infinity 2 4 6 9 infinity
R
i=1 2 3 4 5 j=1 2 3 4 5
K 1 2 4 5 6 7
L 1 5 7 8 infinity 2 4 6 9 infinity
R
i=1 2 3 4 5 j=1 2 3 4 5
K 1 2 4 5 6 7 8
7
11/1/2022
L 1 5 7 8 infinity 2 4 6 9 infinity
R
i=1 2 3 4 5 j=1 2 3 4 5
K 1 2 4 5 6 7 8 9
8
11/1/2022
Heap Sort
Heap Sort is one of the best sorting methods being
in-place and with no quadratic worst-case
scenarios.
Heap sort algorithm is divided into two basic
parts:
Creating a Heap of the unsorted list.
Then a sorted array is created by repeatedly
removing the largest/smallest element from the
heap, and inserting it into the array. The heap
is reconstructed after each removal.
Types of Heap
Max Heap
Min Heap
1
11/1/2022
1-Max Heap
Max-heap Definition:
• is a complete binary tree in which the value in
each internal node is greater than or equal to
the values in the children of that node.
Max-heap property:
The key of a node is ≥ than the keys of its children.
2-Min heap :
Min-Heap Definition:
is a complete binary tree in which the value in
each internal node is lower than or equal to the
values in the children of thatnode.
Min-Heap property:
The key of a node is <= than the keys of its
children.
2
11/1/2022
Example
Example...
3
11/1/2022
Example...
Example...
4
11/1/2022
Example...
Example...
5
11/1/2022
Example
6
11/2/2022
Algorithm
Convert an array A[1 … n] into a max-heap (n = length[A])
The elements in the subarray A[(n/2+1) .. n] are leaves
Apply MAX-HEAPIFY on elements between 1 and n/2
Algo: BUILD-MAX-HEAP(A) 1
4
1. n = length[A]
2 3
2. for i ← n/2 down to 1 1 3
4 5 6 7
3. do MAX-HEAPIFY(A, i, n) 8
2 9 10
16 9 10
14 8 7
A: 4 1 3 2 16 9 10 14 8 7
95
1
11/2/2022
HEAP-EXTRACT-MAX
Algo: HEAP-EXTRACT-MAX(A, n)
1. if n < 1
2. then error “heap underflow”
3. max ← A[1]
4. A[1] A[n]
5. MAX-HEAPIFY(A, 1, n-1)
6. return max
96
2
11/7/2022
Radix Sort
A multiple pass distribution sort algorithm that
distributes each item to a bucket according to part of
the item's key beginning with the least significant
part of the key.
After each pass, items are collected from the buckets,
keeping the items in order, then redistributed
according to the next most significant part of the key
and so on.
Radix Sort
The idea is to consider the key one character at a
time and to divide the entries, not into two sub
lists, but into as many sub-lists as there are
possibilities for the given character from the key.
If our keys, for example, are words or other
alphabetic strings, then we divide the list into 26
sub-lists at each stage.
That is, we set up a table of 26 lists and distribute
the entries into the lists according to one of the
characters in the key.
1
11/7/2022
Example
Example: Sort the numbers 348, 143, 361, 423, 538, 128, 321, 543, 366.
Input 0 1 2 3 4 5 6 7 8 9
348 348
143 143
361 361
423 423
Pass 1
538 538
128 128
321 321
543 543
366 366
Example...
Input 0 1 2 3 4 5 6 7 8 9
361 361
321 321
143 143
423 423
Pass 2
543 543
366 366
348 348
538 538
128 128
101 Dr. Sunil Kumar, CSE Dept., MIET Meerut
2
11/7/2022
Example...
Input 0 1 2 3 4 5 6 7 8 9
321 321
423 423
128 128
538 538
Pass 3
143 143
543 543
348 348
361 361
366 366
Example:
Sort the numbers 551, 12, 346, 311 using Radix sort.
3
11/7/2022
Algorithm
Step 1 - Define 10 queues each representing a bucket for
each digit from 0 to 9.
Step 2 - Consider the least significant digit of each number
in the list which is to be sorted.
Step 3 - Insert each number into their respective queue
based on the least significant digit.
Step 4 - Group all the numbers from queue 0 to queue 9 in
the order they have inserted into their respective queues.
Step 5 - Repeat from step 3 based on the next least
significant digit.
Step 6 - Repeat from step 2 until all the numbers are
grouped based on the most significant digit.
4
07-11-2022
Introduction to Hashing
Table of Contents
• Suppose that we want to store 10,000 students records (each with a 5-digit
• Concept of Hashing ID) in a given container.
• Collision Resolution Techniques used in
Hashing : A linked list implementation would take O(n) time.
– Open Hashing: Closed Addressing A height balanced tree would take O(log n) access time.
• Separate Chaining
– Closed Hashing: Open Addressing Using an array of size 100,000 would take O(1) access time but will
• Linear Probing lead to a lot of space wastage.
• Quadratic Probing
• Is there some way that we could get O(1) access time without wasting a lot
• Double Hashing of space?
1
07-11-2022
Dr. Sunil Kumar, CSE Dept., MIET Meerut 4 Dr. Sunil Kumar, CSE Dept., MIET Meerut 5
2
07-11-2022
Dr. Sunil Kumar, CSE Dept., MIET Meerut 6 Dr. Sunil Kumar, CSE Dept., MIET Meerut 7
3
07-11-2022
4
07-11-2022
Example
Separate Chaining Keys: 0, 1, 4, 9, 16, 25, 36, 49, 64, 81
Exercise: Represent the keys {89, 18, 49, 58, 69, 78} in hash table using separate chaining,
Dr. Sunil Kumar, CSE Dept., MIET Meerut 10 hash(key) = key % 10. 11
31
5
07-11-2022
Example: Load the keys {23, 13, 21, 14, 7, 8, and 15} in this order, in a hash table
of size 7 using separate chaining with the hash function: h(key)=key% 7.
6
08-11-2022
Dr. Sunil Kumar, CSE Dept., MIET Meerut 13 Dr. Sunil Kumar, CSE Dept., MIET Meerut 14
1
08-11-2022
Linear probing is easy to implement, but leads to clustering (long run of occupied slots).
Clusters can lead to poor performance, both for inserting and finding keys.
Dr. Sunil Kumar, CSE Dept., MIET Meerut 15 Dr. Sunil Kumar, CSE Dept., MIET Meerut
2
08-11-2022
3
08-11-2022
Quadratic Probing:
Quadratic Probing
Quadratic probing is a collision resolution method that eliminates
the primary clustering problem take place in a linear probing. • In general, searches the hash table beginning with the original
Compute: hash value = h(x) = x % table size location that the hash function specifies and continues at
When collision occur then the quadratic probing works as follows: increments of 12, 22, 32, and so on
(hash value + 12)% table size,
• Possible problem
if there is again collision occur then there exist rehashing.
(hash value + 22)%table size –Secondary clustering
if there is again collision occur then there exist rehashing.
(hash value = 32)% table size
In general in ith collision
hi(x)=(hash value +i2)%size
Dr. Sunil Kumar, CSE Dept., MIET Meerut 19 Dr. Sunil Kumar, CSE Dept., MIET Meerut 20
4
08-11-2022
Example: Insert keys {89, 18, 49, 58, 69 78} in order with the hash-table size 10 using
quadratic probing. Hash function: h(x)=x mod 10 • when x=69:
Solution: – h(69)=69%10=9 (Collision )
when x=89: – so use following hash function, h1(69)=(9 + 1)%10=0
h(89)=89%10=9 0 49
– again collision occurs use again the following hash function ,
insert key 89 in hash-table in location 9
1 – h2(69)=(9+ 22)%10=3
when x=18: 2 58 – insert key 69 in hash-table in location 3
h(18)=18%10=8 • when x=78:
insert key 18 in hash-table in location 8 3
– h(78)=78%10=8 (Collision )
4
when x=49: – so use following hash function, h1(78)=(8 + 1)%10=9 ; again collision occurs
h(49)=49%10=9 (Collision ) 5 – use again the following hash function ,
so use following hash function,
h1(49)=(9 + 1)%10=0
6 – h2(78)=(8+ 22)%10=2 ; again collision occurs, compute following step
hence insert key 49 in hash-table in location 0 7 – h3(78)=(8+ 32)%10=7
8 18 – insert key 58 in hash-table in location 7
when x=58:
h(58)=58%10=8 (Collision ) 9 89
so use following hash function, Fig. Hash table with keys
h1(58)=(8 + 1)%10=9 Using quadratic probing
again collision occur use again the following hash function ,
h2(58)=(8+ 22)%10=2
insert key 58 in hash-table in location 2
Dr. Sunil Kumar, CSE Dept., MIET Meerut 21 Dr. Sunil Kumar, CSE Dept., MIET Meerut 22
5
08-11-2022
0 1 2 3 4 5 6 7 8 9 10 11 12
44 41 18 32 59 73 22 31 m = 13