Sorting Techniques in Data StructuresIntroduction:Sorting is a fundamental computer science operation that entails putting a group of objects in a specific order. It is extensively utilised in many different applications, including database management, data analysis, and searching. In data structures, different sorting techniques are employed to organize and manipulate large sets of data efficiently. Sorting Techniques:There are various sorting techniques, which are listed in the table below:
Bubble Sort:The straightforward sorting method known as "Bubble Sort" analyses neighbouring elements, iteratively goes over the list, and swaps out any elements that are out of order. To sort the complete list, repeat this step. Example: Let's consider an array of integers: {5, 2, 8, 12, 3}.
Program:Explanation: The bubbleSort function takes an array (arr) and its size (size) as input. It iterates across the array and compares nearby elements using nested loops. A swap is carried out if the current element is larger than the following element. Until the largest element "bubbles" to the end of the array, this process is repeated. An array named arr is initialised with some values in the main function, and the size of the array is determined using the sizeof operator. The bubbleSort function is then called, passing the array and its size. Program Output: Selection Sort:Selection Sort is a sorting algorithm that uses in-place comparisons. It separates the input array into two sections, one that is sorted and the other that is not. The algorithm chooses the least (or maximum) element from the unsorted part of the portion and inserts it at the start of the sorted part of the portion in each iteration. Example: Consider the same array of integers: {5, 2, 8, 12, 3}.
Program:Explanation: The selectionSort function takes an array (arr) and its size (size) as input. It uses nested loops to iterate through the array. The inner loop searches the remaining unsorted portion of the array for the minimum element while the outer loop chooses the current member. When the inner loop is finished, the outer loop's current element is switched out for the minimum element. By doing this, it is ensured that the minimal element is shifted to the proper position in the sort. The size of the array is determined by using the sizeof operator once an array named arr is initialised in the main function with some data. The selectionSort function is then called, passing the array and its size. Program Output: Insertion Sort:Insertion Sort is a straightforward sorting algorithm that creates the finished sorted array one item at a time. One element at a time, iteratively taking into account the input array, it inserts each element into the sorted portion of the array in the proper place. Example: Let's use the same array of integers: {5, 2, 8, 12, 3}.
Program:Explanation: The insertionSort function takes an array arr and its size size as input. It begins comparing with the elements in the array's sorted portion with the second element (i = 1). It then iterates backward (j = i - 1) through the sorted part of the array, shifting the elements to the right if they are greater than the key value. The inner while loop continues until either j becomes negative or the element at index j is not greater than key. This loop shifts the elements to the right to make space for the key value in its correct sorted position. After the while loop, the key value is inserted into the sorted position (arr[j + 1] = key). The array's elements are processed one at a time until all of them have been. The sizeof operator is used in the main function to determine the array's size after initialising an array called arr with certain values.The insertionSort function is then called, passing the array and its size. Program Output: Merge Sort:Merge Sort is a divide-and-conquer algorithm that divides the input array into smaller subarrays, sorts them, and then combines them to produce a sorted output. It uses a "merge" operation to combine the smaller sorted subarrays into a larger sorted array. Example: Consider the array of integers: {5, 2, 8, 12, 3}.
Program:Explanation: The mergeSort function recursively divides the array into smaller subarrays until each The array is split into smaller subarrays by the mergeSort function repeatedly until each subarray contains just one element. The subarrays are then combined by comparing and combining their components in the order they were sorted. Two sorted subarrays are combined into a single sorted array using the merge function. It compares the components of the two subarrays and arranges them in the original array's proper sequence. An array is initialised with some values in the main method, and the mergeSort function is then used to sort the array. The sorted array is printed lastly. Program output: Quick Sort:Another divide-and-conquer technique that divides the array based on a pivot element is Quick Sort. It chooses a pivot and rearranges the array of components so that those closer to the pivot are placed first, while those farther away are placed last. The subarrays are subjected to this process again and again until the full array is sorted. Example: Using the array of integers: {5, 2, 8, 12, 3}. The algorithm selects a pivot element, which can be chosen in various ways (e.g., the last element in the array).
Program:Explanation: The partition function chooses the last element in the array as the pivot and rearranges the array's contents so that all items that are less than the pivot are moved to the left and all elements that are larger than the pivot are moved to the right. It provides the pivot element's index as a response. The quickSort function recursively applies the quicksort algorithm by selecting a pivot, partitioning the array, and recursively sorting the subarrays. It stops when the subarray size is 1 or less. An array is initialised in the main function, and then it is sorted using the quickSort function. The sorted array is then printed. Program Output: Heap Sort:Heap sort is a comparison-based sorting algorithm that uses the heap data structure to efficiently sort elements in ascending or descending order. It works by transforming an array into a binary heap and repeatedly extracting the maximum (in case of ascending order) or minimum (in case of descending order) element from the heap until the array is fully sorted. Here's an explanation of the heap sort algorithm using a good example: Consider an array of numbers: [9, 7, 5, 2, 1, 8, 6, 3, 4]. 1. Build a max heap: We need to create a binary heap first from the supplied array. Starting from the last non-leaf node (n/2-1), we heapify the array so that the maximum element is always at the root. In this case, the last non-leaf node is at index 4. [9, 7, 5, 2, 1, 8, 6, 3, 4] (Original array) [9, 7, 6, 4, 1, 8, 5, 3, 2] (After heapifying) 2. Swap the root with the last element: Replace the root, which is the initial element, with the heap's last element. In this case, swap index 0 with index 8. [2, 7, 6, 4, 1, 8, 5, 3, 9] (After swapping) 3. Reduce the heap size: Reduce the heap size by 1 and heapify the root element (which is now at index 0). This will move the next maximum element to the root. [8, 7, 6, 4, 1, 2, 5, 3, 9] (After heapifying) 4. Repeat steps 2 and 3: Until the heap size is 1, repeat steps 2 and 3 as necessary. The maximum element will be shifted to the end of the array after each iteration. [7, 4, 6, 3, 1, 2, 5, 8, 9] [6, 4, 5, 3, 1, 2, 7, 8, 9] [5, 4, 2, 3, 1, 6, 7, 8, 9] [4, 3, 2, 1, 5, 6, 7, 8, 9] [3, 1, 2, 4, 5, 6, 7, 8, 9] [2, 1, 3, 4, 5, 6, 7, 8, 9] [1, 2, 3, 4, 5, 6, 7, 8, 9] The array is now sorted in ascending order. Program:Explanation: The heapify function compares the node with its left and right children and swaps it with the largest child if necessary, ensuring the max heap property. The heapSort function builds a max heap from the array and then repeatedly extracts the maximum element by swapping it with the last element, reducing the heap size, and calling heapify to maintain the max heap property. An array is initialised in the main function, and heapSort is used to sort the array. The array after sorting is then printed. Program Output: Counting sortA linear sorting method called counting sort arranges elements according to their count or frequency. It works well for a range of non-negative integers with a known maximum value. Counting sort doesn't include any comparison operations, it is quicker than many other sorting algorithms. Example Let's consider an array of integers as our input: [4, 2, 5, 1, 3, 4, 2, 4]. We assume that the range of input values is from 1 to 5.
ProgramExplanation: We Find the maximum element in the array to determine the range of elements. Create a counting array after that, initialise all of its items to 0, and give it a size of (max + 1). The counting array should go through the input array, increasing the count of each entry. Then To store the total number of entries, update the counting array. The number of elements less than or equal to an element's index is represented by that element. A temporary array of the same size as the input array should be created. Reverse the order of the input array traversal. Using the counting array, place each element in the temporary array where it belongs. To create a sorted array, copy the entries from the temporary array back into the input array. Program Output: Radix sortRadix sort is a non-comparison-based sorting algorithm that arranges objects according to their digits or bits. It works by grouping elements by each significant digit or bit and repeatedly distributing them into different buckets. Radix sort can be applied to integers, strings, or other data types if they can be represented as a sequence of digits or bits. Example Let's consider an array of integers as input: [170, 45, 75, 90, 802, 24, 2, 66]. We will use a decimal radix sort, which sorts elements based on their digits from least significant to most significant.
Program:Explanation: The getMax function finds the maximum element in the array to determine the maximum number of digits. The countingSort function performs counting sort based on a specific digit (determined by exp) using a counting array. It calculates the count of each digit and then determines the positions of the digits in the output array. From the least significant digit to the most significant digit, the radixSort function executes a counting sort for each digit position. The items of the array are printed by the printArray function. In the main function, an array is initialized, and its original contents are printed. The array is sorted using radix sort by calling the radixSort function. The sorted array is printed lastly. Program Output: Bucket sortBucket sort is a sorting algorithm that separates the input elements into various "buckets" based on their values and then sorts the elements within each bucket separately. It works best when the input numbers are evenly spread across a range. Bucket sort can be performed in two steps: distribution and collection. The distribution step places elements into their corresponding buckets, and the collection step merges the elements from the buckets to obtain the sorted array. Example
Program:Explanation: Create an array of empty buckets, where each bucket represents a range of values. Put each element in the input array into the appropriate bucket according to its value. The bucket index is determined by multiplying the element by the number of buckets. Within each bucket, sort the elements. The elements. are sorted in this code using the std::sort function. Gather the elements. in the proper sequence from the buckets. After going over each bucket iteratively, add the output array's sorted elements. The main function initializes an input array of floating-point numbers. Finally, the sorted array is printed. Program Output: |