Before knowing the algorithms in data structures, first of all, we will see the basic concept, what is meant by the data structure and what is meant by an Algorithm?

  • Data Structure:

Data Structure is the process of organizing data, in other words, it is the process of arranging data in computer memory in such a way that it can give data quickly to the processor at the time of calculations. The data in a data structure is designed and organized in such a way that it reduces the complexity and increases the efficiency.

Example:  if we have data of students as name and age. We store it in a sheet name as “student record”  in that name has string data types and age have integer data type. Data are stored in a sheet as “Rina 25”, “Shraddha 30” both that field stored in such a way that we can easily access when we need.

 

  • Algorithm:

An Algorithms is a step by step procedure, which defines the set of instruction executed to get the expected output.

There are some top algorithm in data structure, every programmer should know –

  1. Binary Search Algorithm
  2. Sort Algorithm
  3. Hashing

 

  • Binary Search Algorithm:

Binary search is the simple and basic algorithm in the data structure. Binary search is used to find data quickly and easily. In Binary search, data should be stored in a list and arranged in a sequential order. For a random number, we can’t apply binary search algorithm. In binary search, we have to find the middle of the list. Then compare that middle value with targeted value if middle value is less than targeted value then consider remaining half and again find the middle of the list continue this process until we get our targeted value.

Let’s see step by step procedure for binary search –

 

Step 1:  Read the targeted value from the user

Example: Find no. 23

 

2

5 8 12 16 23 38 56 72

91

Step 2: Find the middle of the list

 

2

5 8 12 16 23 38 56 72

91

 

Step 3: Compare the middle value with the targeted value of the list

Here 23 > 16 so we can say 23 is not in 1st half part

 

1st Half                                                                                                                                                     2nd half

2

5 8 12 16 23 38 56 72

91

 

Step 4: If the middle-value match with targeted value then we say targeted no. find and can close the procedure.

Step 5: If a middle value doesn’t match with targeted value then compare a middle value with 2nd half, is smaller or greater than targeted value.

Now middle value is 56, so compare 23 with 56

23 < 56

2

5 8 12 16 23 38 56 72

91

 

Step 6: 23 > 56 so number is on left side and we compare between 23 & 38

 

2

5 8 12 16 23 38 56 72

91

 

Step 7: Repeat the same process until we find the search element in the list

We found 23 at 5th position

 

Step 8: If that element also doesn’t match with the search element, then display “Element not found in the list!!!” and terminate the function.

 

  • Sorting Algorithms

Sorting is the process of arranging the data in a sorted order. The sorted data in a list in ascending or descending order. There are many things in our day-to-day life where we use sorting and searching, searching is very simple if we have sorted data, for example, if we want to find a phone number from phone directory, to find roll number of student. etc for that we use sorting.

There are no. of sorting algorithm –

  1. Bubble Sort
  2. Insertion Sort
  3. Selection Sort

1. Bubble Sort :

Bubble sort is the simple algorithm used to sort a large amount of data. It is a comparison based algorithm compare each pair of a number until the entire array is sorted.

Example:

Step 1: Take unsorted array

 

4

1 5 9

8

 

Step 2: Compare first two elements, and sort them in ascending order.

In this case, value 4 is greater than 1, so swap them to sort.

4

1 5 9

8

 

Step 3: Compare next two elements 4 and 5.

4 is smaller than 5 so they are already sorted.

 

1

4 5 9

8

 

Step 4: Compare next two elements 5 and 9.

Here also 5 is smaller than 9, means they are also in sorted order.

 

1

4 5 9

8

 

Step 5: Compare next two elements 9 and 8.

 

1

4 5 9

8

Here 9 is greater than 8 so we swipe these two elements to sort

 

1

4 5 8

9

In the end, we will get a sorted array.

 

2. Insertion Sort:

Insertion sort is also a comparison based algorithm but in that we maintain sub-list. This algorithm is not suitable for a large amount of data sorting. Let us see an example.

 

Step 1: Take unsorted array

 

25

14 31

27

 

Step 2: Compare first two values 25 and 14.

Here 14 is smaller than 25, so swap these two elements. Now element 14 is in sub-list

 

14

25 31

27

 

Step 3: Check next two elements 25 and 31.

In this case 25 and 31 are already in ascending order, so no need to swap.  

Step 4: Now we have 14 and 25 in sub-list.

These values are in ascending order, so no need to swap.

Step 5: Now move ahead and check next element 31 and 27

 

14

25 31

27

 

Here 31 and 27 are not in correct order so swap these elements

 

14

25 27

31

 

Step 6: Now we have 25 and 27 are in sub-list so compare them.

 

14

25 27

31

 

Here  25 and 27 also in ascending order so we close the process and we get below sorted list.

 

14

25 27

31

 

In the end, we will get a sorted array.

 

3. Selection Sort:

Selection sort is the simplest sorting algorithm. Firstly it scans the array and finds the smallest element from an array then this smallest element replace with first position element. Continue this procedure until we find the sorted element.

Let’s see step by step procedure –

Step 1:  Take unsorted array

 

25

14 31

27

 

Scan the whole array to find the smallest number. After scanning we got 14 is the lowest number.

Step 2: Replace the lowest number with the first element and we will get

 

14

25 31

27

 

Step 3: Now find second lowest number and replace with the second position. Second lowest no. is 25 and is already at correct position.

Step 4: Continue this procedure until we get a sorted array.

 

14

25 27

31

 

In the end, we will get a sorted array.

 

Hope this article will help you to understand searching and sorting algorithms in data structure. We will discuss some advanced algorithms in the next article. Till then share this article with your friends and to get the implementation of these algorithms in C programming comment down below your contact details and subscribe our CodeKul blog.