Sorting Algorithms
Sorting Algorithms
Insertion Sort
- A straightforward sorting algorithm called insertion sort functions in a manner similar to
how you might arrange playing cards in your hands. In a sense, the array is divided into
sorted and unsorted parts. Values are chosen and assigned to the appropriate positions in
the sorted part of the data from the unsorted part. Because of its low overhead, insertion
sort is quick and most effective when the problem size is small or when the data is nearly
sorted (because it is adaptive).
-
2. Selection Sort
- The selection sort method sorts an array by repeatedly selecting the first element from
the unsorted portion that is the least in value while still taking into account ascending
order. In a given array, the method keeps two subarrays:
The smallest element (assuming ascending order) from the unsorted subarray is
selected and moved to the sorted subarray in each iteration or pass of the selection
sort. The property of the selection sort is to minimize the number of swaps. As a
result, it is the ideal option when exchanging is expensive.
3. Bubble Sort
- When using the sorting method Bubble Sort, adjacent elements that are in the wrong
order are frequently switched. The greatest element (in the case of ascending order) or
the smallest element (in the case of passing) reaches the finish after each iteration or
pass (in the case of descending order). Up until the list is sorted, the trip through the
list is repeated. This algorithm's average and worst-case complexity, where n is the
number of items, is O(n^2), making it unsuitable for huge data sets.
4. Merge Sort
- This sorting algorithm, in contrast to the first three, is based on the divide-and-
conquer strategy. The input array is split in half, calls are made for each half, and the
two sorted halves are then combined. The merge() function, which is used to combine
two halves, is the brain of the merge sort. The merge(A, p, q, r) key procedure merges
the two sorted sub-arrays into one, presuming that A[p..q] and A[q+1..r] are sorted.
When you require a stable and O(N log N) sort, merge sort is your only choice.
Merge () function
- Another Divide and Conquer algorithm is Quick Sort. It chooses one element to act as
the pivot and divides the supplied array around it, placing all of the smaller
components to the left of the pivot and all of the larger elements to the right. There
are other variations of quicksort that select pivots in various ways:
The partition() method in quicksort is the main operation. The goal of partitioning is
to sort an array, using element r as the pivot, and place all elements that are smaller
than r before r and all elements that are greater than r after r in the correct order. This
should all be completed in a straight line.
In comparison to the merge sort, the quicksort is the best method for small inputs.
Choose quicksort if you don't want a steady sort and average performance is more
important than worst performance. Let's first look at the partition algorithm and how
it is implemented.
Partition () algorithm
The index of components that are less (or equal to) the starting element on the right is
kept track of as r.