Data structures & algorithms (Beginner to Intermediate)

Free guided path13 chapters99+ problems

Earn badges and level up

Introduction

Sorting stands as one of the foundational algorithms in the realm of data structures and algorithms. It's the process of arranging elements in a specific order, facilitating efficient searching, retrieval, and analysis of data. From arranging a list of names alphabetically to optimizing massive datasets, sorting algorithms play a pivotal role in diverse applications across industries. For example, consider you have five siblings and you want to arrange them according to height.

Now if you wish to arrange them in increasing or decreasing order of height and then algorithm, you’ll perform to do this task is sorting. But more interestingly in How much time, you can do it? Suppose if you do it in five sec and when your brother is given a chance and he does it in three sec then his idea would be best among both of you. That’s thecomplexity of your algorithm.

In this article, we will discuss Sorting Algorithms In Data Structures in detail.

What is Sorting in Data Structure?

Sorting is the process of arranging items in a specific order or sequence. It is a common algorithmic problem in computer science and is used in various applications such as searching, data analysis, and information retrieval.

In other words, you can say that sorting is also used to represent data in a more readable format.

Example

Some real-life examples of sorting are:-

Contact List in Your Mobile Phone also contains all contacts arranged alphabetically (lexicographically). So if you look for contact then you don’t have to look randomly and can be searched easily and many others like apps on your phone.

Keywords in Your book are also in a lexicographical manner and you can find it according to Chapter.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

Types of Sorting Techniques in Data Structure

Although there is no. of sorting algorithms best algorithm is which can solve a problem in the minimum time and minimum space required to do so. Some types of the Sorting algorithm are:-

Bubble Sort

Bubble Sort is one of the simplest algorithms which repeatedly iterates through the list, compares the elements next to each other, and swaps according to the condition passed to them. Confusing isn’t it? It would be right to say this Sorting as an idea or approach to solving a problem what we people usually do (Brute force Solution). Its time complexity for best case is O(n) and for worst case is O(n^{2}).

Insertion Sort

Insertion sort is a simple sorting algorithm. It works by building a sorted list one element at a time. It takes an element from the unsorted part of the list and places it in its correct position in the sorted portion. This process is repeated until all the elements of the list are sorted. Although, it is not the most efficient sorting algorithm for large datasets, it is easy to understand and implement for smaller lists. Its time complexity for best case is O(n) and for worst case is O(n^{2}).

Selection Sort

Selection sort works by repeatedly finding the smallest element from the array and then putting it in the beginning and then again finding the smallest from the rest of the array other than the previous one and arranging it in beginning. So basically we can say that it has two subarrays in it one is Sorted and the other is unsorted. Its time complexity for both best and worst case scenarios is O(n^{2}).

Quick Sort

It is based on partition. It is also known as partition exchange sorting. The basic concept of this algorithm is to pick one element from an array and rearranges the rest of elements around it and that element is known as pivot element and that element divides the main array into two sub-arrays. Once the pivot is fixed then it moves all the elements lesser than it to left and elements greater than it to right. This procedure is repeated recursively until there is one element left in the sub-array. Its time complexity for best and average case scenarios is O(n log n). For worst case scenario, it is O(n^{2}). Its space complexity is O(n).

Merge Sort

Merge sort is a popular sorting algorithm that uses a divide-and-conquer approach. It works by recursively dividing an array into two halves, sorting each half, and then merging the two sorted halves back together to produce a fully sorted array. The time complexity of merge sort is O(n log n), which makes it an efficient sorting algorithm for large datasets. Its space complexity is O(n).

Bucket Sort

The bucket sort algorithm is a distribution sort algorithm that operates on a collection of elements by dividing the elements into several buckets. Each bucket is further sorted individually, either using a different sorting algorithm or recursively applying this algorithm. At last, the elements in each bucket are concatenated to obtain the final sorted list.

It has an average time complexity of O(n) if the elements are uniformly distributed, which makes it an efficient sorting algorithm for uniformly distributed data. However, if the data distribution is not uniform, the performance of bucket sort can degrade to O(n^{2}), making it less efficient than other sorting algorithms such as quicksort.

Heap Sort

Heap sort is one of the best techniques possible for sorting, where we first build the heap using the given elements. The heap is always a complete binary tree. We use the max-heap/min-heap property to sort the elements. Once the heap has been created satisfying all conditions, we swap the last element with the root element and delete the last node. In the max-heap method, we take root as the max element, and we compare it with its left and right child elements. If the left or right element data is greater than the root element data, we swap the data in both nodes. Then the heapify algorithm is called recursively to the subtree to sort them. In the min-heap, we will search for the min element. Its time complexity for average and worst case scenarios is O(n log n). For best case scenario, it is O(n). Its space complexity is O(1).

Counting Sort

Counting sort is an integer sorting algorithm, which sorts elements in an array according to small positive integer keys. The method organizes an array by counting how many times each distinct array element appears. It is not a comparison-based algorithm as it doesn’t compare any two elements; instead, it hashes the value with their corresponding value and uses the position array to find the position of the current element in the sorted array. Two operations that take place in the counting sort are: Count the number of unique elements in the array. Then, determine each element's location or the index at which it will appear in the sorted array.

Radix Sort

Radix sort is known as an integer sorting algorithm that sorts elements by individually grouping digits based on their face value. Being a non-comparative sorting algorithm, it distributes its elements to avoid comparison. Radix sort comes in handy when the range of elements is N^{2}, and the implementation of counting sort cannot be implemented because it is only efficient for elements in the range 1 to K, where K is the maximum element.

Shell Sort

Shell sort is an in-place comparison sort. It's a generalization of insertion sort that allows the exchange of elements that are far apart. The algorithm sorts elements at specific intervals and gradually reduces the interval to perform a final sort. In it, the original array is divided into smaller subarrays based on the gap. Elements at a specific gap are sorted first. This gap is then reduced, and the process is repeated until the gap becomes 1, leading to a completely sorted array.

Comb Sort

Comb sort is a simple yet efficient sorting algorithm that builds upon the idea of bubble sort. It works by comparing and swapping adjacent elements with a gap that initially starts large and progressively reduces with each pass. This gap reduction factor is typically set around 1.3. The algorithm continues to traverse the list, reducing the gap until it becomes 1. At this point, the algorithm performs a final iteration similar to the bubble sort to ensure all elements are correctly placed. Comb sort is efficient for medium-sized datasets and is a small improvement over bubble sort because of its larger gap sizes, which can eliminate multiple small elements in one pass.

Cycle Sort

Cycle sort is a unique and in-place sorting algorithm that aims at minimizing the number of writes to the data storage. It works by identifying cycles within the data and rotating them into their sorted positions. It selects an unsorted element, finds the cycle it belongs to, and then rotates the elements within that cycle to their correct positions. This process continues until all elements are in their proper places. While it is not the fastest sorting method, it is efficient for situations where minimizing data writes is vital, such as with certain memory storage devices or Data structure.

Bitonic Sort

Bitonic sort is a parallel sorting algorithm used in parallel computing environments. It works by recursively dividing a list of elements into two parts: a bitonic sequence, which is a sequence that starts in ascending order and then switches to descending order, and another bitonic sequence, which starts in descending order and switches to ascending order. These two parts are then merged together to create a single sorted sequence. Bitonic sort's strength lies in its suitability for parallel processing, that makes it efficient for large datasets when multiple processors or cores are available. However, it is not as straightforward to implement in a non-parallel environment.

Tree Sort

Tree sort is a sorting algorithm that builds a binary search tree from the elements in the input list and then performs an in-order traversal of the tree to retrieve the sorted values. The process involves inserting each element into the tree based on its value, ensuring that the left subtree contains the smaller values, and the right subtree contains the larger values. Once all elements are inserted, the in-order traversal collects the values in ascending order. Tree sort is simple to understand and can be efficient, but it's not as fast as some other sorting methods, especially for large datasets, due to the probability for unbalanced trees.

Stooge Sort

Stooge sort is an unusual and inefficient sorting algorithm that recursively sorts a list by repeatedly sorting the first two-thirds and last two-thirds of the list while leaving the middle third unsorted. This process continues until the entire list is sorted. Despite its simplicity, it is quite slow and not practical for sorting large datasets due to its poor time complexity, specifically, O(n^(log 3/log 1.5)), which makes it significantly slower than efficient sorting algorithms like quicksort or merge sort. It's primarily used for educational purposes or as a fun coding challenge.

When you perform sorting on an array/elements, many problems become easy (e.g. min/max, kth smallest/largest)

Performing Sorting also gives no. of algorithmic solutions that contain many other ideas such as:

Iterative

Divide-and-conquer

Comparison vs non-comparison-based

Recursive

The main advantage of sorting istime complexityand that’s the most important thing when you solve a problem because it’s not enough you’re able to solve a problem but you should be able to solve it in the minimum time possible. Sometimes problems can be solved easily and quickly based on sorting which can prevent you from every Coder’s Nightmare i.e. TLE (Time Limit Exceeded).

Sorting in Data Structure Categories

The Sorting categories in data structures can be broadly classified into the following types:

Comparison-based Sorting Algorithms

These algorithms compare the elements being sorted to each other and then place them in the desired order. Examples include Bubble Sort, Selection Sort, Insertion Sort, QuickSort, Merge Sort, and Heap Sort.

Non-Comparison-based Sorting Algorithms

These algorithms do not compare the elements being sorted to each other. Instead, they use some specific characteristics of the data to sort them. Examples include Counting Sort, Radix Sort, and Bucket Sort.

In-place Sorting and Not-in-place Sorting

‘In place’ means whenever you are sorting and you don’t require any extra space except the input space excluding the constant space which is used for variables or iterators. It also doesn’t include the space used for stack in the recursive algorithms. Now, In merge sort, when we perform merging then the merge function requires extra linear space which is not constant. So, it is not in-place. Hence, it is called ‘out of place’ algorithm or sorting.

On the other hand, Quicksort is kind of sorting which uses only some constant number of variables for partitioning purpose. So, it is an in-place algorithm or sorting.

Stable and Not stable Sorting

A sorting algorithm when two objects with equal keys appear in the same order in the sorted output as they appear in the unsorted input.

Whereas a sorting algorithm is called as an unstable sorting if there are two or more objects with equal keys which don’t appear in the same order before and after sorting.

Above we can see an example of stable sorting here number 26 is appearing two times at respective position 6 and 8 and their occurrence is kept in unsorted and sorted array i.e. element 26 (blue) at position 6 appears first in unsorted and then appears in sorted array.

One thing should be kept in mind in the case of an unstable sort, the order of appearance/occurrence before and after sorting is not necessarily preserved.

There are some sorting algorithms which are stable by nature such as Insertion sort, Merge Sort, Bubble Sort, etc. while on the other hand sorting algorithms like Heap Sort, Quick Sort are vice-versa.

Adaptive and Non–Adaptive Sorting Algorithm

When occurrence of the elements to be sorted of an input array matters the time complexity of a sorting algorithm, then that algorithm is called “Adaptive” sorting algorithm.

For example, Insertion sort is an adaptive sorting algorithm like in the case if input is already sorted then we know that time complexity will be O(n). That’s why If the input array is almost sorted then choose insertion sort, though this is not the only thing to be kept in mind for Insertion sort over other sorting algorithms.

Merge Sort is a “Non-Adaptive” Sorting algorithm because the order of the elements in the array doesn’t matter So the time complexity of algorithm will always be O(nlogn).

Sorting in a data structure is arranging the elements of the data structure in a specific order. Now, this order can be increasing or decreasing. It makes searching for elements in them easy and quick.

What is sorting an array?

Sorting an array refers to arranging the elements in the array in a specific order, typically in ascending or descending order. This process involves comparing elements and rearranging them based on a set criterion, like numerical or alphabetical order, to make data searching and analysis more efficient.

What is sorting in computer organization?

The fastest sorting algorithm depends on the specific data and its size. Generally, for small datasets, simpler algorithms like insertion sort or bubble sort are fast. For larger datasets, more efficient algorithms like quicksort or merge sort are faster.

Why is sorting important?

Sorting is crucial for organizing data, facilitating efficient searching, retrieval, and analysis, which are essential tasks in various applications and industries.

Which sorting is best and why?

The best sorting algorithm depends on the specific requirements of the problem, including data size, structure, and distribution, as well as time and space constraints. There is no universally "best" sorting algorithm.

Conclusion

In this article, we've learned Sorting Algorithms In Data Structures and the different types of sorting techniques like bubble sort, insertion sort, quick sort, and merge sort. If you want to learn more about sorting, given below are some recommended articles.