标签:复杂 end 语言 复杂度 时间复杂度 rand and log 归并排序
利用递归实现分治。每次排序时间复杂度是O(N),一共需要 LogN 次。
#include <stdlib.h> #include <time.h> #define size 100 static int n[size]; static int t[size]; void msort(int n[], int start, int end) { int restart = start; int left = start; int leftend = (start + end) / 2; int right = leftend + 1; int rightend = end; if(left != leftend) msort(n, left, leftend); if(right != rightend) msort(n, right, rightend); int i = left; while(left <= leftend && right <= rightend) { if(n[left] < n[right]) t[i++] = n[left++]; else t[i++] = n[right++]; } while(left <= leftend) t[i++] = n[left++]; while(right <= rightend) t[i++] = n[right++]; while(restart <= end) { n[restart] = t[restart]; restart++; } } int main() { time_t seed; seed = time(0); srandom(seed); for(int i = 0; i < size; i++) { n[i] = random()%1000; } msort(n, 0, size - 1); for(int i = 0; i < size; i++) printf("%d ",n[i]); printf("\n"); return 0; }
标签:复杂 end 语言 复杂度 时间复杂度 rand and log 归并排序
原文地址:https://www.cnblogs.com/sau-autumnwind/p/14193913.html