Unit-I: Searching and
Sorting
Searching (Def≈ &
Need):
• Searching is the process of finding a given value position in a list of
values.
• It decides whether a search key is present in the data or not.
• It is the algorithmic process of finding a particular item in a collection
of items.
• It can be done on internal data structure or on external data structure.
Searching
Techniques:
1. Linear search or Sequential Search:
Start at the beginning of the list and check every element of the list.
Very slow [order O(n) ] but works on an unsorted list.
2. Binary Search or interval Search:
This is used for searching in a sorted array. Test the middle element
of the array. If it is too big. Repeat the process in the left half of the
array, and the right half if it’s too small. In this way, the amount of
space that needs to be searched is halved every time, so the time is
O(log n).
Sequential or Linear
Search:
• Sequential search starts at the beginning of the list and checks every
element of the list. It is a basic and simple search algorithm.
• Sequential search compares the element with all the other elements
given in the list. If the element is matched, it returns the value
index, else it returns -1.
Linear Search
Algorithm:
// search x in arr[]. If x is present then return its location, otherwise return -1
int search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
Complexity of Linear
(Sequential)
• Space complexity: Search:
As linear search algorithm does not use any extra space thus its space
complexity = O(n) for an array of n number of elements.
• Time Complexity:
• Worst case complexity: O(n) – This case occurs when the element to search is
not present in the array.
• Best case complexity: O(1) – This case occurs when the first element is the
element to be searched.
• Average complexity: O(n) – This means when an element is present
somewhere in the middle of the array.
Binary Search:
• Binary Search is used for searching an element in a sorted array.
• It is a fast search algorithm with run-time complexity of O(log n).
• Binary search works on the principle of divide and conquer.
• This searching technique looks for a particular element by comparing
the middle most element of the collection.
• It is useful when there are large number of elements in an array.
Continued.....
Summarise Binary Search:
• Basically ignore half of the elements just after one comparison.
• Compare x with the middle element.
• If x matches with middle element, we return the mid index.
• Else
If x is greater than the mid element, then x can only lie in right
half subarray after the mid element. So we recur for right half.
Else (x is smaller) recur for the left half.
Binary Search Algorithm: (non-
recursive
Algorithm version)
Binary_Search(list A[0..n-1], item)
// item is T searching element
1) Set L to 0 and R to n
2) if L > R, then Binary_Search terminates as unsuccessful
else
3) Set m (the position in the mid element) to the floor of
(L + R) / 2
4) if A[m] < T, set L to m + 1 and go to step 3
5) if A[m] > T, set R to m: 1 and go to step 3
6) Now, A[m] = T,
7) search is successful; return (m)
Binary Search Algorithm: (recursive
version)
// A recursive binary search function. It returns if (arr[mid] > x)
location of x in given array arr[l..r] is present, return binarySearch(arr, l, mid - 1, x);
otherwise -1
int binarySearch(int arr[], int l, int r, int x)
// Else the element can only be present in
{ right subarray
if (r >= l) { return binarySearch(arr, mid + 1, r, x);
int mid = l + (r - l) / 2; }
// If the element is present at the middle itself
if (arr[mid] == x) // We reach here when element is not present
in array
return mid;
return -1;
// If element is smaller than mid, then it can }
only be present in left subarray
Complexity of Binary Search:
• Space complexity:
As binary search algorithm does not use any extra space thus its
space complexity = O(n) for an array of n number of elements.
• Time Complexity:
• Worst Case: O(nlogn)
• Best Case: O(1)
• Average Case: O(nlogn)
Sorting:
• Sorting algorithm is an algorithm that puts elements of a list in a
certain order. The most frequently used orders are numerical
order and lexicographical order.
• A Sorting Algorithm is used to rearrange a given array or list elements
according to a comparison operator on the elements. The comparison
operator is used to decide the new order of element in the
respective data structure.
Internal and External
sorting:
• Internal sorting are type of sorting which is used when the entire
collection of data is small enough that sorting can take place within
main memory. There is no need for external memory for execution of
sorting program. It is used when size of input is small.
Examples:- Bubble sort, insertion sort,quicksort, heapsort
• External sorting is a term for a class of sorting algorithms that can
handle massive amounts of data. External sorting is required when
the data being sorted do not fit into the main memory of a computing
device (usually RAM) and instead, they must reside in the
slower external memory (usually a hard drive).
Examples:- Merge Sort
Stable Sorting:
• If a sorting algorithm, after sorting the contents, does not change the
sequence of similar content in which they appear, it is called stable
sorting.
Unstable Sorting:
• If a sorting algorithm, after sorting the contents, changes the
sequence of similar content in which they appear, it is called unstable
sorting.
Sorting Techniques
(Methods):
• Bubble sort
• Insertion Sort
• Quick Sort
• Merge Sort
• Shell Sort
Case Study
• Selection Sort
• Bucket Sort
• Radix Sort
Bubble
Sort:
• Bubble sort compares the value of first element with the immediate
next element and swaps according to the requirement and goes till
the last element. This iteration repeates for (N - 1) times/steps where
N is the number of elements in the list.
• Bubble sort of N elements can take (N - 1) steps and (N -1) iterations
in each steps. Thus resultant is (N - 1)*(N - 1). This sorting algorithm is
not however the best in performance when count of the elements
are large. Time complexities of bubble sort is O(N^2) [Square of
N]. This sorting is well suited for small number of elements
Bubble Sort Example: 4
9 510
Example Continued.....
void BubbleSort (int a[], int length)
{ int i, j, temp;
for (i = 0; i < length; i++)
{ for (j = 0; j < (length - i -
1); j++)
{ if (a[j + 1] < a[j])
{ temp =
a[j];
a[j] = a[j + 1];
} a[j + 1] = temp;
} Dashed line indicates alignment of
} curly brackets while writing programs.
}
Complexity of
Bubble
• Sort:
Worst and Average Case Time Complexity: O(n*n).
Worst case occurs when array is reverse sorted.
• Best Case Time Complexity: O(n).
Best case occurs when array is already sorted.
• Auxiliary Space: O(1)
• Boundary Cases: Bubble sort takes minimum time (Order of n) when
elements are already sorted.
• Sorting In Place: Yes
• Stable: Yes
Example (Animation):
Insertion
Sort:
• Insertion sort is a simple sorting algorithm that works similar to the
way you sort playing cards in your hands. The array is virtually split
into a sorted and an unsorted part. Values from the unsorted part are
picked and placed at the correct position in the sorted part.
Example (Animation):
Another
Example:
• Consider the following array:
25, 17, 31, 13, 2
• First Iteration:
Compare 25 with 17.
The comparison
shows 17< 25.
Hence swap 17 and
25.
• The array now looks
like:
• Second Iteration: Begin with the
second element (25), but it
was already swapped on for the
correct position, so we move
ahead to the next element.
• Now hold on to the third element
(31) and compare with the ones
preceding it.
The array after the Second
iteration looks like:
• Since 31> 25, no swapping takes
place. Also, 31> 17, no
swapping takes place and 31 17, 25, 31, 13, 2
remains at its position.
• Third Iteration: Start the following Iteration
with the fourth element (13), and
compare it with its preceding elements.
• Since 13< 31, we swap the two. Array now
becomes: 17, 25, 13, 31, 2.
• But there still exist elements that
we haven’t yet compared with 13.
Now the comparison takes place
between 25 and 13. Since, 13 < 25, we
swap the two. The array
becomes 17, 13, 25, 31, 2.
• The last comparison for the iteration is
now between 17 and 13. Since 13 < 17,
we swap the two. The array now
becomes
• Fourth Iteration: The last iteration calls for
the comparison of the last element (2), with
all the preceding elements and make the
appropriate swapping between elements.
• Since, 2< 31. Swap 2 and 31. Array now
becomes: 13, 17, 25, 2, 31.
• Compare 2 with 25, 17, 13. Since, 2< 25.
Swap 25 and 2. Array is: 13, 17, 2, 25, 31.
• Compare 2 with 17 and 13. Since, 2<17.
Swap 2 and 17. Array now becomes:
• 13, 2, 17, 25, 31.
• The last comparison for the Iteration is to
compare 2 with 13. Since 2< 13. Swap 2 and
13.The array now becomes:2, 13, 17, 25,
31.
One more
• 12, 11, 13, 5, 6
Example:
• Let us loop for i = 1 (second element of the array) to 4 (last element of
the array)
• i = 1. Since 11 is smaller than 12, move 12 and insert 11 before 12
→ 11, 12, 13, 5, 6
• i = 2. 13 will remain at its position as all elements in A[0..I-1] are
smaller than 13 → 11, 12, 13, 5, 6
• i = 3. 5 will move to the beginning and all other elements from 11 to
13 will move one position ahead of their current position.
→ 5, 11, 12, 13, 6
• i = 4. 6 will move to position after 5, and elements from 11 to 13 will
move one position ahead of their current position. → 5, 6, 11, 12,
13
Algorithm Insertion Sort:
• Step 1 − If it is the first element, it is already sorted. return 1;
• Step 2 − Pick next element
• Step 3 − Compare with all elements in the sorted sub-list
• Step 4 − Shift all the elements in the sorted sub-list that is greater
than the value to be sorted
• Step 5 − Insert the value
• Step 6 − Repeat until list is sorted
Complexity of
Insertion
• Time Complexity:Sort:
O(n^2) (Worst case)
• Time Complexity: O(n) (Best Case)
• Auxiliary Space: O(1)
• Boundary Cases: Insertion sort takes maximum time to sort if
elements are sorted in reverse order. And it takes minimum
time (Order of n) when elements are already sorted.
• Algorithmic Paradigm: Incremental Approach
• Sorting In Place: Yes Stable: Yes
Selection
Sort:
• Selection sort is a simple sorting algorithm. This sorting algorithm 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 portion and
swapped with the leftmost element, and that element becomes a
part of the sorted array. This process continues moving unsorted array
boundary by one element to the right.
• This algorithm is not suitable for large data sets as its average and
worst case complexities are of Ο(n2), where n is the number of
items.
• Consider the following depicted array as an 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.
• 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.
Animation of Selection Sort
Algorith
m:
Begin
Step 1 − Set MIN to location 0
Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted
End.
Procedur
procedure selection sort ( list : array of items; n : size of list)
e:
for i = 1 to n - 1 /* set current element as minimum*/
min = i /* check the element to be minimum */
for j = i+1 to n
if (list[j] < list[min]) then min = j;
end if end for
/* swap the minimum element with the
current
if indexMin element*/
!= i swap (list[min] , list[i] ) end if
then end for
end procedure
C++ Code
void selectionSort(int arr[], int n)
Segment:
{ int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{ min_idx = i; // Find the minimum element in unsorted
array for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}
Complexi
•ty:
Time Complexity: O(n ) as there are two nested loops.
2
• Auxiliary Space: O(1)
• The good thing about selection sort is it never makes more than O(n)
swaps and can be useful when memory write is a costly operation.
• In Place : Yes, it does not require extra space.
• Stability : Unstable.
- For example, suppose you sorted 4 2 3 4 1 with selection sort.
The first "round" will go through each element looking for the minimum element. it
will find that 1 is the minimum element. then it will swap the 1 into the first spot.
this will cause the 4 in the first spot to go into the last spot: 1 2 3 4 4
Now the relative order of the 4's has changed. the "first" 4 in the original list has
been moved to a spot after the other 4. (Observe Red and Green colored “4”).