Basic Sorting and Searching Algorithms for Arrays, at a glance
Bubble SortIt’s a sorting algorithm, in which each pair of adjacent items are compared and swapped if they are in wrong order. The comparison is repeated until no swaps are needed, indicating that the list is sorted. The smaller elements ‘bubble’ to the top of the list, hence, the name Bubble Sort. In this like selection sort, after every pass the largest element moves to the highest index position of the list.
Algorithm:It starts with the first element of the list. The comparison of the adjacent pair of elements and swapping is done until the list is sorted as follows
1. Compare two adjacent elements and check if they are in correct order (that is second one has to be greater than the first).
2. Swap them if they are not in correct order.
Let iter denotes the number of iterations, then for iter ranging from 1 to n-1 (where n is total number of elements in the list) check
1. If value of the second item at position iter is lesser than the value of the first item i.e. at position iter-1, then swap them.
2. Else, move to the next element at position iter+1.
The effective size of the list is hence reduced by one after every pass and the largest element is moved to its final position in the sorted array. Repeat the two steps until the list is sorted and no swap is required i.e. the effective size is reduced to 1.
Properties:1. Best Case performance – When the list is already sorted, we require only one pass to check, hence O(n).
2. Worst Case performance – When the list is in the reverse order, n number of comparisons and swap are required to be done n number of times, hence O(n2).
3. Average Case performance – O(n2)
4. It does not require any extra space for sorting, hence O(1) extra space.
|
One of the most elementary sorting algorithms to implement - and also very inefficient. Runs in quadratic time. A good starting point to understand sorting in general, before moving on to more advanced techniques and algorithms. A general idea of how the algorithm works and a the code for a C program. |
Another quadratic time sorting algorithm - an example of dynamic programming. An explanation and step through of how the algorithm works, as well as the source code for a C program which performs insertion sort. |
Another quadratic time sorting algorithm - an example of a greedy algorithm. An explanation and step through of how the algorithm works, as well as the source code for a C program which performs selection sort. |
An inefficient but interesting algorithm, the complexity of which is not exactly known. |
An example of a Divide and Conquer algorithm. Works in O(n log n) time. The memory complexity for this is a bit of a disadvantage. |
In the average case, this works in O(n log n) time. No additional memory overhead - so this is better than merge sort in this regard. A partition element is selected, the array is restructured such that all elements greater or less than the partition are on opposite sides of the partition. These two parts of the array are then sorted recursively. |
Efficient sorting algorithm which runs in O(n log n) time. Uses the Heap data structure. |
Commonly used algorithm used to find the position of an element in a sorted array. Runs in O(log n) time. |
Testing Zone For Programmers-
Try out our online Multiple-Choice-Question tests in Programming and Computer Science!

