Visual Studio
A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order.
There exist different sorting algorithms for different different types of inputs, for example a binary array, a character array, an array with a large range of values or an array with many duplicates or a small vs large array. The algorithms may also differ according to output requirements. For example, stable sorting (or maintains original order of equal elements) or not stable. Sorting is provided in library implementation of most of the programming languages. These sorting functions typically are general purpose functions with flexibility of providing the expected sorting order (increasing or decreasing or by a specific key in case of objects).
Selection Sort: Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.
- First we find the smallest element and swap it with the first element. This way we get the smallest element at its correct position.
- Then we find the smallest among remaining elements (or second smallest) and swap it with the second element.
- We keep doing this until we get all elements moved to correct position.
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 data sets as its average and worst-case time complexity are quite high.
We sort the array using multiple passes. After the first pass, the maximum element goes to end (its correct position). Same way, after second pass, the second largest element goes to second last position and so on. In every pass, we process only those elements that have already not moved to correct position. After k passes, the largest k elements must have been moved to the last k positions. In a pass, we consider remaining elements and compare all adjacent and swap if larger element is before a smaller element. If we keep doing this, we get the largest (among the remaining elements) at its correct position.
QuickSort:
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array.
It works on the principle of divide and conquer, breaking down the problem into smaller sub-problems.
There are mainly three steps in the algorithm:
Choose a Pivot: Select an element from the array as the pivot. The choice of pivot can vary (e.g., first element, last element, random element, or median). Partition the Array: Re arrange the array around the pivot. After partitioning, all elements smaller than the pivot will be on its left, and all elements greater than the pivot will be on its right. The pivot is then in its correct position, and we obtain the index of the pivot. Recursively Call: Recursively apply the same process to the two partitioned sub-arrays (left and right of the pivot). Base Case: The recursion stops when there is only one element left in the sub-array, as a single element is already sorted.
The concept of sorting is explained by the following codes,
- Selection sort
- Bubble sort
- QuickSort
Algorithm: Sorting and Searching in an Array
- Start
- Define a class
bubleSort
with: Functionsort(std::vector<int>& arr)
- Find the size
n
of the array. - Repeat for
i = 0
ton-2
: For eachj = 0
ton-i-2
: Ifarr[j] > arr[j+1]
, swap them.
- Find the size
- Define a class
SearchArr
with: Functionbinarysearch(const std::vector<int>& arr, int target)
- Initialize
low = 0
,high = n-1
. - While
low <= high
:- Compute
mid = low + (high - low)/2
. - If
arr[mid] == target
, returnmid
. - If
arr[mid] < target
, setlow = mid + 1
. - Else set
high = mid - 1
.
- Compute
- If element is not found, return
-1
.
- Initialize
- In
main()
:- Create objects
sorter
(for sorting) ands
(for searching). - Initialize an array
arr = {-1, 0, 3, 5, 9, 12}
. - Call
sorter.sort(arr)
to sort the array. - Print the sorted array.
- Define
target = 9
. - Call
s.binarysearch(arr, target)
. - If result ≠ -1 → print index of element. Else → print "Element not found".
- Create objects
- End
Algorithm: Quick Sort
-
Start
-
Partition function (
partition(arr, low, high)
): Choose the last element as pivot (arr[high]
). Initialize indexi = low - 1
. For eachj = low
tohigh - 1
:If `arr[j] < pivot`: Increment `i`. Swap `arr[i]` and `arr[j]`.
After loop, swap
arr[i+1]
andarr[high]
. Returni+1
(partition index). -
Quick Sort function (
quickSort(arr, low, high)
): Iflow < high
:- Call
partition(arr, low, high)
to get pivot indexpi
. - Recursively call
quickSort(arr, low, pi - 1)
. - Recursively call
quickSort(arr, pi + 1, high)
.
- Call
-
Main function:
Initialize an array
arr = {10,7,8,9,1,5}
. CallquickSort(arr, 0, n-1)
. Print the sorted array. -
End
Algorithm: Selection Sort
- Start
- Take an unsorted array of size
n
. - Repeat the following for
i = 0
ton-2
: Assume the element at indexi
is the minimum (min_idx = i
). Compare this element with all elements to its right (j = i+1
ton-1
). If any elementarr[j]
is smaller thanarr[min_idx]
, updatemin_idx = j
. After the inner loop ends, swaparr[i]
andarr[min_idx]
. - Continue until the whole array is sorted.
- Print the sorted array.
- End
The above codes demonstrates the various techniques to sort a given arrray in C++