CTSD2 UNIT-6 Searching and Sorting
CTSD2 UNIT-6 Searching and Sorting
Structured Design-II(303105151)
Mrs. Praptiba Solanki
Computer Science & Engineering Department (PIT)
CHAPTER-6
SEARCHING AND SORTING
Contents
• Selectionsort
• BubbleSort
• Insertion sort
• Linear Searching Techniques
• Binary Searching Techniques
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 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.
Selection Sort
• Selection Sort Algorithm | Swapping 1st element with the minimum in
array
Selection Sort
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.
• Selection Sort Algorithm | swapping i=1 with the next minimum element
Selection Sort
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.
• Selection Sort Algorithm | swapping i=2 with the next minimum element
Source:- Britannica
Selection Sort
Fifth Pass:
• At last the largest value present in the array automatically get placed at the
last position in the array
• The resulted array is the sorted array.
Source:- Britannica
Selection Sort
Advantages of Selection Sort Algorithm
• Simple and easy to understand.
• Works well with small datasets
Disadvantages of the Selection Sort Algorithm
• Selection sort has a time complexity of O(n^2) in the worst and average
case.
• Does not work well on large datasets.
• Does not preserve the relative order of items with equal keys which means
it is not stable.
Source:- Britannica
Bubble Sort
• Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in the wrong order.
• This algorithm is not suitable for large data sets as its average and worst-
case time complexity is quite high.
Bubble Sort
In Bubble Sort algorithm,
• traverse from left and compare adjacent elements and the higher one
is placed at right side.
• In this way, the largest element is moved to the rightmost end at first.
• This process is then continued to find the second largest and place it
and so on until the data is sorted.
Bubble Sort
• How does Bubble Sort Work?
• Let us understand the working of bubble sort with the help of the
following illustration: Input: arr[] = {6, 0, 3, 5}
First Pass:
The largest element is placed in its correct position, i.e., the end of the
array.
Bubble Sort Algorithm : Placing the largest element at correct position
Source:- medium
Bubble Sort
Source:- medium
Bubble Sort
Second Pass:
• Place the second largest element at correct position
• Bubble Sort Algorithm : Placing the second largest element at correct
position
Bubble Sort
Bubble Sort
Third Pass:
• Place the remaining two elements at their correct positions.
• Bubble Sort Algorithm : Placing the remaining elements at their correct Positions
Second Pass:
• Compare 1 with 23 (current element with the sorted part).
• Since 1 is smaller, insert 1 before 23.
• The sorted part until 1st index is: [1, 23]
Insertion Sort
Third Pass:
• Compare 10 with 1 and 23 (current element with the sorted part).
• Since 10 is greater than 1 and smaller than 23, insert 10 between 1 and
23.
• The sorted part until 2nd index is: [1, 10, 23]
Fourth Pass:
• Compare 5 with 1, 10, and 23 (current element with the sorted part).
• Since 5 is greater than 1 and smaller than 10, insert 5 between 1 and
10.
• The sorted part until 3rd index is: [1, 5, 10, 23]
Insertion Sort
Fifth Pass:
• Compare 2 with 1, 5, 10, and 23 (current element with the sorted part).
• Since 2 is smaller than all elements in the sorted part, insert 2 at the
• beginning.
• The sorted part until 4th index is: [2, 1, 5, 10, 23]
Final Array:
• The sorted array is: [2, 1, 5, 10, 23]
Insertion Sort
Advantages of Insertion Sort:
• Simple and easy to implement.
• Stable sorting algorithm.
• Efficient for small lists and nearly sorted lists.
• Space-efficient.
Step 1:
• 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.
Linear Search
Comparing key with next element arr[1]. SInce not equal, the iterator
moves to the next element as a potential match.
Linear Search
Step 2:
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 (here 2).
Linear Search
Advantages of Linear Search:
• Linear search can be used irrespective of whether the array is sorted or
not.
• It can be used on arrays of any data type.
• Does not require any additional memory.
• It is a well-suited algorithm for small datasets.
• Consider an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the
target = 23.
Binary Search
First Step:
• Calculate the mid and compare the mid element with the key. If the key
is less than mid element, move to left and if it is greater than the mid
then move search space to the right.
• Key (i.e., 23) is greater than current mid element (i.e., 16). The search
space moves to the right.
Binary Search
Key is less than the current mid 56. The search space moves to the left.
Binary Search
Second Step:
• If the key matches the value of the mid element, the element is found
and stop search.
Binary Search
How to Implement Binary Search?
The Binary Search Algorithm can be implemented in the following two ways
1. Iterative Binary Search Algorithm
2. Recursive Binary Search Algorithm