sorting algo
sorting algo
Selection Sort divides the input list into two parts: the sublist of items
already sorted and the sublist of items remaining to be sorted. It repeatedly
selects the smallest (or largest) element from the unsorted sublist and
moves it to the sorted sublist.
Insertion Sort builds the final sorted array one item at a time. It is much
less efficient on large lists than more advanced algorithms such as
quicksort, heapsort, or merge sort.
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
if (largest != i) {
std::swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
Counting Sort is an integer sorting algorithm that assumes the range of the
numbers to be sorted is known. It counts the number of objects having
distinct key values, and using arithmetic to calculate the position of each
object.
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
int minE = *min_element(begin(nums), end(nums));
int maxE = *max_element(begin(nums), end(nums));
int i = 0;
return nums;
}
};