Insertion Sort Algorithm Explained
Insertion Sort Algorithm Explained
Insertion Sort :
I. Insertion sort is a sorting algorithm that places an unsorted element at its
suitable place in each iteration.
II. Insertion sort works similar to the sorting of playing cards in hands. It is
assumed that the first card is already sorted in the card game, and then we
select an unsorted card.
III. If the selected unsorted card is greater than the first card, it will be placed at
the right side; otherwise, it will be placed at the left side.
IV. Similarly, all unsorted cards are taken and put in their exact [Link] same
approach is applied in insertion sort.
V. The idea behind the insertion sort is that first take one element, iterate it
through the sorted array.
VI. 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.
VII. Insertion sort has various advantages such as -
VIII. Simple implementation
Efficient for small data sets
Adaptive, i.e., it is appropriate for data sets that are already substantially
sorted.
A. Simple implementation
B. Efficient for small data sets
C. Adaptive, i.e., it is appropriate for data sets that are already substantially
sorted.
IX. Algorithm of insertion sort: The simple steps of achieving the insertion
sort are listed as follows -
Step1 - If the element is the first element, assume that it is already sorted.
Return 1.
Step2 - Pick the next element, and store it separately in a key.
Step3 - Now, compare the key with all elements in the sorted array.
Step4 - 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.
Step 5 - Insert the value.
Step 6 - Repeat until the array is sorted.
Time Complexity Case Time Complexity
1. Best Case O(n)
2. Average Case O(n2)
3. Worst Case O(n2)
a) Best Case Complexity - It occurs when there is no
sorting required, i.e. the array is already sorted. The best-
case time complexity of insertion sort is O(n).
b) 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 insertion sort is O(n2).
c) 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 insertion sort is
O(n2).
d) Space Complexity : Space Complexity O(1)Stable YES.
The space complexity of insertion sort is O(1). It is
because, in insertion sort, an extra variable is required for
swapping.
Selection Sort
Selection sort is a simple and efficient sorting algorithm that works by
repeatedly selecting the smallest (or largest) element from the unsorted
portion of the list and moving it to the sorted portion of the list.
The algorithm repeatedly selects the smallest (or largest) element from the
unsorted portion of the list and swaps it with the first element of the unsorted
part. This process is repeated for the remaining unsorted portion until the
entire list is sorted.
Let's consider the following array as an example: arr[]={64, 25, 12, 22, 11}
e) First pass:
(1) For the first position in the sorted array, the whole
array is traversed from index 0 to 4 sequentially.
The first position where 64 is stored presently, after
traversing the whole array it is clear that 11 is the
lowest value.
(2) Thus, replace 64 with 11. After one iteration 11,
which happens to be the least value in the array,
tends to appear in the first position of the sorted
list.
f) Second Pass:
(1) For the second position, where 25 is present, again
traverse the rest of the array in a sequential
manner.
(2) After traversing, we found that 12 is the second
lowest value in the array and it should appear at
the second place in the array, thus swap these
values.
g) Third Pass:
(1) Now, for third place, where 25 is present again,
traverse the rest of the array and find the third
least value present in the array.
(2) While traversing, 22 came out to be the third least
value and it should appear at the third place in the
array, thus swap 22 with element present at third
position.
h) Fourth pass:
(1) Similarly, for fourth position traverse the rest of the
array and find the fourth least element in the array
(2) As 25 is the 4th lowest value hence, it will place at
the fourth position.
i) Fifth Pass:
(1) At last the largest value present in the array
automatically get placed at the last position in the
array
(2) The resulting array is the sorted array.
How does Merge Sort work? : Merge sort is a recursive algorithm that continuously
splits the array in half until it cannot be further divided i.e., the array has only one
element left (an array with one element is always sorted). Then the sorted
subarrays are merged into one sorted array.
See the below illustration to understand the working of merge sort.
Illustration:
Let's consider an array arr[] = {38, 27, 43, 10}
● Initially divide the array into two equal halves:
● These subarrays are further divided into two halves. Now they become array
of unit length that can no longer be divided and array of unit length are always
sorted.
These sorted subarrays are merged together, and we get bigger sorted subarrays.
This merging process is continued until the sorted array is built from the smaller
subarrays.
The following diagram shows the complete merge sort process for
an example array {38, 27, 43, 10}.
QuickSort – Data Structure and Algorithm Tutorials :
QuickSort is a sorting algorithm based on the Divide and Conquer algorithm that
picks an element as a pivot and partitions the given array around the picked pivot
by placing the pivot in its correct position in the sorted array.
How does QuickSort work?
The key process in quickSort is a partition(). The target of partitions is to place the
pivot (any element can be chosen to be a pivot) at its correct position in the sorted
array and put all smaller elements to the left of the pivot, and all greater elements
to the right of the pivot.
Partition is done recursively on each side of the pivot after the pivot is placed in its
correct position and this finally sorts the array.
The working procedure of insertion sort is also simple. This article will be very
helpful and interesting to students as they might face insertion sort as a question in
their examinations. So, it is important to discuss the [Link] sort works
similar to the sorting of playing cards in hands. It is assumed that the first card is
already sorted in the card game, and then we select an unsorted card. If the
selected unsorted card is greater than the first card, it will be placed at the right
side; otherwise, it will be placed at the left side. Similarly, all unsorted cards are
taken and put in their exact [Link] same approach is applied in insertion sort.
The idea behind the insertion sort is that first take one element, iterate it through
the sorted array. 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.
○ Simple implementation
○ Efficient for small data sets
○ Adaptive, i.e., it is appropriate for data sets that are already substantially
sorted.
The simple steps of achieving the insertion sort are listed as follows -
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.
To understand the working of the insertion sort algorithm, let's take an unsorted
array. It will be easier to understand the insertion sort via an example.
Here, 31 is greater than 12. That means both elements are already in ascending
order. So, for now, 12 is stored in a sorted sub-array.
Here, 25 is smaller than 31. So, 31 is not at the correct position. Now, swap 31 with
25. Along with swapping, insertion sort will also check it with all elements in the
sorted array.
For now, the sorted array has only one element, i.e. 12. So, 25 is greater than 12.
Hence, the sorted array remains sorted after swapping.
Now, two elements in the sorted array are 12 and 25. Move forward to the next
elements that are 31 and 8.
Now, the sorted array has three items that are 8, 12 and 25. Move to the next items
that are 31 and 32.
Hence, they are already sorted. Now, the sorted array includes 8, 12, 25 and 31.
Now, let's see the time complexity of insertion sort in best case, average case, and
in worst case. We will also see the space complexity of insertion sort.
1. Time Complexity
○ 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 insertion sort is O(n2).
2. Space Complexity
Stable YES
Selection Sort : Selection sort is a simple and efficient sorting algorithm that
works by repeatedly selecting the smallest (or largest) element from the unsorted
portion of the list and moving it to the sorted portion of the list.
The algorithm repeatedly selects the smallest (or largest) element from the
unsorted portion of the list and swaps it with the first element of the unsorted part.
This process is repeated for the remaining unsorted portion until the entire list is
sorted.
Lets consider the following array as an example: arr[] = {64, 25, 12, 22,
11}
First pass:
● For the first position in the sorted array, the whole array is traversed from
index 0 to 4 sequentially. The first position where 64 is stored presently, after
traversing the whole array it is clear that 11 is the lowest value.
● Thus, replace 64 with 11. After one iteration 11, which happens to be the least
value in the array, tends to appear in the first position of the sorted list.
Second Pass:
● For the second position, where 25 is present, again traverse the rest of the
array in a sequential manner.
● After traversing, we found that 12 is the second lowest value in the array and it
should appear at the second place in the array, thus swap these values.
Third Pass:
● Now, for third place, where 25 is present again, traverse the rest of the array
and find the third least value present in the array.
● While traversing, 22 came out to be the third least value and it should appear at
the third place in the array, thus swap 22 with element present at third position.
Fourth pass:
● Similarly, for fourth position traverse the rest of the array and find the fourth
least element in the array
● As 25 is the 4th lowest value hence, it will place at the fourth position.
Fifth Pass:
● At last the largest value present in the array automatically get placed at the
last position in the array
Merge Sort –Merge sort is defined as a sorting algorithm that works by dividing an
array into smaller subarrays, sorting each subarray, and then merging the sorted
subarrays back together to form the final sorted array.
How does Merge Sort work? : Merge sort is a recursive algorithm that
continuously splits the array in half until it cannot be further divided i.e., the array
has only one element left (an array with one element is always sorted). Then the
sorted subarrays are merged into one sorted array.
See the below illustration to understand the working of merge sort.
Illustration:
● These subarrays are further divided into two halves. Now they
become array of unit length that can no longer be divided and array of
unit length are always sorted.
These sorted subarrays are merged together, and we get bigger sorted subarrays.
This merging process is continued until the sorted array is built from the smaller
subarrays.
The following diagram shows the complete merge sort process for an example array
{38, 27, 43, 10}.
QuickSort is a sorting algorithm based on the Divide and Conquer algorithm that
picks an element as a pivot and partitions the given array around the picked pivot
by placing the pivot in its correct position in the sorted array.
Partition is done recursively on each side of the pivot after the pivot is placed in its
correct position and this finally sorts the array.
Choice of Pivot:
Partition Algorithm:
The logic is simple, we start from the leftmost element and keep track of the index
of smaller (or equal) elements as i. While traversing, if we find a smaller element,
we swap the current element with arr[i]. Otherwise, we ignore the current element.
Let us understand the working of partition and the Quick Sort algorithm with the
help of the following example:
Illustration of Quicksort:
As the partition process is done recursively, it keeps on putting the pivot in its
actual position in the sorted array. Repeatedly putting pivots in their actual position
makes the array sorted.
Follow the below images to understand how the recursive implementation of the
partition algorithm helps to sort the array.
● There are sorting algorithms that use special information about the keys and
operations other than comparison to determine the sorted order of elements.
● A sorting algorithm is In-place if the algorithm does not use extra space for
manipulating the input but may require a small though nonconstant extra space for
its operation. Or we can say, a sorting algorithm sorts in-place if only a constant
number of elements of the input array are ever stored outside the array.
Online/Offline: The algorithm that accepts a new element while the sorting process is
going on, that algorithm is called the online sorting algorithm. From, the above sorting
algorithms, the insertion sort is online.
Critical ideas to think
● Understanding the sorting algorithms are the best way to learn problem
solving and complexity analysis in the algorithms. In some cases, We often use
sorting as a key routine to solve several coding problems.
● If stability is important and space is available, the merge sort might be the
best choice for the implementation.
● Like insertion sort, quick sort has tight code and hidden constant factor in its
running time is small.
● When the input array is almost sorted or input size is small, then the insertion
sort can be preferred.
1. Time Complexity
2. Space Complexity
Time Complexity:
Space Complexity is the total memory space required by the program for its
execution.
Both are calculated as the function of input size(n). One important thing here is that
despite these parameters, the efficiency of an algorithm also depends upon the
nature and size of the input.
1. Best Time Complexity: Define the input for which the algorithm
takes less time or minimum time. In the best case calculate the lower
bound of an algorithm. Example: In the linear search when search data is
present at the first location of large data then the best case occurs.
2. Average Time Complexity: In the average case take all random
inputs and calculate the computation time for all inputs.
And then we divide it by the total number of inputs.
3. Worst Time Complexity: Define the input for which algorithm takes
a long time or maximum time. In the worst calculate the upper bound of
an algorithm. Example: In the linear search when search data is present
at the last location of large data then the worst case occurs.
What is Hashing in C
In C programming language, hashing is a technique that involves
converting a large amount of data into a fixed-size value or a smaller
value known as a hash. The hash is generated through a hash function,
which maps the input data to an output hash. The resulting hash value
can then be used to efficiently search, retrieve, and compare data
within large data sets.
Hashing is useful for several reasons. Firstly, it can reduce the amount
of memory required to store large data sets by converting the data into
a smaller value. Secondly, it can improve the performance of
algorithms by allowing for faster searching and retrieval of data.
Finally, it can help to ensure data integrity by detecting duplicate data
and preventing collisions (when two different keys map to the same
index).
The process of hashing involves three main steps: creating the hash
function, generating the hash value, and storing the data in the hash
table.
Once the hash function is created, the next step is to generate the
hash value for the data. This involves passing the data through the
hash function, which returns a fixed-size hash value. This value is then
used as an index within the hash table to store the data.
Storing the data in the hash table involves placing the data in the
corresponding location within the array. If a collision occurs (i.e. if two
different keys map to the same index), the hash table may use a
technique called chaining to store both keys in the same index. In
chaining, a linked list is created for each index, and the keys are added
to the linked list.
The pair is of the form (key, value), where for a given key, one can find a value
using some kind of a “function” that maps keys to values. The key for a given
object can be calculated using a function called a hash function. For example,
given an array A, if i is the key, then we can find the value by simply looking up
A[i].
There are many hash functions that use numeric or alphanumeric keys. This
article focuses on discussing different hash functions:
1. Division Method.
2. Mid Square Method.
3. Folding Method.
4. Multiplication Method.
1. Division Method:
This is the most simple and easiest method to generate a hash value. The hash
function divides the value k by M and then uses the remainder obtained.
Here,
k is the key value, and
M is the size of the hash table.
It is best suited that M is a prime number as that can make sure the keys are
more uniformly distributed. The hash function is dependent upon the remainder
of a division.
Example:
k = 12345
M = 95
h(12345) = 12345 mod 95
= 90
k = 1276
M = 11
h(1276) = 1276 mod 11
=0
Pros:
Cons:
The mid-square method is a very good hashing method. It involves two steps to
compute the hash value-
Formula:
h(K) = h(k x k)
Here,
k is the key value.
Example:
Suppose the hash table has 100 memory locations. So r = 2 because two digits
are required to map the key to the memory location.
k = 60
k x k = 60 x 60
= 3600
h(60) = 60
Pros:
1. The performance of this method is good as most or all digits of the key
value contribute to the result. This is because all digits in the key
contribute to generating the middle digits of the squared result.
2. The result is not dominated by the distribution of the top digit or bottom
digit of the original key value.
Cons:
1. The size of the key is one of the limitations of this method, as the key is
of big size then its square will double the number of digits.
2. Another disadvantage is that there will be collisions but we can try to
reduce collisions.
1. Divide the key-value k into a number of parts i.e. k1, k2, k3,….,kn, where
each part has the same number of digits except for the last part that
can have lesser digits than the other parts.
2. Add the individual parts. The hash value is obtained by ignoring the last
carry if any.
Formula:
Here,
s is obtained by adding the parts of the key k
Example:
k = 12345
k1 = 12, k2 = 34, k3 = 5
s = k1 + k2 + k3
= 12 + 34 + 5
= 51
h(K) = 51
Note:
The number of digits in each part varies depending upon the size of the hash
table. Suppose for example the size of the hash table is 100, then each part must
have two digits except for the last part which can have a lesser number of digits.
4. Multiplication Method
Here,
M is the size of the hash table.
k is the key value.
A is a constant value.
Example:
k = 12345
A = 0.357840
M = 100
Pros: The advantage of the multiplication method is that it can work with any
value between 0 and 1, although there are some values that tend to give better
results than the rest.
Cons: The multiplication method is generally suitable when the table size is the
power of two, then the whole process of computing the index by the key using
multiplication hashing is very fast.
Hash functions are widely used in computer science and cryptography for a
variety of purposes, including data integrity, digital signatures, password storage,
and more.
There are many types of hash functions, each with its own strengths and
weaknesses. Here are a few of the most common types:
1. SHA (Secure Hash Algorithm): SHA is a family of cryptographic hash
functions designed by the National Security Agency (NSA) in the United States.
The most widely used SHA algorithms are SHA-1, SHA-2, and SHA-3. Here’s a
brief overview of each:
● SHA-1: SHA-1 is a 160-bit hash function that was widely used for digital
signatures and other applications. However, it is no longer considered
secure due to known vulnerabilities.
● SHA-2: SHA-2 is a family of hash functions that includes SHA-224, SHA-
256, SHA-384, and SHA-512. These functions produce hash values of
224, 256, 384, and 512 bits, respectively. SHA-2 is widely used in
security protocols such as SSL/TLS and is considered secure.
● SHA-3: SHA-3 is the latest member of the SHA family and was selected
as the winner of the NIST hash function competition in 2012. It is
designed to be faster and more secure than SHA-2 and produces hash
values of 224, 256, 384, and 512 bits.
When the message is received, the receiver can recalculate the checksum using
the same algorithm, and compare it with the checksum transmitted with the
message. If the two checksums match, the receiver can be reasonably certain
that the message was not corrupted during transmission.
The specific algorithm used for CRC depends on the application and the desired
level of error detection. Some common CRC algorithms include CRC-16, CRC-32,
and CRC-CCITT.
Argon2 has several key features that make it a strong choice for password
hashing and key derivation:
Output
10 inserted at array[3]
4 inserted at array[4]
2 inserted at array[2]
Collision : array[3] has element 10 already!
Unable to insert 3
Hash table
array[0] = -1
array[1] = -1
array[2] = 2
array[3] = 10
array[4] = 4
array[5] = -1
array[6] = -1
So now we are looking for a data structure that can store the data and
search in it in constant time, i.e. in O(1) time. This is how Hashing data
structure came into play. With the introduction of the Hash data
structure, it is now possible to easily store data in constant time and
retrieve them in constant time as well.
Components of Hashing
The hashing process generates a small number for a big key, so there
is a possibility that two keys could produce the same value. The
situation where the newly inserted key maps to an already occupied,
and it must be handled using some collision handling technology.
Collision in Hashing
a) Linear Search
b) Binary Search
c) Interpolation Search
d) Jump Search
b) O(log n)
c) O(n)
d) O(n^2)
a) Bubble Sort
b) Selection Sort
c) Quick Sort
d) Insertion Sort
a) First element
b) Last element
c) Random element
a) Insertion Sort
b) Quick Sort
c) Merge Sort
d) Heap Sort
a) Quick Sort
b) Merge Sort
c) Selection Sort
d) Heap Sort
7. Which sorting algorithm has the best time complexity in the worst-
case scenario?
a) Quick Sort
b) Merge Sort
c) Bubble Sort
d) Insertion Sort
8. The time complexity of the worst-case scenario for bubble sort is:
a) O(n)
b) O(n log n)
c) O(n^2)
d) O(log n)
a) Bubble Sort
b) Radix Sort
c) Quick Sort
d) Merge Sort
a) Bubble Sort
b) Selection Sort
c) Merge Sort
d) Insertion Sort
11. What is the primary advantage of the merge sort algorithm over
the quick sort algorithm?
a) Linear Search
b) Binary Search
c) Interpolation Search
d) Jump Search
13. Which sorting algorithm has the best time complexity for sorting
linked lists?
a) Quick Sort
b) Merge Sort
c) Bubble Sort
d) Insertion Sort
14. In the selection sort algorithm, how many times do we select the
minimum element in each iteration?
a) Once
b) Twice
c) Thrice
d) It varies
15. Which of the following sorting algorithms is not suitable for large
datasets due to its poor performance?
a) Merge Sort
b) Quick Sort
c) Bubble Sort
d) Insertion Sort
b) Unstable sorting
a) Quick Sort
b) Merge Sort
c) Bubble Sort
d) Insertion Sort
18. What is the main advantage of using the insertion sort algorithm
for small datasets?
a) It is stable.
c) It is easy to implement.
a) Merge Sort
b) Quick Sort
c) Selection Sort
d) Insertion Sort
a) Linear Search
b) Binary Search
c) Interpolation Search
d) Jump Search
b) O(log n)
c) O(n)
d) O(n^2)
a) Binary Search
b) Quick Sort
c) Linear Search
23. Which sorting algorithm has the best time complexity in the
average case?
a) Bubble Sort
b) Quick Sort
c) Merge Sort
d) Selection Sort
24. In which sorting algorithm does the largest element "bubble up" to
its correct position in each pass?
a) Bubble Sort
b) Insertion Sort
c) Selection Sort
d) Quick Sort
25. Which of the following data structures is not suitable for binary
search?
a) Array
b) Linked List
d) Heap
26. What is the worst-case time complexity of the merge sort
algorithm?
a) O(n)
b) O(n log n)
c) O(n^2)
d) O(log n)
a) Bubble Sort
b) Quick Sort
c) Merge Sort
d) Selection Sort
a) It is stable.
c) It is easy to implement.
a) Quick Sort
b) Merge Sort
c) Bubble Sort
d) Insertion Sort
a) Linear Search
b) Binary Search
c) Interpolation Search
d) Jump Search
a) O(n)
b) O(log n)
c) O(n^2)
d) O(n log n)
33. Which of the following is a characteristic of a binary search tree?
a) Bubble Sort
b) Selection Sort
c) Merge Sort
d) Insertion Sort
a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)
a) First element
b) Last element
c) Random element
d) Middle element
37. Which sorting algorithm has the best time complexity for sorting
linked lists?
a) Quick Sort
b) Merge Sort
c) Bubble Sort
d) Insertion Sort
38. What is the main advantage of using the merge sort algorithm over
other sorting algorithms?
c) It is stable.
d) It is easy to implement.
b) Unstable sorting
b) Quick Sort
c) Radix Sort
d) Selection Sort
41. What is the primary advantage of using the binary search tree over
other data structures for searching?
a) Linear Search
b) Binary Search
c) Interpolation Search
d) Jump Search
a) Quick Sort
b) Merge Sort
c) Bubble Sort
d) Insertion Sort
a) It is not stable.
a) Linear Search
b) Binary Search
c) Interpolation Search
d) Jump Search
a) Merge Sort
b) Quick Sort
c) Selection Sort
d) Insertion Sort
47. What is the primary advantage of using the heap sort algorithm?
a) It has a better average-case time complexity.
48. Which searching algorithm works by dividing the array into blocks
of fixed size and then searching for the desired element in the
appropriate block?
a) Linear Search
b) Binary Search
c) Interpolation Search
d) Jump Search
a) Bubble Sort
b) Radix Sort
c) Quick Sort
d) Merge Sort
50. Which sorting algorithm is known for its best-case time complexity
of O(n)?
a) Bubble Sort
b) Selection Sort
c) Quick Sort
d) Insertion Sort
51. What is the primary advantage of using the radix sort algorithm?
c) It is stable.
53. What is the time complexity of linear search when the element is
not present in the array?
a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)
54. Which sorting algorithm is known for its stability and is often used
as a subroutine in other sorting algorithms?
a) Quick Sort
b) Merge Sort
c) Bubble Sort
d) Selection Sort
b) It is stable.
a) Quick Sort
b) Merge Sort
c) Bubble Sort
d) Selection Sort
58. What is the main disadvantage of using the bubble sort algorithm?
a) It is not stable.
c) It is stable.
60. Which sorting algorithm is known for its simplicity and is often used
for educational purposes rather than practical applications?
a) Merge Sort
b) Quick Sort
c) Bubble Sort
d) Insertion Sort
61. What is the primary advantage of using the heap sort algorithm?
62. Which searching algorithm works by dividing the array into blocks
of fixed size and then searching for the desired element in the
appropriate block?
a) Linear Search
b) Binary Search
c) Interpolation Search
d) Jump Search
a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)
65. Which sorting algorithm is known for its stability and is often used
as a subroutine in other sorting algorithms?
a) Quick Sort
b) Merge Sort
c) Bubble Sort
d) Selection Sort
68. Which sorting algorithm works by repeatedly dividing the array into
two halves and then merging them in sorted order?
a) Quick Sort
b) Merge Sort
c) Bubble Sort
d) Selection Sort
69. What is the main disadvantage of using the bubble sort algorithm?
a) It is not stable.
c) It is stable.
*Theory questions:*
1. Explain the working principle of the binary search algorithm.
3. Compare and contrast the time complexity of bubble sort and merge
sort.
4. Explain how the selection sort algorithm works and analyze its time
complexity.
10. How does the performance of binary search differ from linear
search, especially on large datasets?
15. Explain the concept of hashing and its applications in data storage
and retrieval.
16. Compare and contrast the time complexity of bubble sort and
merge sort.
17. Explain how the selection sort algorithm works and analyze its time
complexity.
18. Discuss the advantages and disadvantages of using quick sort over
merge sort.
21. Explain the concept of in-place sorting and provide examples of in-
place and non-in-place sorting algorithms.
22. Discuss the applications of sorting algorithms in real-world
scenarios.