多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
###JAVA版 ~~~ public class Merge { //合并有序数组 public static void mergeSort(int a[], int b[], int c[]) { int n = a.length, m = b.length; int i, j, k; i = j = k = 0; while (i < n && j < m) { if (a[i] < b[j]) { c[k++] = a[i++]; } else { c[k++] = b[j++]; } } while (i < n) c[k++] = a[i++]; while (j < m) c[k++] = b[j++]; } //打印数组中的元素 public static void printArr(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + "\t"); } } public static void main(String[] args) { System.out.println("Hello World!"); int[] a = new int[] { 1, 2, 5, 6 }; int[] b = new int[] { 3, 8, 9, 10 }; int c[] = new int[8]; mergeSort(a, b, c); printArr(c); } } 输出结果: 1 2 3 5 6 8 9 10 ~~~ ###C语言版 ~~~ #include <stdio.h> // 打印数组a void printArr(int a[],int n){ for (int i = 0; i < n; ++i) { printf("%d\t",a[i]); } printf("\n"); } //合并有序数组 void mergeArray(int a[],int n,int b[],int m,int c[]){ int i, j, k; i = j = k = 0; while (i <n && j<m) { if (a[i] < b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; } while (i < n) c[k++] = a[i++]; while (j < m) c[k++] = b[j++]; } int main(){ int a[3]={2,3,6}; int b[2]={1,5}; int c[5]={}; mergeArray(a,3,b,2,c); printArr(c,5); } 输出结果: yaopans-MacBook-Pro:algorithm yaopan$ ./a.out 1 2 3 5 6 ~~~