Data Structure B Tech 2 Year Unit 3 Notes
Data Structure B Tech 2 Year Unit 3 Notes
/CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Searching: Concept of Searching, Sequential search, Index Sequential Search, Binary-search. Concept of
Hashing & Collision resolution Techniques used in Hashing.
Sorting: Insertion Sort, Selection, Bubble Sort, Quick Sort, Merge Sort, Heap Sort and Radix Sort.
Introduction to Searching -
Searching is the fundamental process of locating a specific element or item within a collection of data.
This collection of data can take various forms, such as arrays, lists, trees, or other structured
representations.
The primary objective of searching is to determine whether the desired element exists within the data, and
if so, to identify its precise location or retrieve it. It plays an important role in various computational tasks
and real-world applications, including information retrieval, data analysis, decision-making processes, and
more.
Importance of Searching in DSA
Efficiency: Efficient searching algorithms improve program performance.
Data Retrieval: Quickly find and retrieve specific data from large datasets.
Database Systems: Enables fast querying of databases.
Problem Solving: Used in a wide range of problem-solving tasks.
Characteristics of Searching
Understanding the characteristics of searching in data structures and algorithms is crucial for designing
efficient algorithms and making informed decisions about which searching technique to employ. Here, we
explore key aspects and characteristics associated with searching:
1. Target Element:
In searching, there is always a specific target element or item that you want to find within the data
collection. This target could be a value, a record, a key, or any other data entity of interest.
2. Search Space:
The search space refers to the entire collection of data within which you are looking for the target element.
Depending on the data structure used, the search space may vary in size and organization.
3. Complexity:
Searching can have different levels of complexity depending on the data structure and the algorithm used.
The complexity is often measured in terms of time and space requirements.
4. Deterministic vs Non-deterministic:
Some searching algorithms, like binary search, are deterministic, meaning they follow a clear and
systematic approach. Others, such as linear search, are non-deterministic, as they may need to examine the
entire search space in the worst case.
Applications of Searching:
Searching algorithms have numerous applications across various fields. Here are some common
applications:
Information Retrieval: Search engines like Google, Bing, and Yahoo use sophisticated searching
algorithms to retrieve relevant information from vast amounts of data on the web.
Database Systems: Searching is fundamental in database systems for retrieving specific data
records based on user queries, improving efficiency in data retrieval.
Page 1 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
E-commerce: Searching is crucial in e-commerce platforms for users to find products quickly
based on their preferences, specifications, or keywords.
Networking: In networking, searching algorithms are used for routing packets efficiently through
networks, finding optimal paths, and managing network resources.
Artificial Intelligence: Searching algorithms play a vital role in AI applications, such as problem-
solving, game playing (e.g., chess), and decision-making processes
Pattern Recognition: Searching algorithms are used in pattern matching tasks, such as image
recognition, speech recognition, and handwriting recognition.
Searching Algorithms:
Searching Algorithms are designed to check for an element or retrieve an element from any data structure
where it is stored.
Below are some searching algorithms:
1. Linear Search
2. Binary Search
3. Jump Search
4. Ternary Search
5. Interpolation Search
6. Fibonacci Search
7. Exponential Search
1. Linear Search:
Linear Search, also known as Sequential Search, is one of the simplest and most straightforward searching
algorithms. It works by sequentially examining each element in a collection of data(array or list) until a
match is found or the entire collection has been traversed.
Linear Search
Page 2 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Start from the first element (index 0) and compare key with each element (arr[i]).
Comparing key with first element arr[0]. Since not equal, the iterator moves to the next
element as a potential match.
Comparing key with next element arr[1]. Since not equal, the iterator moves to the next
element as a potential match
.
Now when comparing arr[2] with key, the value matches. So the Linear Search Algorithm
will yield a successful message and return the index of the element when key is found.
Key is less than the current mid 56. The search space moves to the left.
If the key matches the value of the mid element, the element is found and stop search.
Page 5 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Page 6 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Page 7 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
1) Separate Chaining
The idea behind Separate Chaining is to make each cell of the hash table point to a linked list of records
that have the same hash function value. Chaining is simple but requires additional memory outside the
table.
Example: We have given a hash function and we have to insert some elements in the hash table using a
separate chaining method for collision resolution technique.
Hash function = key % 5,
Elements = 12, 15, 22, 25 and 37.
Let’s see step by step approach to how to solve the above problem:
1/5
Hence In this way, the separate chaining method is used as the collision resolution technique.
2) Open Addressing
In open addressing, all elements are stored in the hash table itself. Each table entry contains either a record
Page 8 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
or NIL. When searching for an element, we examine the table slots one by one until the desired element is
found or it is clear that the element is not in the table.
2.a) Linear Probing
In linear probing, the hash table is searched sequentially that starts from the original location of the hash.
If in case the location that we get is already occupied, then we check for the next location.
Algorithm:
1. Calculate the hash key. i.e. key = data % size
2. Check, if hashTable[key] is empty
store the value directly by hashTable[key] = data
3. If the hash index already has some value then
check for next index using key = (key+1) % size
4. Check, if the next index is available hashTable[key] then store the value. Otherwise try
for next index.
5. Do the above process till we find the space.
Example: Let us consider a simple hash function as “key mod 5” and a sequence of keys that are to be
inserted are 50, 70, 76, 85, 93.
1/6
Page 9 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
1/4
Page 10 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
4/5
Page 11 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Join GfG 160, a 160-day journey of coding challenges aimed at sharpening your skills. Each day, solve a
handpicked problem, dive into detailed solutions through articles and videos, and enhance your
preparation for any interview—all for free! Plus, win exciting GfG goodies along the way! - Explore Now
1. Linear Probing:
In linear probing, the hash table is searched sequentially that starts from the original location of the hash.
If in case the location that we get is already occupied, then we check for the next location.
The function used for rehashing is as follows: rehash(key) = (n+1)%table-size.
For example, The typical gap between two probes is 1 as seen in the example below:
Let hash(x) be the slot index computed using a 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
Example: Let us consider a simple hash function as “key mod 5” and a sequence of keys that are to be
inserted are 50, 70, 76, 85, 93.
Page 12 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
1/6
Implementation : Please refer Program to implement Hash Table using Open Addressing
2. Quadratic Probing
If you observe carefully, then you will understand that the interval between probes will increase
proportionally to the hash value. Quadratic probing is a method with the help of which we can solve the
problem of clustering that was discussed above. This method is also known as the mid-square method. In
this method, we look for the i2‘th slot in the ith iteration. We always start from the original hash location.
If only the location is occupied then we check the other slots.
let hash(x) be the slot index computed using hash function.
If slot hash(x) % S is full, then we try (hash(x) + 1*1) % S
If (hash(x) + 1*1) % S is also full, then we try (hash(x) + 2*2) % S
If (hash(x) + 2*2) % S is also full, then we try (hash(x) + 3*3) % S
Example: Let us consider table Size = 7, hash function as Hash(x) = x % 7 and collision resolution
strategy to be f(i) = i2 . Insert = 22, 30, and 50.
Page 13 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
1/4
Implementation : Please refer Program for Quadratic Probing in Hashing
3. Double Hashing
The intervals that lie between probes are computed by another hash function. Double hashing is a
technique that reduces clustering in an optimized way. In this technique, the increments for the probing
sequence are computed by using another hash function. We use another hash function hash2(x) and look
for the i*hash2(x) slot in the ith rotation.
let hash(x) be the slot index computed using hash function.
If slot hash(x) % S is full, then we try (hash(x) + 1*hash2(x)) % S
If (hash(x) + 1*hash2(x)) % S is also full, then we try (hash(x) + 2*hash2(x)) % S
If (hash(x) + 2*hash2(x)) % S is also full, then we try (hash(x) + 3*hash2(x)) % S
Example: Insert the keys 27, 43, 692, 72 into the Hash Table of size 7. where first hash-function
is h1 (k) = k mod 7 and second hash-function is h2(k) = 1 + (k mod 5)
Page 14 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
2/5
Comparison of the above three:
Open addressing is a collision handling technique used in hashing where, when a collision occurs (i.e.,
when two or more keys map to the same slot), the algorithm looks for another empty slot in the hash table
to store the collided key.
In linear probing, the algorithm simply looks for the next available slot in the hash table and
places the collided key there. If that slot is also occupied, the algorithm continues searching for the
next available slot until an empty slot is found. This process is repeated until all collided keys have
been stored. Linear probing has the best cache performance but suffers from clustering. One more
advantage of Linear probing is easy to compute.
In quadratic probing, the algorithm searches for slots in a more spaced-out manner. When a
collision occurs, the algorithm looks for the next slot using an equation that involves the original hash
value and a quadratic function. If that slot is also occupied, the algorithm increments the value of the
quadratic function and tries again. This process is repeated until an empty slot is found. Quadratic
probing lies between the two in terms of cache performance and clustering.
Page 15 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
In double hashing, the algorithm uses a second hash function to determine the next slot to check
when a collision occurs. The algorithm calculates a hash value using the original hash function, then
uses the second hash function to calculate an offset. The algorithm then checks the slot that is the sum
of the original hash value and the offset. If that slot is occupied, the algorithm increments the offset
and tries again. This process is repeated until an empty slot is found. Double hashing has poor cache
performance but no clustering. Double hashing requires more computation time as two hash functions
need to be computed.
The choice of collision handling technique can have a significant impact on the performance of a hash
table. Linear probing is simple and fast, but it can lead to clustering (i.e., a situation where keys are stored
in long contiguous runs) and can degrade performance. Quadratic probing is more spaced out, but it can
also lead to clustering and can result in a situation where some slots are never checked. Double hashing is
more complex, but it can lead to more even distribution of keys and can provide better performance in
some cases.
Chaining is Less sensitive to the hash function or Open addressing requires extra care to avoid
3.
load factors. clustering and load factor.
Wastage of Space (Some Parts of hash table in In Open addressing, a slot can be used even
6.
chaining are never used). if an input doesn’t map to it.
Note: Cache performance of chaining is not good because when we traverse a Linked List, we are
basically jumping from one node to another, all across the computer’s memory. For this reason, the CPU
cannot cache the nodes which aren’t visited yet, this doesn’t help us. But with Open Addressing, data isn’t
spread, so if the CPU detects that a segment of memory is constantly being accessed, it gets cached for
quick access.
Performance of Open Addressing:
Page 16 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Like Chaining, the performance of hashing can be evaluated under the assumption that each key is equally
likely to be hashed to any slot of the table (simple uniform hashing)
m = Number of slots in the hash table
n = Number of keys to be inserted in the hash table
Load factor α = n/m ( < 1 )
Expected time to search/insert/delete < 1/(1 – α)
So Search, Insert and Delete take (1/(1 – α)) time
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current element, then move to the next
element. Else, shift greater elements in the array towards the right.
Advertisement
Step 5 - Insert the value.
Page 17 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
To deepen your understanding of sorting algorithms like insertion sort and other essential data
structures
Although it is simple to use, it is not appropriate for large data sets as the time complexity of insertion sort
in the average case and worst case is O(n2), where n is the number of items. Insertion sort is less efficient
than the other sorting algorithms like heap sort, quick sort, merge sort, etc.
Selection sort is a simple sorting algorithm. This sorting algorithm, like insertion sort, is an in-place
comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and
the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.
The smallest element is selected from the unsorted array and swapped with the leftmost element, and that
element becomes a part of the sorted array. This process continues moving unsorted array boundaries by
one element to the right.
This algorithm is not suitable for large data sets as its average and worst case complexities are of O(n2),
where n is the number of items.
This type of sorting is called Selection Sort as it works by repeatedly sorting elements. That is: we first
find the smallest value in the array and exchange 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 we continue the
process in this way until the entire array is sorted.
Page 18 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Pseudocode
In the worst case, this could be quadratic, but in the average case, this quantity is O(n log n). It implies
that the running time of Selection sort is quite insensitive to the input.
Example
For the first position in the sorted list, the whole list is scanned sequentially. The first position where 14 is
stored presently, we search the whole list and find that 10 is the lowest value.
Page 19 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the list,
appears in the first position of the sorted list.
For the second position, where 33 is residing, we start scanning the rest of the list in a linear manner.
We find that 14 is the second lowest value in the list and it should appear at the second place. We swap
these values.
After two iterations, two least values are positioned at the beginning in a sorted manner.
The same process is applied to the rest of the items in the array −
Page 20 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Page 21 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Bubble Sort is an elementary sorting algorithm, which works by repeatedly exchanging adjacent elements,
if necessary. When no exchanges are required, the file is sorted.
We assume list is an array of n elements. We further assume that swap function swaps the values of the
given array elements.
Step 1 − Check if the first element in the input array is greater than the next element in the array.
Step 2 − If it is greater, swap the two elements; otherwise move the pointer forward in the array.
Step 4 − Check if the elements are sorted; if not, repeat the same process (Step 1 to Step 3) from the last
element of the array to the first.
Pseudocode
We observe in algorithm that Bubble Sort compares each pair of array element unless the whole array is
completely sorted in an ascending order. This may cause a few complexity issues like what if the array
needs no more swapping as all the elements are already ascending.
Page 22 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
To ease-out the issue, we use one flag variable swapped which will help us see if any swap has happened
or not. If no swap has occurred, i.e. the array requires no more processing to be sorted, it will come out of
the loop.
Analysis
In this algorithm, the number of comparison is irrespective of the data set, i.e. whether the provided input
elements are in sorted order or in reverse order or at random.
Memory Requirement
From the algorithm stated above, it is clear that bubble sort does not require extra memory.
Example
We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it short and
precise.
Bubble sort starts with very first two elements, comparing them to check which one is greater.
Page 23 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with 27.
We find that 27 is smaller than 33 and these two values must be swapped.
Next we compare 33 and 35. We find that both are in already sorted positions.
We know then that 10 is smaller 35. Hence they are not sorted. We swap these values. We find that we
have reached the end of the array. After one iteration, the array should look like this −
Page 24 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
To be precise, we are now showing how an array should look like after each iteration. After the second
iteration, it should look like this −
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sort learns that an array is completely sorted.
Page 25 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
In this article, we will discuss the Quicksort Algorithm. The working procedure of Quicksort is also
simple. This article will be very helpful and interesting to students as they might face quicksort as a
question in their examinations. So, it is important to discuss the topic.
Sorting is a way of arranging items in a systematic manner. Quicksort is the widely used sorting algorithm
that makes n log n comparisons in average case for sorting an array of n elements. It is a faster and highly
efficient sorting algorithm. This algorithm follows the divide and conquer approach. Divide and conquer is
a technique of breaking down the algorithms into subproblems, then solving the subproblems, and
combining the results back together to solve the original problem.
Divide: In Divide, first pick a pivot element. After that, partition or rearrange the array into two sub-arrays
such that each element in the left sub-array is less than or equal to the pivot element and each element in
the right sub-array is larger than the pivot element.
Quicksort picks an element as pivot, and then it partitions the given array around the picked pivot element.
In quick sort, a large array is divided into two arrays in which one holds values that are smaller than the
specified value (Pivot), and another array holds the values that are greater than the pivot.
After that, left and right sub-arrays are also partitioned using the same approach. It will continue until the
single element remains in the sub-array.
o Pivot can be random, i.e. select the random pivot from the given array.
o Pivot can either be the rightmost element of the leftmost element of the given array.
o Select median as the pivot element.
Page 26 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Algorithm:
To understand the working of quick sort, let's take an unsorted array. It will make the concept more clear
and understandable.
In the given array, we consider the leftmost element as pivot. So, in this case, a[left] = 24, a[right] = 27
and a[pivot] = 24.
Since, pivot is at left, so algorithm starts from right and move towards left.
Page 27 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Now, a[pivot] < a[right], so algorithm moves forward one position towards left, i.e. -
Advertisement
Because, a[pivot] > a[right], so, algorithm will swap a[pivot] with a[right], and pivot moves to right, as -
Now, a[left] = 19, a[right] = 24, and a[pivot] = 24. Since, pivot is at right, so algorithm starts from left and
moves to right.
Page 28 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Now, a[left] = 9, a[right] = 24, and a[pivot] = 24. As a[pivot] > a[left], so algorithm moves one position to
right as -
Now, a[left] = 29, a[right] = 24, and a[pivot] = 24. As a[pivot] < a[left], so, swap a[pivot] and a[left], now
pivot is at left, i.e. -
Since, pivot is at left, so algorithm starts from right, and move to left. Now, a[left] = 24, a[right] = 29, and
a[pivot] = 24. As a[pivot] < a[right], so algorithm moves one position to left, as -
Page 29 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Now, a[pivot] = 24, a[left] = 24, and a[right] = 14. As a[pivot] > a[right], so, swap a[pivot] and a[right],
now pivot is at right, i.e. -
Now, a[pivot] = 24, a[left] = 14, and a[right] = 24. Pivot is at right, so the algorithm starts from left and
move to right.
Now, a[pivot] = 24, a[left] = 24, and a[right] = 24. So, pivot, left and right are pointing the same element.
It represents the termination of procedure.
Element 24, which is the pivot element is placed at its exact position.
Elements that are right side of element 24 are greater than it, and the elements that are left side of element
24 are smaller than it.
Now, in a similar manner, quick sort algorithm is separately applied to the left and right sub-arrays. After
sorting gets done, the array will be -
Page 30 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Quicksort complexity
Now, let's see the time complexity of quicksort in best case, average case, and in worst case. We will also
see the space complexity of quicksort.
o Best Case Complexity - In Quicksort, the best-case occurs when the pivot element is the middle
element or near to the middle element. The best-case time complexity of quicksort is O(n*logn).
o Average Case Complexity - It occurs when the array elements are in jumbled order that is not
properly ascending and not properly descending. The average case time complexity of quicksort
is O(n*logn).
o Worst Case Complexity - In quick sort, worst case occurs when the pivot element is either
greatest or smallest element. Suppose, if the pivot element is always the last element of the array,
the worst case would occur when the given array is sorted already in ascending or descending
order. The worst-case time complexity of quicksort is O(n2).
Though the worst-case complexity of quicksort is more than other sorting algorithms such as Merge
sort and Heap sort, still it is faster in practice. Worst case in quick sort rarely occurs because by changing
the choice of pivot, it can be implemented in different ways. Worst case in quicksort can be avoided by
choosing the right pivot element.
2. Space Complexity
Stable NO
Page 31 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Merge Sort
The Merge Sort algorithm is a divide-and-conquer algorithm that sorts an array by first breaking
it down into smaller arrays, and then building the array back together the correct way so that it is
sorted.
Merge Sort
Divide: The algorithm starts with breaking up the array into smaller and smaller pieces until one
such sub-array only consists of one element.
Conquer: The algorithm merges the small pieces of the array back together by putting the lowest
values first, resulting in a sorted array.
The breaking down and building up of the array to sort the array is done recursively.
In the animation above, each time the bars are pushed down represents a recursive call, splitting
the array into smaller pieces. When the bars are lifted up, it means that two sub-arrays have been
merged together.
The Merge Sort algorithm can be described like this:
How it works:
Divide the unsorted array into two sub-arrays, half the size of the original.
Continue to divide the sub-arrays as long as the current piece of the array has more than one
element.
Merge two sub-arrays together by always putting the lowest value first.
Keep merging until there are no sub-arrays left.
Take a look at the drawing below to see how Merge Sort works from a different perspective. As
you can see, the array is split into smaller and smaller pieces until it is merged back together. And
as the merging happens, values from each sub-array are compared so that the lowest value comes
first.
Merge Sort
Step 1: We start with an unsorted array, and we know that it splits in half until the sub-arrays only
consist of one element. The Merge Sort function calls itself two times, once for each half of the
array. That means that the first sub-array will split into the smallest pieces first.
[ 12, 8, 9, 3, 11, 5, 4]
[ 12, 8, 9] [ 3, 11, 5, 4]
[ 12] [ 8, 9] [ 3, 11, 5, 4]
[ 12] [ 8] [ 9] [ 3, 11, 5, 4]
Step 2: The splitting of the first sub-array is finished, and now it is time to merge. 8 and 9 are the
first two elements to be merged. 8 is the lowest value, so that comes before 9 in the first merged
sub-array.
Page 32 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
[ 12] [ 8, 9] [ 3, 11, 5, 4]
Step 3: The next sub-arrays to be merged is [ 12] and [ 8, 9]. Values in both arrays are compared
from the start. 8 is lower than 12, so 8 comes first, and 9 is also lower than 12.
[ 8, 9, 12] [ 3, 11, 5, 4]
Step 4: Now the second big sub-array is split recursively.
[ 8, 9, 12] [ 3, 11, 5, 4]
[ 8, 9, 12] [ 3, 11] [ 5, 4]
[ 8, 9, 12] [ 3] [ 11] [ 5, 4]
Step 5: 3 and 11 are merged back together in the same order as they are shown because 3 is lower
than 11.
[ 8, 9, 12] [ 3, 11] [ 5, 4]
Step 6: Sub-array with values 5 and 4 is split, then merged so that 4 comes before 5.
[ 8, 9, 12] [ 3, 11] [ 5] [ 4]
[ 8, 9, 12] [ 3, 11] [ 4, 5]
Step 7: The two sub-arrays on the right are merged. Comparisons are done to create elements in
the new merged array:
3 is lower than 4
4 is lower than 11
5 is lower than 11
11 is the last remaining value
[ 8, 9, 12] [ 3, 4, 5, 11]
Step 8: The two last remaining sub-arrays are merged. Let's look at how the comparisons are done
in more detail to create the new merged and finished sorted array:
3 is lower than 8:
What is a heap?
A heap is a complete binary tree, and the binary tree is a tree in which the node can have the utmost two
children. A complete binary tree is a binary tree in which all the levels except the last level, i.e., leaf node,
should be completely filled, and all the nodes should be left-justified.
Heapsort is a popular and efficient sorting algorithm. The concept of heap sort is to eliminate the elements
one by one from the heap part of the list, and then insert them into the sorted part of the list.
Page 34 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Algorithm
. HeapSort(arr)
. BuildMaxHeap(arr)
. for i = length(arr) to 2
. swap arr[1] with arr[i]
. heap_size[arr] = heap_size[arr] ? 1
. MaxHeapify(arr,1)
. End
BuildMaxHeap(arr)
. BuildMaxHeap(arr)
. heap_size(arr) = length(arr)
. for i = length(arr)/2 to 1
. MaxHeapify(arr,i)
. End
MaxHeapify(arr,i)
. MaxHeapify(arr,i)
. L = left(i)
. R = right(i)
. if L ? heap_size[arr] and arr[L] > arr[i]
. largest = L
. else
. largest = i
. if R ? heap_size[arr] and arr[R] > arr[largest]
. largest = R
. if largest != i
. swap arr[i] with arr[largest]
. MaxHeapify(arr,largest)
. End
Advertisement
In heap sort, basically, there are two phases involved in the sorting of elements. By using the heap sort
algorithm, they are as follows -
o The first step includes the creation of a heap by adjusting the elements of the array.
o After the creation of heap, now remove the root element of the heap repeatedly by shifting it to the
end of the array, and then store the heap structure with the remaining elements.
Page 35 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Now let's see the working of heap sort in detail by using an example. To understand it more clearly, let's
take an unsorted array and try to sort it using heap sort. It will make the explanation clearer and easier.
First, we have to construct a heap from the given array and convert it into max heap.
After converting the given heap into max heap, the array elements are -
Next, we have to delete the root element (89) from the max heap. To delete this node, we have to swap it
with the last node, i.e. (11). After deleting the root element, we again have to heapify it to convert it into
max heap.
After swapping the array element 89 with 11, and converting the heap into max-heap, the elements of
array are -
In the next step, again, we have to delete the root element (81) from the max heap. To delete this node, we
have to swap it with the last node, i.e. (54). After deleting the root element, we again have to heapify it to
convert it into max heap.
Page 36 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
After swapping the array element 81 with 54 and converting the heap into max-heap, the elements of array
are -
In the next step, we have to delete the root element (76) from the max heap again. To delete this node, we
have to swap it with the last node, i.e. (9). After deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 76 with 9 and converting the heap into max-heap, the elements of array
are -
In the next step, again we have to delete the root element (54) from the max heap. To delete this node, we
have to swap it with the last node, i.e. (14). After deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 54 with 14 and converting the heap into max-heap, the elements of array
are -
Page 37 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
In the next step, again we have to delete the root element (22) from the max heap. To delete this node, we
have to swap it with the last node, i.e. (11). After deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 22 with 11 and converting the heap into max-heap, the elements of array
are -
In the next step, again we have to delete the root element (14) from the max heap. To delete this node, we
have to swap it with the last node, i.e. (9). After deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 14 with 9 and converting the heap into max-heap, the elements of array
are -
In the next step, again we have to delete the root element (11) from the max heap. To delete this node, we
have to swap it with the last node, i.e. (9). After deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 11 with 9, the elements of array are -
Now, heap has only one element left. After deleting it, heap will be empty.
Page 38 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
o Best Case Complexity - It occurs when there is no sorting required, i.e. the array is already sorted.
The best-case time complexity of heap sort is O(n logn).
o Average Case Complexity - It occurs when the array elements are in jumbled order that is not
properly ascending and not properly descending. The average case time complexity of heap sort
is O(n log n).
o Worst Case Complexity - It occurs when the array elements are required to be sorted in reverse
order. That means suppose you have to sort the array elements in ascending order, but its elements
are in descending order. The worst-case time complexity of heap sort is O(n log n).
The time complexity of heap sort is O(n logn) in all three cases (best case, average case, and worst case).
Stable N0
The height of a complete binary tree having n elements is logn.
2. Space Complexity
Page 39 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
The process of radix sort works similar to the sorting of students names, according to the alphabetical
order. In this case, there are 26 radix formed due to the 26 alphabets in English. In the first pass, the names
of students are grouped according to the ascending order of the first letter of their names. After that, in the
second pass, their names are grouped according to the ascending order of the second letter of their name.
And the process continues until we find the sorted list.
Algorithm
. radixSort(arr)
. max = largest element in the given array
. d = number of digits in the largest element (or, max)
. Now, create d buckets of size 0 - 9
. for i -> 0 to d
. sort the array elements using counting sort (or any stable sort) according to the digits at
. the ith place
The steps used in the sorting of radix sort are listed as follows -
o First, we have to find the largest element (suppose max) from the given array. Suppose 'x' be the
number of digits in max. The 'x' is calculated because we need to go through the significant places
of all elements.
o After that, go through one by one each significant place. Here, we have to use any stable sorting
algorithm to sort the digits of each significant place.
Now let's see the working of radix sort in detail by using an example. To understand it more clearly, let's
take an unsorted array and try to sort it using radix sort. It will make the explanation clearer and easier.
In the given array, the largest element is 736 that have 3 digits in it. So, the loop will run up to three times
(i.e., to the hundreds place). That means three passes are required to sort the array.
Now, first sort the elements on the basis of unit place digits (i.e., x = 0). Here, we are using the counting
sort algorithm to sort the elements.
Pass 1:
In the first pass, the list is sorted on the basis of the digits at 0's place.
Page 40 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Pass 2:
In this pass, the list is sorted on the basis of the next significant digits (i.e., digits at 10th place).
Page 41 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
Pass 3:
In this pass, the list is sorted on the basis of the next significant digits (i.e., digits at 100th place).
1. Time Complexity
o Best Case Complexity - It occurs when there is no sorting required, i.e. the array is already sorted.
Page 42 of 43
B. TECH. /CSE-IIInd SEM / BCS-301: DATA STRUCTURE / UNIT-III / Introduction, Searching & Sorting
Amit Kumar Jaiswal / Assistant Professor /Department of CSE /JMS Institute of Technology-Ghaziabad
o Worst Case Complexity - It occurs when the array elements are required to be sorted in reverse
order. That means suppose you have to sort the array elements in ascending order, but its elements
are in descending order. The worst-case time complexity of Radix sort is O(nk).
Radix sort is a non-comparative sorting algorithm that is better than the comparative sorting algorithms. It
has linear time complexity that is better than the comparative algorithms with complexity O(n logn).
2. Space Complexity
Stable YES
o The space complexity of Radix sort is O(n + k).
Following is a quick revision sheet that you may refer to at the last minute:
Page 43 of 43