Sorting Algorithms

Sorting Algorithms

In this blog we will look at the concept of sorting and 6 sorting Algorithms .

Let me share with you something about myself:

I am a CS Undergrad. I love馃槏 to learn and explore technology. I am interested in full stack web development and Machine learning. I am very much interested in data structure and algorithms.

What is Sorting

Sorting is a process of arranging list of numbers in either ascending or descending order. Sorting can be divided into two categories.

i. Internal Sorting

ii. External Sorting

Internal sorting takes place in the main memory of the computer. Internal sorting can take advantage of random access nature of the main memory.

External sorting is carried on secondary storage. If the input data is such that it cannot be adjusted in the memory entirely at once, it needs to be stored in a hard disk, floppy disk, or any other storage device.

SORTING TECHNIQUES can be compared on basis of following factors:

Number of Comparisons: Total number of comparison while performing sorting. Total time taken by sorting algorithm depends on comparisons. So number of comparison decides the complexity of any sorting technique.

Number of swaps: To arrange numbers in proper order swapping of numbers is done. It is a common process done in almost all sorting algorithms.

Adaptive: If all numbers are already sorted then it must take minimum time as it does not have to rearrange the elements.

Stable: If after sorting , identical numbers appear in same sequence as in the original unsorted list.

Memory requirement: The amount of memory required for performing the algorithm

Now let鈥檚 look at commonly used algorithms

  1. Bubble Sort

  2. Insertion Sort

  3. Selection sort

  4. Merge Sort

  5. Quick Sort

  6. Shell Sort

BUBBLE SORT :

Bubble sort is one of the simplest and the most popular sorting method. The basic idea behind bubble sort is as a bubble rises up in water. i.e. smallest element goes in beginning

The worst complexity is O(n虏) where n is number of elements. This algorithm takes (n-1) passes where n is number of elements. It is also adaptive, when elements are already sorted then it takes minimum time i.e. O(n). It is also stable .

Now lets see the example 馃榾

bubble-sort.png

bubble-sort-2.png

INSERTION SORT

The insertion sort is based on the principle of inserting elements at its correct place in a previously sorted list . The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.

The worst complexity is O(n虏) where n is number of elements. This algorithm takes (n-1) passes where n is number of elements. It is also adaptive, when elements are already sorted then it takes minimum time i.e. O(n). It is also stable .

Now lets see the example 馃榾

insertion-sort.png

insertion-sort-2.png This process goes on until all the unsorted values are covered in a sorted sub-list.

SELECTION SORT

Selection sort is very simple sorting algorithm. The list is divided into two parts sorted and unsorted .Initially sorted part is empty and unsorted part contains entire list. Then minimum element is swapped with leftmost element. This process continues moving unsorted array boundary by one element to the right.

The worst complexity is O(n虏) where n is number of elements. This algorithm takes (n-1) passes where n is number of elements. It is not adaptive because when elements are already sorted then it does not take minimum time i.e. O(n虏). It is also not stable.

Now let鈥檚 the example 馃榾

selection-sort-1.png

selection-sort-2.png

MERGE SORT

Merge sort is the algorithm which follows divide and conquer approach. Consider an array A of n number of elements. The algorithm processes the elements in 3 steps.

  1. If A Contains 0 or 1 elements then it is already sorted, otherwise, Divide A into two sub-array of equal number of elements.

  2. Conquer means sort the two sub-arrays recursively using the merge sort.

  3. Combine the sub-arrays to form a single final sorted array maintaining the ordering of the array.

The time complexity is O(n log n) where n is number of elements. It is stable.

Now let鈥檚 see the example 馃榾

merge-sort-1.png

merge-sort-2.png

QUICK SORT

Quick sort works on the principle of divide and conquer approach in which array is split into subarrays. Then the subarrays are recursively called to sort the elements.

There are 3 steps for sorting in Quick sort

  1. Pivot element is selected .Any number from array is selected as pivot element.(Mostly leftmost or rightmost element.)

  2. The elements smaller than the pivot element are added in the left part and elements greater than pivot element are added on the right part. Then pivot is at its final position.

  3. Then all the steps are applied recursively to the left part and right part of the array.

This algorithm is also known as Selection Exchange Sort or Partition Exchange Sort. The time complexity is O(n log n) where n is number of elements. It is not adaptive because when elements are already sorted then it does not take minimum time but it take for O(n虏) for unsorted list . It is also not stable.

Now lets look at example 馃榾

quick-sort.png

SHELL SORT

Shell sort is an algorithm that first sorts the elements far apart from each other and successively reduces the interval between the elements to be sorted. It is a generalized version of insertion sort.

The time complexity is O(n log2n) where n is number of elements. It is adaptive but not stable.

Now let鈥檚 see the example 馃榾

shell-sort-1.png

shell-sort-2.png

shell-sort-3.png

Conclusion

So we looked on the brief idea of sorting ,types of sorting , factors of sorting algorithm and 6 commonly used sorting algorithms with examples. I hope you got the basic idea of sorting techniques. Every algorithm has its own advantages and disadvantages . Sorting algorithm should not be judged on the basis of time complexity. The algorithms are used on the basis of the need as under right circumstance each algorithm works in more efficient way.

Do share the post if you like馃憤

Feel free to reach me out馃榾

LinkedIn

Twitter