0% found this document useful (0 votes)
11 views

Data Structure - Unit3

Data Structure_unit3

Uploaded by

mukesh dhabe
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Data Structure - Unit3

Data Structure_unit3

Uploaded by

mukesh dhabe
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Searching Algorithms

Searching algorithms are methods or procedures used to find a specific item or element within a
collection of data. These algorithms are widely used in computer science and are crucial for tasks like
searching for a particular record in a database, finding an element in a sorted list, or locating a file on a
computer.

Types of searching:

1. Linear Search
2. Binary Search

1. Linear Search: In this simple algorithm, each element in the collection is sequentially checked
until the desired item is found, or the entire list is traversed. It is suitable for small-sized or
unsorted lists, but its time complexity is O(n) in the worst case.

Linear Search Algorithm

The algorithm for linear search is relatively simple. The procedure starts at the very first index of the
input array to be searched.

Step 1 − Start from the 0th index of the input array, compare the key value with the value present in the
0th index.
Step 2 − If the value matches with the key, return the position at which the value was found.
Step 3 − If the value does not match with the key, compare the next element in the array.
Step 4 − Repeat Step 3 until there is a match found. Return the position at which the match was found.
Step 5 − If it is an unsuccessful search, print that the element is not present in the array and exit the
program.

Example

Let us look at the step-by-step searching of the key element (say 47) in an array using the linear search
method.

Step 1

The linear search starts from the 0th index. Compare the key element with the value in the 0th index, 34.
However, 47 ≠ 34. So it moves to the next element.

Step 2

Now, the key is compared with value in the 1st index of the array.

Still, 47 ≠ 10, making the algorithm move for another iteration.

Step 3

The next element 66 is compared with 47. They are both not a match so the algorithm compares the
further elements.

Step 4

Now the element in 3rd index, 27, is compared with the key value, 47. They are not equal so the
algorithm is pushed forward to check the next element.

Step 5
Comparing the element in the 4th index of the array, 47, to the key 47. It is figured that both the elements
match. Now, the position in which 47 is present, i.e., 4 is returned.

The output achieved is “Element found at 4th index”.

C program for linear search:

#include <stdio.h>

void linear_search(int a[], int n, int key){

int i, count = 0;

for(i = 0; i < n; i++) {

if(a[i] == key) { // compares each element of the array

printf("The element is found at %d position\n", i+1);

count = count + 1;

if(count == 0) // for unsuccessful search

printf("The element is not present in the array\n");

int main(){

int i, n, key;

n = 6;

int a[10] = {12, 44, 32, 18, 4, 10};

key = 18;

linear_search(a, n, key);
key = 23;

linear_search(a, n, key);

return 0;

Output
The element is found at 4 position
The element is not present in the array

2. Binary Search:

This algorithm is applicable only to sorted lists. It repeatedly compares the middle element of the
list with the target element and narrows down the search range by half based on the comparison
result. Binary search has a time complexity of O(log n), making it highly efficient for large sorted
lists.

Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search
algorithm works on the principle of divide and conquer, since it divides the array into half before
searching. For this algorithm to work properly, the data collection should be in the sorted form.

Binary search looks for a particular key value by comparing the middle most item of the
collection. If a match occurs, then the index of item is returned. But if the middle item has a value
greater than the key value, the right sub-array of the middle item is searched. Otherwise, the left
sub-array is searched. This process continues recursively until the size of a subarray reduces to
zero.
Binary Search Algorithm

Binary Search algorithm is an interval searching method that performs the searching in intervals only.
The input taken by the binary search algorithm must always be in a sorted array since it divides the array
into subarrays based on the greater or lower values. The algorithm follows the procedure below −

Step 1 − Select the middle item in the array and compare it with the key value to be searched. If it is
matched, return the position of the median.
Step 2 − If it does not match the key value, check if the key value is either greater than or less than the
median value.
Step 3 − If the key is greater, perform the search in the right sub-array; but if the key is lower than the
median value, perform the search in the left sub-array.
Step 4 − Repeat Steps 1, 2 and 3 iteratively, until the size of sub-array becomes 1.
Step 5 − If the key value does not exist in the array, then the algorithm returns an unsuccessful search.

Example

For a binary search to work, it is mandatory for the target array to be sorted. We shall learn the process of
binary search with a pictorial example. The following is our sorted array and let us assume that we need
to search the location of value 31 using binary search.

First, we shall determine half of the array by using this formula −

mid = low + (high - low) / 2

Here it is, 0 + (9 - 0) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.

Now we compare the value stored at location 4, with the value being searched, i.e. 31. We find that the
value at location 4 is 27, which is not a match. As the value is greater than 27 and we have a sorted array,
so we also know that the target value must be in the upper portion of the array.

We change our low to mid + 1 and find the new mid value again.

low = mid + 1
mid = low + (high - low) / 2

Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.

The value stored at location 7 is not a match, rather it is less than what we are looking for. So, the value
must be in the lower part from this location.

Hence, we calculate the mid again. This time it is 5.

We compare the value stored at location 5 with our target value. We find that it is a match.

We conclude that the target value 31 is stored at location 5.

Binary search halves the searchable items and thus reduces the count of comparisons to be made to very
less numbers.

C program for Binary search:

#include<stdio.h>
void binary_search(int a[], int low, int high, int key){

int mid;

mid = (low + high) / 2;

if (low <= high) {

if (a[mid] == key)

printf("Element found at index: %d\n", mid);

else if(key < a[mid])

binary_search(a, low, mid-1, key);

else if (a[mid] < key)

binary_search(a, mid+1, high, key);

} else if (low > high)

printf("Unsuccessful Search\n");

int main(){

int i, n, low, high, key;

n = 5;

low = 0;

high = n-1;

int a[10] = {12, 14, 18, 22, 39};

key = 22;

binary_search(a, low, high, key);

key = 23;
binary_search(a, low, high, key);

return 0;

Sorting
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange data
in a particular order. Most common orders are in numerical or lexicographical order.

The importance of sorting lies in the fact that data searching can be optimized to a very high level, if data
is stored in a sorted manner. Sorting is also used to represent data in more readable formats.

Important Terms

Some terms are generally coined while discussing sorting techniques, here is a brief introduction to them

Increasing Order
A sequence of values is said to be in increasing order, if the successive element is greater than the
previous one. For example, 1, 3, 4, 6, 8, 9 are in increasing order, as every next element is greater than the
previous element.

Decreasing Order
A sequence of values is said to be in decreasing order, if the successive element is less than the current
one. For example, 9, 8, 6, 4, 3, 1 are in decreasing order, as every next element is less than the previous
element.

Bubble sort
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which
each pair of adjacent elements is compared and the elements are swapped if they are not in order. This
algorithm is not suitable for large data sets as its average and worst case complexity are of O(n 2)
where n is the number of items.

Bubble Sort Algorithm

Bubble Sort is an elementary sorting algorithm, which works by repeatedly exchanging adjacent
elements, if necessary. When no exchanges are required, the file is sorted.

We assume list is an array of n elements. We further assume that swap function swaps the values of the
given array elements.
Step 1 − Check if the first element in the input array is greater than the next element in the array.
Step 2 − If it is greater, swap the two elements; otherwise move the pointer forward in the array.
Step 3 − Repeat Step 2 until we reach the end of the array.
Step 4 − Check if the elements are sorted; if not, repeat the same process (Step 1 to Step 3) from the last
element of the array to the first.
Step 5 − The final output achieved is the sorted array.
Example

We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it short and
precise.

Bubble sort starts with very first two elements, comparing them to check which one is greater.

In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with 27.

We find that 27 is smaller than 33 and these two values must be swapped.

Next we compare 33 and 35. We find that both are in already sorted positions.

Then we move to the next two values, 35 and 10.


We know then that 10 is smaller 35. Hence they are not sorted. We swap these values. We find that we
have reached the end of the array. After one iteration, the array should look like this −

To be precise, we are now showing how an array should look like after each iteration. After the second
iteration, it should look like this −

Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sort learns that an array is completely sorted.

Insertion Sort:

Insertion sort is a very simple method to sort numbers in an ascending or descending order. This method
follows the incremental method. It can be compared with the technique how cards are sorted at the time of
playing a game.

This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always
sorted. For example, the lower part of an array is maintained to be sorted. An element which is to be
'inserted' in this sorted sub-list, has to find its appropriate place and then it has to be inserted there. Hence
the name, insertion sort.

The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in
the same array). This algorithm is not suitable for large data sets as its average and worst case complexity
are of Ο(n2), where n is the number of items.

Insertion Sort Algorithm


Now we have a bigger picture of how this sorting technique works, so we can derive simple steps by
which we can achieve 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

Example

We take an unsorted array for our example.

Insertion sort compares the first two elements.

It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.

Insertion sort moves ahead and compares 33 with 27.


And finds that 33 is not in the correct position. It swaps 33 with 27. It also checks with all the elements of
sorted sub-list. Here we see that the sorted sub-list has only one element 14, and 27 is greater than 14.
Hence, the sorted sub-list remains sorted after swapping.

By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10. These values are not in a
sorted order.

So they are swapped.

However, swapping makes 27 and 10 unsorted.

Hence, we swap them too.

Again we find 14 and 10 in an unsorted order.

We swap them again.


By the end of third iteration, we have a sorted sub-list of 4 items.

This process goes on until all the unsorted values are covered in a sorted sub-list. Now we shall see some
programming aspects of insertion sort.

Selection Sort:

Selection sort is a simple sorting algorithm. This sorting algorithm, like insertion sort, 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 array and swapped with the leftmost element, and that
element becomes a part of the sorted array. This process continues moving unsorted array boundaries by
one element to the right.

This algorithm is not suitable for large data sets as its average and worst case complexities are of O(n2),
where n is the number of items.

Example

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.

For the second position, where 33 is residing, we start scanning the rest of the list in a linear manner.

We find that 14 is the second lowest value in the list and it should appear at the second place. We swap
these values.

After two iterations, two least values are positioned at the beginning in a sorted manner.

The same process is applied to the rest of the items in the array −

You might also like