解题思路:
注意事项:
参考代码:
#include <stdio.h> #include <iostream> #include <memory.h> #include <cstring> #include <string> using namespace std; void merge(int a[], int l, int mid, int r) { int p1 = l, p2 = mid + 1, i = 0, len = r - l + 1; int *ax = new int[len](); while (p1 <= mid && p2 <= r) ax[i++] = a[p1] < a[p2] ? a[p1++] : a[p2++]; while (p1 <= mid) ax[i++] = a[p1++]; while (p2 <= r) ax[i++] = a[p2++]; for (i = 0; i < len; i++) a[l + i] = ax[i]; // 坑 "l + i" delete ax; return; } void mergeSort(int a[], int l, int r) { if (l == r) return; int mid = l + ((r - l) >> 1); mergeSort(a, l, mid); mergeSort(a, mid + 1, r); merge(a, l, mid, r); } int main(void) { int n = 0; cin >> n; int *a = new int[n](); for (int i = 0; i < n; i++) cin >> a[i]; mergeSort(a, 0, n -1); for (int i = 0; i < n; i++) cout << a[i] << " "; delete a; return 0; }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:635 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:1073 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:592 |
printf基础练习2 (有点不明白)浏览:888 |
WU-蓝桥杯算法提高VIP-交换Easy (C++代码)浏览:1186 |
【金明的预算方案】 (C++代码)浏览:873 |
星期判断机 (C语言代码)浏览:896 |
IP判断 (C语言代码)浏览:593 |
众数问题 (C语言代码)浏览:717 |
C语言程序设计教程(第三版)课后习题9.3 (C语言代码)浏览:630 |