Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 组合和排序两个数组?_Java_Arrays_Sorting - Fatal编程技术网

Java 组合和排序两个数组?

Java 组合和排序两个数组?,java,arrays,sorting,Java,Arrays,Sorting,因此,基本上有两个单独的预排序数组,您必须将它们组合起来并对它们进行排序(当然没有sort()方法)。这是我的密码: public static void main(String[] args) { int a [] = {3,5,7,9,12,14, 15}; int b [] = {6 ,7, 10}; int j = 0; //output array should be 3,5,6,7,7,9,10,12,14,15 int c [] = n

因此,基本上有两个单独的预排序数组,您必须将它们组合起来并对它们进行排序(当然没有sort()方法)。这是我的密码:

public static void main(String[] args) {

    int a [] = {3,5,7,9,12,14, 15};
    int b [] = {6 ,7, 10};
    int j = 0;

    //output array should be 3,5,6,7,7,9,10,12,14,15

    int c [] = new int[a.length+b.length];//10 values

    for (int i = 0;i<b.length;i++){
        while(b[i]>a[j]){
            c[j] = a[j] ;
            j++;    
         }

        if(b[i] == a[j]){
            c[j] = b[i];
            c[j+1] = a[j];
        }

        c[j] = b[i];
        j++;
    }

    for(int i = 0;i<c.length;i++)
        System.out.println(c[i]);
    }
publicstaticvoidmain(字符串[]args){
int a[]={3,5,7,9,12,14,15};
int b[]={6,7,10};
int j=0;
//输出阵列应为3,5,6,7,7,9,10,12,14,15
int c[]=新的int[a.length+b.length];//10个值
对于(int i=0;ia[j]){
c[j]=a[j];
j++;
}
如果(b[i]==a[j]){
c[j]=b[i];
c[j+1]=a[j];
}
c[j]=b[i];
j++;
}

对于(inti=0;i实际上最好说合并(而不是合并)两个数组

将排序数组AB[0..n-1]合并为结果C[0..m+n-1]的简单算法(取自此):

  • 引入读索引ij来遍历数组A[0..m-1]B。引入写索引k来存储结果数组中第一个空闲单元的位置。默认情况下i=j=k=0
  • 在每个步骤中:如果两个索引都在范围内(imjn),则选择最小值(A[i]B[j])并将其写入C[k]。否则,进入步骤4
  • 将位于最小值处的数组的k和索引增加1。重复步骤2
  • 将索引仍在范围内的数组中的其余值复制到结果数组中

  • 希望有帮助。

    试试这个,你的错误是你对数组A和数组C使用了相同的蜂窝索引:

    public class MainClass {
          public static void main(String[] args) {
            int[] arrayA = { 23, 47, 81, 95 };
            int[] arrayB = { 7, 14, 39, 55, 62, 74 };
            int[] arrayC = new int[10];
    
            merge(arrayA, arrayA.length, arrayB, arrayB.length, arrayC);
            for (int i : arrayC) {
              System.out.println(i);
    
            }
          }
    
          public static void merge(int[] arrayA, int sizeA, int[] arrayB, int sizeB, int[] arrayC) {
            int arrayAIndex = 0, arrayBIndex = 0, arrayCIndex = 0;
    
            while (arrayAIndex < sizeA && arrayBIndex < sizeB)
              if (arrayA[arrayAIndex] < arrayB[arrayBIndex])
                arrayC[arrayCIndex++] = arrayA[arrayAIndex++];
              else
                arrayC[arrayCIndex++] = arrayB[arrayBIndex++];
    
            while (arrayAIndex < sizeA)
              arrayC[arrayCIndex++] = arrayA[arrayAIndex++];
    
            while (arrayBIndex < sizeB)
              arrayC[arrayCIndex++] = arrayB[arrayBIndex++];
          }
        }
    
    public类MainClass{
    公共静态void main(字符串[]args){
    int[]arrayA={23,47,81,95};
    int[]arrayB={7,14,39,55,62,74};
    int[]arrayC=新int[10];
    合并(arrayA,arrayA.length,arrayB,arrayB.length,arrayC);
    for(int i:arrayC){
    系统输出打印LN(i);
    }
    }
    公共静态无效合并(int[]arrayA、int-sizeA、int[]arrayB、int-sizeB、int[]arrayC){
    int-ArrayIndex=0,ArrayIndex=0,ArrayIndex=0;
    而(ArrayIndex
    这是另一个版本

    // size of C array must be equal or greater than
    // sum of A and B arrays' sizes
    public void merge(int[] A, int[] B, int[] C) {
          int i, j, k, m, n;
          i = 0;
          j = 0;
          k = 0;
          m = A.length;
          n = B.length;
          while (i < m && j < n) {
                if (A[i] <= B[j]) {
                      C[k] = A[i];
                      i++;
                } else {
                      C[k] = B[j];
                      j++;
                }
                k++;
          }
          if (i < m) {
                for (int p = i; p < m; p++) {
                      C[k] = A[p];
                      k++;
                }
          } else {
                for (int p = j; p < n; p++) {
                      C[k] = B[p];
                      k++;
                }
          }
    }
    
    //C数组的大小必须等于或大于
    //A和B数组大小之和
    公共无效合并(int[]A、int[]B、int[]C){
    int i,j,k,m,n;
    i=0;
    j=0;
    k=0;
    m=A.长度;
    n=B.长度;
    而(i如果(A[i]使用
    ai
    bi
    作为源数组的索引,使用
    ci
    作为目标数组的索引

    你只需要一个循环

    尽量保持这一点非常清楚,并在每次迭代中通过一个元素在
    c
    中前进

    在循环中,检查是否到达了一个数组的末尾。如果是,只从另一个数组中获取一个元素。否则,只获取
    a[ai]
    b[bi]
    中较小的元素,并增加相应的索引

    在mergesort(或任何需要并行遍历两个数组的代码)中,如果认为“嘿,我可以使用while循环,而不只是执行单个if”,则很容易出错,但通常会有两个循环嵌套在第三个循环中,对于每个循环,必须进行正确的边界检查,并且通常不会有显著的性能提升


    p、 s.在主循环之后执行一个主循环,然后执行两个清理循环是可以的,如果没有必要,只需避免嵌套循环,特别是在访谈中,这可能会在计算运行时时造成混乱。

    这是一种简单的方式:

    public static void main(String[] args) {
    
        int a [] = {3,5,7,9,12,14, 15};
        int b [] = {6 ,7, 10};
        int j = 0, k = 0;
    
        //output array should be 3,5,6,7,7,9,10,12,14,15
    
        int c [] = new int[a.length+b.length];//10 values
    
        // we're filling c with the next appropriate number
        // we start with checking a[0] and b[0] till we add
        // all the elements
        for (int i = 0; i < c.length; i++) {
            // if both "a" and "b" have elements left to check
            if (j < a.length && k < b.length) {
                // check if "b" has a smaller element
                if (b[k] < a[j]) {
                    // if so add it to "c"
                    c[i] = b[k];
                    k++;
                }
                // if "a" has a smaller element
                else {
                    // add it to "c"
                    c[i] = a[j];
                    j++;
                }       
            }
            // if there are no more elements to check in "a"
            // but there are still elements to check in "b"
            else if (k < b.length) {
                // add those elements in "b" to "c"
                c[i] = b[k];
                k++;
            }
            // if there are no more elements to check in "b"
            // but there are still elements to check in "a"
            else {
                // add those elements in "a" to "c"
                c[i] = a[j];
                j++;
            }
        }
    
        for(int i = 0; i < c.length; i++)
            System.out.println(c[i]);
    }
    
    publicstaticvoidmain(字符串[]args){
    int a[]={3,5,7,9,12,14,15};
    int b[]={6,7,10};
    int j=0,k=0;
    //输出阵列应为3,5,6,7,7,9,10,12,14,15
    int c[]=新的int[a.length+b.length];//10个值
    //我们正在用下一个合适的数字填充c
    //我们从检查a[0]和b[0]开始,直到添加
    //所有元素
    for(int i=0;i
    希望有帮助。

    您可以尝试此代码

    public static void main(String[] args) {
        int a[] = { 3, 5, 7, 9, 12, 14, 15 };
        int b[] = { 6, 7, 10 };
    
        // output array should be 3,5,6,7,7,9,10,12,14,15
    
        int alen = a.length;
        int blen = b.length;
        int c[] = new int[a.length + b.length];// 10 values
    
        int s[] = null;
        int[] l = null;
    
        if (alen < blen) {
            s = a;
            l = b;
        } else {
            s = b;
            l = a;
        }
                // Constructing Combined Array
        for (int i = 0, p = 0; i < c.length; i++, p++) {
            if (i == s.length) {
                p = 0;
            }
            if (i < s.length) {
                c[i] = s[p];
            } else {
                c[i] = l[p];
            }
        }
                //Sorting the C array 
        for (int i = 1; i < c.length; i++) {
            int j = i;
            int B = c[i];
            while ((j > 0) && (c[j - 1] > B)) {
                c[j] = c[j - 1];
                j--;
            }
            c[j] = B;
        }
    
        for (int i = 0; i < c.length; i++)
            System.out.print(c[i]);
    }
    
    publicstaticvoidmain(字符串[]args){
    INTA[]={3,5,7,9,12,14,15};
    int b[]={6,7,10};
    //输出阵列应为3,5,6,7,7,9,10,12,14,15
    内部
    
    public class Combinearray {
    
        public static void main(String[] args) {
            int[] array1= {5,4,6,2,1};
            int[] array2= {2,5,8,4,1,6,4};
            int m=array1.length;
            int n=array2.length;
            int[] array3=new int[m+n];
            int a=1;
            for(int i=0;i<m;i++) {
    
                array3[i]=array1[i];//array1 is copied to array3
            }
            for(int i=0;i<n;i++) {
                array3[m-1+a]=array2[i];//array2 is copied to array3
                a++;
            }
            //array3 is combined array
             int l=array3.length;
                int temp[]=new int[l];
                for(int i=0;i<l;i++) {
                    for(int j=i+1;j<l;j++) {
                        if(array3[i]>array3[j]) {
                            temp[i]=array3[i];
                            array3[i]=array3[j];
                            array3[j]=temp[i];
                        }
                    }
                }
                System.out.println("sorted array is ");
                System.out.print("[");
                for(int i=0;i<l;i++) {
                    System.out.print(array3[i]+" ");  
                }
                System.out.print("]");
    
        }
    
    }