You are on page 1of 8

Data Structures and Algorithm

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

Pseudocode for Bubble Sort


Procedure bubble_sort(array,N)
//array – array of items to be sorted
//N – size of array
begin
for I = 1 to N-1
begin
for j = i+1 to N - i
begin
if array[j] > array[j + 1] then
swap (array[j], array[j + 1]);
end if
end for
end for
end procedure
Data Structures and Algorithm
Lab 04: Sorting and Searching

Complexity Analysis of the Bubble Sort Algorithm


From the pseudo code and the illustration that we have seen above, in bubble sort, we make N-1
comparisons in the first pass, N-2 comparisons in the second pass and so on.

Hence the total number of comparisons in bubble sort is:


Sum = (N-1) + (N-2) + (N-3) + … + 3 + 2 + 1

= N(N-1)/2

= O(n2) => Time complexity of bubble sort technique


Thus the various complexities for bubble sort technique are given below:
Worst case time complexity O(n2 )
Best case time complexity O(n)
Average time complexity O(n2 )
Space complexity O(1)
The bubble sort technique requires only a single additional memory space for the temp variable to
facilitate swapping. Hence the space complexity for bubble sort algorithm is O (1).
Note that the best case time complexity for bubble sort technique will be when the list is
already sorted and that will be O(n).

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

Pseudocode for Selection Sort


Procedure selection_sort(array,N)
array – array of items to be sorted
N – size of array
begin
for I = 1 to N-1
begin
set min = i
for j = i+1 to N
begin
if array[j] < array[min] then
min = j;
end if
end for
//swap the minimum element with current element
if minIndex != I then
swap array[min[] and array[i]
end if
end for
end procedure
Data Structures and Algorithm
Lab 04: Sorting and Searching

Complexity Analysis of Selection Sort


As seen in the pseudocode above for selection sort, we know that selection sort requires two for
loops nested with each other to complete itself. One for loop steps through all the elements in the
array and we find the minimum element index using another for loop which is nested inside the
outer for loop.

Therefore, given a size N of the input array, the selection sort algorithm has the following time and
complexity values.

Worst case time complexity O( n2 ) ; O(n) swaps


Best case time complexity O( n2 ) ; O(n) swaps
Average time complexity O( n2 ) ; O(n) swaps
Space complexity O(1)

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

Pseudocode/Procedure for Linear Search


Procedure LinearSearch(array,first,last,targetValue)
//array – sorted/unsorted array
//first – first element of the array
//last – size of the array
//targetValue – Value to be searched

loop until first is not equal to last - 1


if (array[i] == targetValue )
return i
end for loop
return -1 //flag if targetValue not found
end procedure
Complexity Analysis of Linear Search Algorithm

 Best Time Complexity: O(1)


 Average Time Complexity: O(n)
 Worst Time Complexity: O(n)

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

Pseudocode for Binary Search


Procedure binarySearch(array,N)
array – sorted array
N – size of array

loop until beg is not equal to end


midIndex = (beg + end)/2
if (item == arr[midIndex] )
return midIndex
else if (item > arr[midIndex] )
beg = midIndex + 1
else
end = midIndex - 1
end procedure
Complexity Analysis of Binary Search Algorithm

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.

You might also like