Embedded Systems

Sorting Algorithms in C

Sorting Algorithms in C programming is vast topic and often used in most common interview questions to check the logic building aptitude. Sorting in general refers to ordering things based on criteria like numerical, chronological, alphabetical, hierarchical etc.

For example :- Keeping business records and want to sort them by ID number or last name of client? Then you’ll need a sorting algorithm.

There are many articles on internet about the different sorting algorithms but i have discussed here some of them  basics and inefficient. To understand the more complex and efficient sorting algorithms, it’s important to first understand the simpler and slower algorithms.

Fact – It is estimated that around 25% of all CPU cycles are used to sort data.

Selection Sort

Algo – Repeatedly finding the minimum element and swapping.

It basically determines the minimum (or maximum) of the list and swaps it with the element at the index where its supposed to be. The process is repeated such that the nth minimum (or maximum) element is swapped with the element at the n-1th index of the list. The below is an implementation of the algorithm in C.

Bubble Sort

Algo – Swapping the adjacent elements

Bubble Sort works by comparing each element of the list with the element next to it and swapping them if required. With each pass, the largest of the list is “bubbled” to the end of the list whereas the smaller values sink to the bottom. It is similar to selection sort although not as straight forward. Instead of “selecting” maximum values, they are bubbled to a part of the list. An implementation in C.

Insertion sort

Algo – Shift elements one by one

Try recalling how you sort a deck of cards. You start from the beginning, traverse through the cards and as you find cards misplaced by precedence you remove them(like making space/here we call it hole/) and insert them back into the right position. Eventually what you have is a sorted deck of cards. The same idea is applied in the Insertion Sort algorithm. The following is an implementation in C.

Merge Sort

Algo – Divide and Conquer algorithm or Merge two array

Merge sort is a recursive algorithm that continually splits a array in equal two halves. If the array is empty or has one item, it is sorted by definition (the base case). If the array has more than one item, we split array and recursively invoke a merge sort on both halves. Once the two halves are sorted, the fundamental operation, called a merge, is performed. Merging is the process of taking two smaller sorted array and combining them together into a single, sorted, new array.

Quick Sort

Algo – Divide and Conquer algorithm or Partitioning array

1.) Choose any element as pivot(here i chose last) element from the array list.

2.) Partitioning the array so that all the elements with value less than the pivot will come before the pivot and the element with value greater will come after the pivot with in the same array, which make pivot element in the sorted position.(If the reverse the order we are reversing the sorting order that is descending).

3.) Apply recursively the 3rd step to the sub array of the element with smaller values and separate the sub array of the elements with the greater values until partition of array containing 2 elements.