Lab 04 - Sorting and Searching
Lab 04 - Sorting and Searching
Lab 04 - Sorting and Searching
Learning Objectives
1. Sorting
2. Bubble Sort.
3. Selection Sort.
4. Linear Search.
5. Binary Search.
6. Lab Tasks.
1. Sorting
Sorting is nothing but arranging the data in ascending or descending order. The term sorting came
into picture, as humans realized the importance of searching quickly. There are so many things in
our real life that we need to search for, like a particular record in database, roll numbers in merit
list, a particular telephone number in telephone directory, a particular page in a book etc. All this
would have been a mess if the data was kept unordered and unsorted, but fortunately the concept
of sorting came into existence, making it easier for everyone to arrange data in an order, hence
making it easier to search. Sorting arranges data in a sequence which makes searching easier. Since
the beginning of the programming age, computer scientists have been working on solving the
problem of sorting by coming up with various different algorithms to sort data. The two main
criteria to judge which algorithm is better than the other have been:
Time taken to sort the given data.
Memory Space required to do so.
There are many different techniques available for sorting, differentiated by their efficiency and
space requirements. We will be studying the following:
Bubble Sort
Selection Sort
2. Bubble Sort
Implementing the bubble sort Algorithm:
1. Starting with the first element (index = 0/node = 1), compare the current element with the
next element of the array.
2. If the current element is greater than the next element of the array, swap them.
3. If the current element is less than the next element, move to the next element. Repeat Step 1.
Data Structures and Algorithm
Lab 04: Sorting and Searching
= N(N-1)/2
3. Selection Sort
Implementing the Selection Sort Algorithm:
1. Starting from the first element, we search the smallest element in the array, and replace it
with the element in the first position.
2. We then move on to the second position, and look for smallest element present in the
subarray, starting from index 1, till the last index.
3. We replace the element at the second position in the original array, or we can say at the first
position in the subarray, with the second smallest element.
4. This is repeated, until the array is completely sorted.
Data Structures and Algorithm
Lab 04: Sorting and Searching
Therefore, given a size N of the input array, the selection sort algorithm has the following time and
complexity values.
The time complexity of O(n2) is mainly because of the use of two for loops. Note that the selection
sort technique never takes more than O(n) swaps and is beneficial when the memory write operation
proves to be costly.
4. Linear Search
Search an array or list by checking items one at a time. The linear search is also known as sequential
search.
Every element in the data set is examined in the order presented until the value being searched for
is found. If the value being searched for does not exist, a flag value is returned.
Data Structures and Algorithm
Lab 04: Sorting and Searching
5. Binary Search
Binary Search is 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). 5.
The basic steps to perform Binary Search are:
1. Sort the array in ascending order.
2. Set the low index to the first element of the array and the high index to the last element.
3. Set the middle index to the average of the low and high indices.
4. If the element at the middle index is the target element, return the middle index.
5. If the target element is less than the element at the middle index, set the high index to the
middle index – 1.
6. If the target element is greater than the element at the middle index, set the low index to the
middle index + 1.
7. Repeat steps 3-6 until the element is found or it is clear that the element is not present in the
array.
Data Structures and Algorithm
Lab 04: Sorting and Searching
In each iteration, the search space is getting divided by 2. That means that in the current iteration
you have to deal with half of the previous iteration array. And the above steps continue till
beg<end. Best case could be the case where the first mid-value get matched to the element to be
searched
Best Time Complexity: O(1)
Average Time Complexity: O(logn)
Worst Time Complexity: O(logn)
Data Structures and Algorithm
Lab 04: Sorting and Searching
6. Lab Tasks
1) We know that even if we give sorted array to our current bubble sort algorithm, it will
perform all the iterations. Change this code so that, if sorted data is given as input, algorithm
recognizes it and no further iterations are required.
2) Write recursive code for the linear search algorithm.
3) Write recursive code for the binary search algorithm.