Searching Sorting Techniques
Searching Sorting Techniques
Technology
1
What is Searching Algorithm?
Searching Algorithms are designed to check for an element or retrieve an
element from any data structure where it is stored.
Based on the type of search operation, these algorithms are generally
classified into two categories:
Sequential Search: In this, the list or array is traversed sequentially and
every element is checked. For example: Linear Search.
Linear Search to find the element “20” in a given list of numbers
How Does Linear Search Algorithm Work?
In Linear Search Algorithm,
Every element is considered as a potential match for the key and checked
for the same.
If any element is found equal to the key, the search is successful and the
index of that element is returned.
If no element is found equal to the key, the search yields “No match
found”.
Complexity Analysis of Linear Search:
Time Complexity:
Best Case: In the best case, the key might be present at the first index. So
the best case complexity is O(1)
Worst Case: In the worst case, the key might be present at the last index
i.e., opposite to the end from which the search has started in the list. So
the worst-case complexity is O(N) where N is the size of the list.
Average Case: O(N)
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.
Drawbacks of Linear Search:
Linear search has a time complexity of O(N), which in turn makes it
slow for large datasets.
Not suitable for large arrays.
When to use Linear Search?
When we are dealing with a small dataset.
When you are searching for a dataset stored in contiguous
memory.
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
Binary Search is defined as a searching algorithm used in a sorted array
by repeatedly dividing the search interval in half. The idea of binary search is to
use the information that the array is sorted and reduce the time complexity to
O(log N).
Example of Binary Search Algorithm
In this algorithm,
Divide the search space into two halves by finding the middle index
“mid”.
Finding the middle index “mid” in Binary Search Algorithm
Compare the middle element of the search space with the key.
If the key is found at middle element, the process is terminated.
If the key is not found at middle element, choose which half will be
used as the next search space.
If the key is smaller than the middle element, then the left side is
used for next search.
If the key is larger than the middle element, then the right side is
used for next search.
This process is continued until the key is found or the total search
space is exhausted.
Time Complexity of binary search:
Best Case: O(1)
Average Case: O(log N)
Worst Case: O(log N)
Auxiliary Space: O(1), If the recursive call stack is considered then the
auxiliary space will be O(logN).
Advantages of Binary Search:
Binary search is faster than linear search, especially for large arrays.
More efficient than other searching algorithms with a similar time
complexity, such as interpolation search or exponential search.
Binary search is well-suited for searching large datasets that are stored in
external memory, such as on a hard drive or in the cloud.
Drawbacks of Binary Search:
The array should be sorted.
Binary search requires that the data structure being searched be stored in
contiguous memory locations.
Binary search requires that the elements of the array be comparable,
meaning that they must be able to be ordered
What is Recursive Binary Search
Define recursive binary search
Recursive algorithms are used in binary search. The broad strategy is to look at the middle item on
the list. The procedure is either terminated (key found), the left half of the list is searched
recursively, or the right half of the list is searched recursively, depending on the value of the middle
element.
Example: Input: arr[] = {1, 4, 3, 5, 6, 8, 11, 10, 14, 17}
Target value = 8
OUTPUT: Element 8 is present at index 6.
Algorithm
Find the element at arr[size/2], which will be the array's midpoint. The array is split halfway, with
the lower half consisting of items 0 to midpoint -1 and the top half consisting of elements midpoint
to size -1.
Otherwise return NULL, indicating that there is no match if the array has only one element.
Search the lower half of the array by repeatedly executing search if the key is less than the value
taken from arr[midpoint].
// Driver code
int main()
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}
Amity School of Engineering &
Technology
SORTING
TECHNIQUES
1
Amity School of Engineering &
Technology
1
Amity School of Engineering &
Technology
Sorting:
• Sorting means arranging the elements of an array so that they are placed
in some relevant order which may be either ascending or descending.
1
Amity School of Engineering &
Technology
1. Bubble Sort:
• Bubble sort is a very simple method that sorts the array elements by repeatedly moving the
largest element to the highest index position of the array segment (in case of
arranging elements in ascending order).
• In bubble sorting, consecutive adjacent pairs of elements in the array are compared with
each other.
• If the element at the lower index is greater than the element at the higher index, the two
elements are interchanged so that the element is placed before the bigger one.
• This process will continue till the list of unsorted elements exhausts.
• This procedure of sorting is called bubble sorting because elements ‘bubble’ to the top of
the list. Note that at the end of the first pass, the largest element in the list will be placed at
its proper position (i.e., at the end of the list).
• If the elements are to be sorted in descending order, then in first pass the smallest element
is moved to the highest index of the array.
1
Amity School of Engineering &
Technology
•In Pass 1, A[0] and A[1] are compared, then A[1] is compared with A[2], A[2] is compared with
A[3], and so on. Finally, A[N–2] is compared with A[N–1]. Pass 1 involves n–1
comparisons and places the biggest element at the highest index of the array.
•In Pass 2, A[0] and A[1] are compared, then A[1] is compared with A[2], A[2] is compared with
A[3], and so on. Finally, A[N–3] is compared with A[N–2]. Pass 2 involves n–2
comparisons and places the second biggest element at the second highest index of the array.
•In Pass 3, A[0] and A[1] are compared, then A[1] is compared with A[2], A[2] is compared with
A[3], and so on. Finally, A[N–4] is compared with A[N–3]. Pass 3 involves n–3
comparisons and places the third biggest element at the third highest index of the array.
•In Pass n–1, A[0] and A[1] are compared so that A[0]<A[1]. After this step, all the elements of
the array are arranged in ascending order.
1
Amity School of Engineering &
Technology
1
Amity School of Engineering &
Technology
• Observe that after the end of the first pass, the largest element is placed at the highest
index of the array. All the other elements are still unsorted.
Pass 2:
• Compare 30 and 29. Since 30 > 29, swapping is done.
29, 30, 52, 63, 27, 19, 54, 87
• Compare 30 and 52. Since 30 < 52, no swapping is done.
• Compare 52 and 63. Since 52 < 63, no swapping is done.
• Compare 63 and 27. Since 63 > 27, swapping is done.
29, 30, 52, 27, 63, 19, 54, 87
• Compare 63 and 19. Since 63 > 19, swapping is done. 29, 30, 52, 27, 19, 63, 54, 87
• Compare 63 and 54. Since 63 > 54, swapping is done. 29, 30, 52, 27, 19, 54, 63, 87
• Observe that after the end of the second pass, the second largest element is placed at the
second highest index of the array. All the other elements are still unsorted.
1
Amity School of Engineering &
Technology
Pass 3:
(a)Compare 29 and 30. Since 29 < 30, no swapping is done.
(b)Compare 30 and 52. Since 30 < 52, no swapping is done.
(c)Compare 52 and 27. Since 52 > 27, swapping is done. 29, 30, 27, 52, 19, 54, 63, 87
(d)Compare 52 and 19. Since 52 > 19, swapping is done.
29, 30, 27, 19, 52, 54, 63, 87
(a)Compare 52 and 54. Since 52 < 54, no swapping is done.
(a)Observe that after the end of the third pass, the third largest element is placed at
the third highest index of the array. All the other elements are still unsorted.
10
Amity School of Engineering &
Technology
Pass 4:
(a)Compare 29 and 30. Since 29 < 30, no swapping is done.
•Compare 30 and 27. Since 30 > 27, swapping is done. 29, 27, 30, 19, 52, 54, 63, 87
•Compare 30 and 19. Since 30 > 19, swapping is done. 29, 27, 19, 30, 52, 54, 63, 87
(a)Compare 30 and 52. Since 30 < 52, no swapping is done.
•Observe that after the end of the fourth pass, the fourth largest element is placed at the fourth
highest index of the array. All the other elements are still unsorted.
Pass 5:
(a)Compare 29 and 27. Since 29 > 27, swapping is done.
27, 29, 19, 30, 52, 54, 63, 87
(a)Compare 29 and 19. Since 29 > 19, swapping is done. 27, 19, 29, 30, 52, 54, 63, 87
(b)Compare 29 and 30. Since 29 < 30, no swapping is done.
(c)Observe that after the end of the fifth pass, the fifth largest element is placed at the fifth highest
index of the array. All the other elements are still unsorted.
20
Amity School of Engineering &
Technology
Pass 6:
(a)Compare 27 and 19. Since 27 > 19, swapping is done.
19, 27, 29, 30, 52, 54, 63, 87
(a)Compare 27 and 29. Since 27 < 29, no swapping is done.
(a)Observe that after the end of the sixth pass, the sixth largest element is placed at the
sixth largest index of the array. All the other elements are still unsorted.
Pass 7:
(a) Compare 19 and 27. Since 19 < 27, no swapping is done.
21
Amity School of Engineering &
Technology
• However, the frequency of the inner loop will decrease with every pass because after every
pass, one element will be in its correct position.
• Therefore, for every pass, the inner loop will be executed N–I times, where N is the number
of elements in the array and I is the count of the pass.
22
Amity School of Engineering &
Technology
Bubble sort Implementation:
23
Amity School of Engineering &
Technology
• In bubble sort, we have seen that there are N–1 passes in total. In the first pass, N–1
comparisons are made to place the highest element in its correct position.
• Then, in Pass 2, there are N–2 comparisons and the second highest element is placed
in its position.
• Therefore, to compute the complexity of bubble sort, we need to calculate the total
number of comparisons. It can be given as:
Therefore, the complexity of bubble sort algorithm is O(𝑛2 ). It means the time
required to execute bubble sort is proportional to 𝑛2, where n is the total number of
•
24
elements in the array.
Amity School of Engineering &
Technology
2. Insertion Sort:
• Insertion sort is a very simple sorting algorithm in which the sorted array (or list)
is built one element at a time.
• We all are familiar with this technique of sorting, as we usually use it for ordering
a deck of cards while playing bridge.
• The main idea behind insertion sort is that it inserts each item into its proper
place in the final list.
25
Amity School of Engineering &
Technology
•The array of values to be sorted is divided into two sets. One that stores sorted
values and another that contains unsorted values.
•The sorting algorithm will proceed until there are elements in the unsorted set.
•Suppose there are n elements in the array. Initially, the element with index
0 (assuming LB = 0) is in the sorted set. Rest of the elements are in the unsorted set.
1)The first element of the unsorted partition has array index 1 (if LB = 0).
•During each iteration of the algorithm, the first element in the unsorted set
is picked up and inserted into the correct position in the sorted set.
26
Amity School of Engineering &
Technology
27
Amity School of Engineering &
Technology
• In Pass 2, A[2] will be placed either before A[0], in between A[0] and A[1], or after
A[1].
• In Pass N–1, A[N–1] will be placed in its proper place to keep the array sorted.
28
Amity School of Engineering &
Technology
20
Amity School of Engineering &
Technology
Insertion Sort Implementation:
#include <iostream> using namespace std; int main()
{
int A[10], int n;
int i, j, temp;
cout<<“Enter the size of the array:”<<endl; cin>>n;
cout<<“Enter the elements of the array:”<<endl;
for (i = 0; i < n; i++) cout << a[i] <<" "; for (i = 1; i < n; i+
+)
{
temp = A[i]; j = i - 1;
while(j>=0 && temp <= a[j])
{ A[j+1] = A[j];
j = j-1;
}
A[j+1] = temp;
}
30 }
Amity School of Engineering &
Technology
• This is because, during each iteration, the first element from the unsorted set is
compared only with the last element of the sorted set of the array.
• Similarly, the worst case of the insertion sort algorithm occurs when the array is
sorted in the reverse order. In the worst case, the first element of the unsorted
set has to be compared with almost every element in the sorted set.
• Furthermore, every iteration of the inner loop will have to shift the elements of
the sorted set of the array before inserting the next element. Therefore, in the
worst case, insertion sort has a quadratic running time (i.e., O(𝑛2)).
• Even in the average case, the insertion sort algorithm will have to make at least
(K–1)/2 comparisons. Thus, the average case also has a quadratic running time.
31
Amity School of Engineering &
Technology
3. Selection Sort:
• This is perhaps the simplest method of sorting. In this method, to sort the data in
ascending order, the element is compared with all other elements.
• If the element is found to be greater than the compared element then they are
interchanged.
• So after the first iteration the smallest element gets placed at the position. The
same procedure is repeated for the element and so on. This procedure can be
best understood with the help of Figure.
32
Amity School of Engineering &
Technology
• In Pass 1, find the position POS of the smallest value in the array and then swap
ARR[POS] and ARR[0]. Thus, ARR[0] is sorted.
• In Pass 2, find the position POS of the smallest value in sub-array of N–1
elements. Swap ARR[POS] with ARR[1]. Now, ARR[0] and ARR[1] is sorted.
• In Pass N–1, find the position POS of the smaller of the elements ARR[N–2] and
ARR[N–1]. Swap ARR[POS] and ARR[N–2] so that ARR[0], ARR[1], ..., ARR[N–1] is
sorted.
33
Amity School of Engineering &
Technology
• 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.
64 25 12 22 11
• 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.
11 25 12 22 64
• Second Pass: For the second position, where 25 is present, again traverse the rest of the
array in a sequential manner.
11 25 12 22 64
• 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.
11 12 25 22 64
34
Amity School of Engineering &
Technology
• 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.
11 12 25 22 64
• 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.
11 12 22 25 64
• 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.
11 12 22 25 64
• 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.
11 12 22 25 64
35