Sorting algorithm merge sort and its time complexity and space complexity
The efficiency of quick sort in the sorting algorithm is very high, but there is another sorting algorithm that can be comparable in efficiency, that is merge sort; merge sort and quick sort have the same effect. The magic, quick sort: first roughly sort the array into two sub-arrays, and then roughly divide the two sub-arrays recursively, until there is only one element in the sub-array, then it will be sorted naturally, which can be summarized as sorting first and then recursive ; Merge sort: No matter what, divide the array into two sub-arrays, and divide the array into two sub-arrays recursively until there is only one element in the array, then sorting starts, so that the two arrays are sorted in sequence, Sort the two arrays according to the recursive return, and sort the entire array at the end;
#include<stdio.h>
#define LEN 12 // Macro defines the size of the array
static int tmp[LEN] = {0};// Set temporary array
// print array
void print_array(int *array)
{
int index = 0;
printf("\narray:\n");
for (; index < LEN; index++){
printf(" %d, ", *(array + index));
}
printf("\n");
}
// Sort two ordered arrays into one array
void _mergeSort(int *array, int start, int middle, int end)
{
int first = start;
int second = middle + 1;
int index = start;
while ((first <= middle) && (second <= end)){
if (array[first] >= array[second])
tmp[index++] = array[second++];
else
tmp[index++] = array[first++];
}
while(first <= middle) tmp[index++] = array[first++];
while(second <= end) tmp[index++] = array[second++];
for (first = start; first <= end; first++)
array[first] = tmp[first];
}
// Recursively divide the array
void mergeSort(int *array, int start, int end)
{
if (start >= end)
return;
int middle = ((end + start) >> 1);
mergeSort(array, start, middle);// Recursively divide the left array
mergeSort(array, middle+1, end);// Recursively divide the array on the right
_mergeSort(array, start, middle, end);// Combine two ordered arrays into an ordered array
}
int main(void)
{
int array[LEN] = {2, 1, 4, 0, 12, 520, 2, 9, 5, 3, 13, 14};
print_array(array);
mergeSort(array, 0, LEN-1);
print_array(array);
return 0;
}
When the sorting of the records in the sequence to be sorted is in increasing order (that is, the orderly records from small to large, called positive order), reachThe smallest number of comparisons: ...
/* * Heap sort * A function to build a heap, a function to sort * heap initially builds a heap (large root heap), obtains the largest node among left and right children, and exchang...
Bubble sorting and performance optimization of sorting algorithm (time complexity + space complexity analysis) Sorting algorithm simple selection sorting and time complexity analysis Sorting algorithm...
1. Time complexity: under normal circumstances, in the algorithmBasic operationThe number of repeated executions is a function f(n) of the problem size n, and the time measure of the algorithm is writ...
Time Complexity time complexity The first thing to say is that the calculation of time complexity isIt is not the time when the program is run, but the number of times the algorithm executes the state...
What is an algorithm? ——In order to solve a certain problem and prescribed finite series of operations。 Five characteristics of the algorithm &nb...
Efficiency of the algorithm is divided into two: the first is time efficiency, the second is space efficiency. Time efficiency called time complexity; referred to as spatial complexity of the space ef...
Usually used less, often forgotten, the concept of time complexity is recorded here Complexity is a method of marking algorithms. Represented by O, usually read as big O O contains time complexity and...
1. Time frequency Time frequency is the time it takes for an algorithm to execute. It cannot be calculated theoretically, and it can only be known by running tests on the computer. But it is impossibl...