Get me outta here!

Friday, February 25, 2022

Sorting Techniques

 An algorithm is thus a sequence of computational steps that transform the input into output. Sorting is the basic and most common computational problem. Allocating scarce resources is the most beneficial way. The learning of Algorithms is basically finding the efficient way to solve problems. Sorting is a classical and important algorithmic problem. Some types of techniques are given below:

Bubble Sort: It compares adjacent numbers in pairs. We have to use nested loops for comparing arrays, traversing, and decrement value by 1. 

Selection Sort: It searches the smallest elements from the positions starting at index 1. We need two identifiers and one smallest value indicator to sort the numbers.

Insertion Sort: It will start from index 1. Then compare backward with all previous elements and if the previous element is larger at that time shift it forward otherwise stop comparing.

Merge Sort: It works in the divide and conquers technique. It first checks if the endpoint is greater than the start point or not. If yes then calculate mid-point and it will divide into two sub-arrays from start to mid and mid+1 to end. Finally, when the two recursive calls have done calling, it will stop. Then single values will be copied into a temporary array in ascending order. After that, it will be copied into the original half of the array. Then the second half will start working as the first half. In the end, the two half will compare their elements and merge them together.

Quick Sort: Quicksort also follows the divide and conquer technique. It works in 3 ways. 

  • Pivot
  • Partition
  • Sub-arrays
First pindex starts from i. If arr[i]<pivot, then it will be swapped. After swapping we have to increment the value of pindex and the following it will swap pindex with pivot. Then the recursive calls will traverse left and right respectively pindex-1 and pindex+1.

Counting Sort: Find the count of every distinct element in the array. Calculate the position of each element in a sorted array.


0 comments:

Post a Comment