Data Structures - Algorithms
Data Structures - Algorithms
4. Searching Algorithm:
Searching algorithms are the ones that are used for searching elements or groups of elements
from a particular data structure.
5. Sorting Algorithm:
Sorting is arranging a group of data in a particular manner according to the requirement.
Sorting algorithms are used to sort groups of data in an increasing or decreasing manner.
6. Hashing Algorithm:
Hashing algorithms work similarly to the searching algorithm. But they contain an index with
a key ID. A key is assigned to specific data.
Sorting
Sorting refers to rearrangement of a given array or list of elements in ascending or
descending order. Sorting reduces the complexity of a problem. The importance of sorting
lies in the fact that data searching can be optimized to a very high level. Sorting is also used
to represent data in more readable formats. We are going to look at bubble sort, insertion sort
and quick sort.
1. 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.
Example
We take an unsorted array for our example.
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.
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 −
And when there's no swap required, bubble sort learns that an array is completely sorted.
Example
We take an unsorted array for our example.
It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.
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.
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.
Quick sort
Quick sort uses the technique of divide-and-conquer. Quick sort is a highly efficient sorting
algorithm and is based on partitioning of array of data into smaller arrays. A large array is
partitioned into two arrays one of which holds values smaller than the specified value, say
pivot, based on which the partition is made and another array holds values greater than the
pivot value. Quicksort partitions an array and then calls itself recursively twice to sort the two
resulting subarrays. This algorithm is quite efficient for large-sized data sets.
We proceed as follows:
1. Pick an arbitrary element of the array (the pivot).
2. Divide the array into two segments, those that are smaller and those that are greater, with
the pivot in between (the partition phase).
3. Recursively sort the segments to the left and right of the pivot.
and we pick 3 as our pivot. Then we have to compare each element of this (unsorted!) array
to the pivot to obtain a partition where 2, 1 are to the left and 4, 7, 8, 4 are to the right of the
pivot. Then the two segments containing (2, 1) and (4, 7, 8, 4) are further divided using an
arbitrary figure as a pivot until all the segments are sorted.