What are the temporal and spatial complexities of common sorting algorithms?
As shown in Fig
Start by taking a number from the sequence as a "baseline".
Partitioning process: Put all the numbers larger than the "baseline" to the right of the "baseline", and all the numbers less than or equal to the "baseline" to the left of the "baseline".
Repeat the second step for the left and right intervals until there is only one number for each interval.
var quicksort = function(arr) var pivotindex = math.floor(arr.length / 2);Datum position (theoretically arbitrary) var pivot = arrsplice(pivotindex, 1)[0];Benchmark number var left = var right = for (var i = 0; i < arr.length; i++)else }return quicksort(left).concat([pivot], quicksort(right));link left array, array composed of base number, right array};First, find the smallest (largest) element in the unsorted sequence and store it at the beginning of the sorted sequence.
Then continue to look for the smallest (large) element from the remaining unsorted elements, and then put it at the end of the sorted sequence.
Repeat the second step until all the elements are sorted.
function selectionsort(arr) temp = arr[i]; arr[i] = arr[minindex]; arr[minindex] = temp; }return arr;}Treat the first element of the first sequence to be sorted as an ordered sequence, and the second element to the last element as an unsorted sequence.
Scan the unsorted sequence from start to finish, inserting each element that is scanned into the appropriate position of the ordered sequence. (If the element to be inserted is equal to one of the elements in the sequence, the element to be inserted is inserted after the equal element.) )
function insertionsort(arr) arr[preindex+1] = current; }return arr;}Compare adjacent elements. If the first one is bigger than the second, swap both of them.
Do the same work for each pair of adjacent elements, starting with the first pair and ending with the last pair. After this step is done, the final element will be the largest number.
Repeat for all elements, except for the last one.
Repeat the above steps for fewer and fewer elements each time until there are no pairs of numbers to compare.
function bubblesort(arr) return arr;}In 1959, the shell was invented, the first sorting algorithm that broke through o(n2), and was an improved version of simple insertion sorting. It differs from insertion sort in that it prioritizes comparing elements that are further away. Hill sort is also known as shrinking delta sorting.
First, the entire record sequence to be sorted is divided into several sub-sequences for direct insertion and sorting, and the specific algorithm is described:
Select an incremental sequence t1,t2,..., tk, where ti>tj, tk=1;
Sort the sequences k times according to the number of incremental sequences k;
For each sorting, according to the corresponding incremental ti, the sequence to be sorted is divided into several sub-sequences of length m, and each sub-table is directly inserted and sorted.
When the increment factor is 1 only, the entire sequence is treated as a table, and the table length is the length of the entire sequence.
function shellsort(arr) for (gap; gap > 0; gap = math.floor(gap / 3)) arr[j + gap] = temp; }return arr;}Go straight to the **.
function mergesort(arr)function merge(left, right) else } //remaining part needs to be addred to the result return result.concat(left.slice(l)).concat(right.slice(r));