Array | Sorting

Sorting an array means arranging the elements of the array in a certain order. Generally sorting in an array is done to arrange the elements in increasing or decreasing order.

Important terminologies related to Sorting:

Here we will see several algorithms used for array sorting. But before that let us see some important terminologies related to sorting:

In-Place Sorting: In-place sorting means arranging the elements without using any extra space other than the given array.

Stability of Sorting: A sorting algorithm is said to be stable if the relative order of the same valued elements is preserved in the final sorted array i.e., the relative order of the same valued elements in the sorted array is the same as in the original array.

Some commonly used Array Sorting Algorithms:

Bubble Sort: Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large arrays as its average and worst-case time complexity is quite high.

Selection Sort: Selection sort is another sorting technique in which we find the minimum element in every iteration and place it in the array beginning from the first index. Thus, a selection sort also gets divided into a sorted and unsorted subarray.

Insertion Sort: Insertion sort similarly to the way we sort playing cards in our hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed in the correct position in the sorted part.

Merge Sort: It is a sorting algorithm that is based on the Divide and Conquer paradigm. In this algorithm, the array is repeatedly divided into two equal halves and then they are combined in a sorted manner.

Quick Sort: This is a sorting algorithm based on the divide and conquer approach where an array is divided into subarrays by selecting a pivot element (element selected from the array).

Heap Sort: Heap sort is a comparison-based sorting technique based on Binary Heap data structure. It is similar to the selection sort where we first find the minimum element and place the minimum element at the beginning. Repeat the same process for the remaining elements.

Counting Sort: Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of elements having distinct key values (kind of hashing). Then do some arithmetic to calculate the position of each element in the output sequence.

To learn more about all other types of Sorting Algorithms, refer to the below articles:

Comparison of Time Complexity of Sorting Algorithms:

Name Best Case   Average Case   Worst Case  Memory Stable    Method Used
Quick Sort N * logN N * logN N2 logN No Partitioning
Merge Sort N * logN N * logN N * logN N Yes Merging
Heap Sort N * logN N * logN N * logN 1 No Selection
Insertion Sort N N2 N2 1 Yes Insertion
Selection Sort N2 N2 N2 1 No Selection
Bubble Sort N N2 N2 1 Yes Exchanging
Counting Sort N+K N+K N+K N+K Yes Hashing
Some practice problems on Array Sorting: Quick Links :

  • Last Updated : 26 Sep, 2023

Share your thoughts in the comments