Javatpoint Logo
Javatpoint Logo

Sorting Algorithms in C++

Sorting algorithms are foundational tools in computer science and data processing. They enable the arrangement of data elements in a specific order, making it easier to search, retrieve, and analyze information efficiently. Sorting is a fundamental operation in a wide range of applications, from database management to web searches, and from scientific simulations to video game mechanics.

Sorting Algorithms in C++

The Need for Sorting Algorithms

Data frequently arrives in unsorted or skewedly arranged forms in the digital world. For instance, the search engine results are normally displayed in a sorted order based on relevance when you run a search. Using sorting algorithms, you may quickly filter products in a list on an e-commerce website by their price, rating, or other characteristics. Similar to this, sorting is necessary for effective data retrieval when working with huge datasets or databases.

Overview of Sorting Algorithms

Data items are arranged into a particular order using sorting algorithms based on predetermined criteria. They can be divided into a number of classes, each with a unique set of traits and applications. The main groups of sorting algorithms are:

  1. Comparison-Based Sorting Algorithms: These algorithms compare data elements and, if necessary, exchange out of order ones.
    • Comparison Sorts: Examples of comparison-based sorting algorithms include Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, and Quick Sort. These algorithms rely on comparisons between elements to determine their relative order.
    • Non-Comparison Sorts: Non-comparison sorting algorithms, such as Counting Sort, Radix Sort, and Bucket Sort, do not rely on element comparisons. They are often more efficient than comparison sorts in specific scenarios but have limitations.
  2. Adaptive Sorting Algorithms: Based on the data's original order, adaptive sorting algorithms modify their performance. They can be quicker with partially ordered data and slower with entirely random input.
    • Adaptive Comparison Sorts: Some comparison-based sorting algorithms, like Insertion Sort, are adaptive and perform well when given partially sorted data.
  3. Stable Sorting Algorithms: The relative order of equal elements is maintained using stable sorting algorithms. In other words, even after sorting, if two elements have the same key, their positions won't change.
    • Stable Comparison Sorts: Examples include Bubble Sort and Merge Sort.
  4. In-Place Sorting Algorithms: In-place sorting algorithms do not require additional memory to sort the data. They rearrange the elements within the existing data structure.
    • In-Place Comparison Sorts: Selection Sort and Quick Sort are in-place sorting algorithms.
  5. External Sorting Algorithms: These algorithms are made to deal with data that doesn't totally fit in memory. They entail breaking the data up into smaller pieces, organizing it in memory, then combining it.
    • External Merge Sort: A common external sorting algorithm used in database systems.
  6. Parallel Sorting Algorithms: Parallel sorting algorithms leverage multiple processors or cores to sort data concurrently, reducing the time required for sorting large datasets.
    • Parallel Merge Sort: A parallel version of Merge Sort.

Working of Sorting Algorithms

Computer science and data processing require sorting algorithms to arrange data items in a particular order. Despite the fact that there are many different sorting algorithms, they all aim to arrange data pieces in a given order, whether that be in ascending or descending order based on specified criteria.

  1. Comparison or Evaluation: Sorting algorithms begin by comparing or evaluating pairs of elements in the dataset. The comparison is typically based on a specific key or attribute of each element
  2. Swapping or Rearranging: When a sorting algorithm identifies that two elements are out of order, it initiates a swap operation. This operation rearranges the positions of the two elements, effectively bringing them into the correct order. The swap operation is essential for reorganizing the data elements and ensuring that they end up in the desired order.
  3. Iteration or Recursion: The comparison and swapping steps are repeated iteratively or recursively until the entire dataset is sorted. The specific number of iterations or passes needed to complete the sorting process depends on the algorithm's design and the initial state of the data. Some algorithms require fewer passes than others to achieve the same result.
  4. Final Result: Once all iterations or recursive calls are completed, the result is a sorted dataset. This dataset is now in the desired order, which could be in ascending or descending order based on the chosen criteria (e.g., numerical value, alphabetical order).

Sorting Algorithms

  1. Bubble Sort:
    • A simple comparison-based sorting algorithm.
    • It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
    • Not efficient for large datasets but easy to understand and implement.
  2. Insertion Sort:
    • Builds the final sorted array one item at a time.
    • It takes each element from the unsorted part and inserts it into its correct position in the sorted part.
    • It's efficient for small datasets or partially sorted data.
  3. Selection Sort:
    • Divides the input into a sorted and an unsorted region.
    • It repeatedly selects the minimum element from the unsorted region and moves it to the sorted region.
    • Simple but not very efficient for large datasets.
  4. Merge Sort:
    • A divide-and-conquer algorithm that divides the unsorted list into n sub lists, each containing one element.
    • It repeatedly merges subsists to produce new sorted sub lists until there is only one sublets remaining.
    • Known for its stability and consistent performance.
  5. Quick Sort:
    • Another divide-and-conquer algorithm.
    • It selects a 'pivot' element from the array and partitions the other elements into two sub-arrays according to whether they are less than or greater than the pivot.
    • Efficient for large datasets and often used in practice.
  6. Heap Sort:
    • Builds a binary heap from the data and repeatedly removes the largest (for max heap) or smallest (for min heap) element.
    • Efficient and has a time complexity of O(n log n) but not a stable sorting algorithm.
  7. Radix Sort:
    • Sorts numbers by processing individual digits.
    • It can be used for integers or strings, where each digit or character is considered at a time.
    • Can be very efficient for certain types of data.
  8. Counting Sort:
    • Suitable for sorting integers within a specified range.
    • It counts the frequency of each element and uses this information to place elements in sorted order.
    • Fast and stable but not suitable for datasets with a wide range of values.
  9. Bucket Sort:
    • Divides the data into a finite number of buckets, each of which is then sorted individually.
    • Suitable for distributed data and can be very efficient when the data is uniformly distributed.
  10. Shell Sort:
    • An extension of insertion sort where elements at a specified interval are compared and swapped.
    • The interval decreases with each pass until the whole array is sorted.
    • Offers a balance between insertion sort and quick sort in terms of efficiency.

Bubble Sort Algorithm in C++

  1. Go down the list starting at the top.
  2. Assess the first two elements. Exchange the first and second elements if the first element is bigger than the second element.
  3. Repeat steps 2 and 3 for the subsequent group of components to cycle over the list of elements.
  4. Repeat steps 2 and 3 until the list is complete.
  5. The largest unsorted entry will have "bubbled up" to the end of the list after being traversed once.
  6. Repetition of steps 1 through 5 will sort the remaining unsorted elements of the list (apart from the final item, which is already in the proper position).
  7. Repeat these steps for each element until no more swaps are required and all the elements pass the merge sort condition, at which point the list will be considered sorted.

Source Code

Merge Sort in C++

  1. Divide the unsorted array into two halves by finding the middle point.
  2. Recursively sort the left half and the right half.
  3. Merge the sorted halves back together to produce a single sorted array.

The key step in Merge Sort is the merging process, where two sorted arrays are combined into one sorted array. This step involves comparing elements from both arrays and placing them in sorted order in a temporary array.

Source Code

Insertion Sort

The straightforward sorting technique known as insertion sort produces the final sorted array one item at a time. In comparison to more sophisticated algorithms like quicksort, heapsort, or merge sort, it performs significantly worse on huge lists. It does, however, have some benefits, such as being quick and easy to use for tiny datasets or lists that have already undergone some sorting.

Source Code

Selection Sort

A straightforward sorting technique called selection sort repeatedly places the least (or highest, depending on the sorting order) element from the array's unsorted portion at the start of the portion that has been sorted. Its time complexity is O(n2), making it unsuitable for huge datasets, although it can be helpful for tiny lists or as a component of more sophisticated sorting algorithms.

  1. Find the minimum (or maximum) element from the unsorted portion of the array.
  2. Swap the minimum (or maximum) element with the first element of the unsorted portion.
  3. Move the boundary between the sorted and unsorted portions one element to the right.
  4. Repeat steps 1-3 until the entire array is sorted.

Quick Sort

Quick sort is a comparison-based, divide-and-conquer sorting algorithm. Choosing one element from the array to act as the "pivot" element and dividing the remaining items into two sub-arrays based on whether they are less than or greater than the pivot element is how it operates. The sub-arrays are next sorted recursively. The array gets sorted completely once this operation is finished. One of the quickest sorting algorithms, quick sort has an average and best-case time complexity of O(n log n). In the worst scenario, it can, nevertheless, become O(n2).

  1. Pick one pivotal component from the array.
  2. Partitioning: Rearrange the array so that all elements that are smaller than the pivot go in front of it and all elements that are larger than the pivot go in back. The pivot is now in the place it was sorted to.
  3. Recursively use Quick Sort on the sub-arrays on the left (which contain elements below the pivot) and the right (which contain entries above the pivot).
  4. Keep doing this until the full array has been sorted.

Source Code

Heap Sort

The binary heap data structure's advantages are used by the comparison-based heap sort method. It repeatedly removes the maximum (for max-heap) or minimum (for min-heap) element from the unsorted region and adds it to the sorted region after dividing the input array into a sorted and an unsorted region. The array gets sorted completely once this operation is finished. Heap sort is an in-place sorting algorithm with an O(n log n) time complexity.

  1. Start by using the input array to generate a binary heap. Each entry at index i has two progenies at positions 2*i + 1 and 2*i + 2, respectively, in a binary heap that is often represented as an array. The binary heap can be either a max-heap or a min-heap depending on whether you want to sort in ascending or descending order.
  2. The element with the highest or lowest value in the unsorted region is found at the root of a max-heap or min-heap, respectively. The last element in the area that isn't sorted should be used in place of this element.
  3. Decrease the size of the heap (exclude the last element) and restore the heap property (heapify the root).
  4. Repeat steps 2 and 3 until the heap is empty. The sorted elements will accumulate at the end of the array, in descending order for max-heap or ascending order for min-heap.

Source Code

Radix Sort

A non-comparative sorting method called radix sort divides objects into buckets based on the specific digits or characters they contain. From least significant (rightmost) to most significant (leftmost), the digits or characters are processed. At each pass, the elements are rearranged into buckets according on the current digit's value. A sorted array is created by concatenating the components back in the order they were placed in the buckets after processing all of the numbers.

  1. To ascertain the number of digits, find the largest number in the array.
  2. From the least significant digit to the most significant digit for each digit position:
  3. Initialize the empty buckets (0-9 for integers with base 10).
  4. Based on the current digit, put each element in the appropriate bucket.
  5. Combine the buckets to create a new array that is sorted.
  6. For each position of the digit, repeat step 2.
  7. Now the array has been sorted.

C++ code

Counting Sort

Counting For sorting objects or numbers with a narrow range of values, sort is an effective, non-comparative sorting method. In order to put the elements in the proper sorted order, it counts the frequency of each unique element in the input data.

  1. Find the min value and max value of the input array.
  2. Create a counting array to store the count of each unique element in the range.
  3. Traverse the input array and increment the count for each element.
  4. Calculate the cumulative sum of counts in the counting array. This step helps determine the correct position for each element in the sorted output.
  5. Initialize an output array to store the sorted elements.
  6. Traverse the input array in reverse order (to maintain stability) and place each element in its correct position in the output array based on the cumulative count.
  7. The output array now contains the sorted elements.

C++ code

Bucket Sort

Bucket sort is a sorting algorithm that works by dividing the input into a fixed number of equally spaced "buckets," distributing the elements into these buckets, and then sorting each bucket individually, typically using another sorting algorithm like insertion sort or quicksort. You concatenate all the sorted buckets after sorting each one to get the final sorted array.

  1. Decide on the program's bucket count and the range of input values that will be accepted.
  2. Create a set of empty buckets in an array to represent each value in the range.
  3. Iterate through the input array, assigning each entry to the appropriate bucket using a mapping function. Using the mapping function, values should be spread equally among the buckets.
  4. Sort each bucket individually, either using another sorting algorithm or recursively applying the bucket sort algorithm if the bucket size is large enough.
  5. Concatenate all the sorted buckets to obtain the final sorted array.

Source Code

Shell Sort

Shell Sort is an efficient, in-place, and unstable sorting algorithm that's an extension of the Insertion Sort algorithm. It works by repeatedly sorting elements that are distant from each other, gradually reducing the gap between elements to be compared until it becomes 1. At this point, the algorithm becomes equivalent to a simple Insertion Sort, but due to the prior sorting passes, the array is partially sorted, which can significantly improve the Insertion Sort's performance.

  1. Choose a sequence of gap values (often referred to as the "increment sequence") that starts with a relatively large gap and progressively reduces it. Common sequences include the Knuth sequence (3k+1) or simply dividing the gap by 2 in each pass.
  2. Starting with the largest gap, repeatedly perform Insertion Sort on subarrays of the original array, where the elements in each subarray are separated by the chosen gap value.
  3. Decrease the gap gradually and repeat step 2 until the gap becomes equal to 1, which is equivalent to a final Insertion Sort pass on the entire array.

Source Code

Application of Sorting Algorithms

  1. Data Retrieval: Sorting algorithms are used to efficiently retrieve data from databases and file systems. For example, databases often use sorting to quickly locate records with specific criteria.
  2. Search Algorithms: Many search algorithms, such as binary search, call for the sorting of data prior to the search. Finding a specific item in a list fast is made possible by sorting.
  3. Arranging Results: Search results that are requested by the end-user, items, or postings are arranged using sorting algorithms in software applications like search engines like Google, e-commerce websites, and social media platforms based on relevance, user preferences, or other factors like whether the products are sponsored.
  4. Data Analysis: Data and statistics can be sorted to look for patterns, outliers, and trends. Data can exhibit temporal trends by being grouped according to time, for instance.
  5. File and Directory Listing: File systems use sorting algorithms to display files and directories in an ordered manner, making it easier for users to find and manage their files.
  6. Task Scheduling: Scheduling algorithms often require sorting tasks or processes based on priority, deadline, or other criteria.
  7. Optimization Problems: Some optimization problems involve sorting as an intermediate step. For example, the traveling salesman problem can benefit from sorting cities based on their proximity.
  8. Graph Algorithms: In graph algorithms like Kruskal's algorithm for minimum spanning trees, sorting edges based on their weights is a critical step.
  9. Image Processing: Image processing applications may use sorting to order pixels based on intensity, color, or other attributes.
  10. Natural Language Processing (NLP): In NLP, sorting may be used to order documents or sentences by relevance or other criteria.
  11. Genetics: Genetic algorithms may involve sorting individuals in a population based on fitness or other genetic characteristics.
  12. Computer Graphics: Rendering and animation applications sometimes use sorting to determine the order in which objects are drawn, taking into account their depth or transparency.
  13. Load Balancing: In distributed systems and cloud computing, load balancing algorithms may sort tasks or data to distribute the load evenly among resources.
  14. Routing and Navigation: In GPS and map applications, sorting algorithms may be used to order waypoints or routes based on distance or time.
  15. Collaborative Filtering: Recommender systems often use sorting to rank and recommend items to users based on their preferences and behavior.
  16. Financial and Stock Market Analysis: In order to find trends and make investment decisions, financial data, such as stock prices, are sorted using algorithms.
  17. Games and simulations can utilize sorting to arrange objects, characters, or events according to numerous criteria, such as proximity, rapidity, or significance.

Next TopicCalloc in C++

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA