合并排序数组

基本思想:一次比较两个数组中元素的大小,将较小元素赋值给新元素

private static int[] merge(int[] a, int[] b) {

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

		int i=0,j=0,n=0;

		while (i<a.length&&j<b.length) {

			if (a[i]<b[j]) {//a中元素小,则将其赋值给c

				c[n++]=a[i++];

			}

			else {//反之将b中元素赋值给c

				c[n++]=b[j++];

			}

		}//a中还有未赋值的元素

		while (i<a.length) {

			c[n++]=a[i++];

		}

		//b中还有未赋值的元素

		while (j<b.length) {

			c[n++]=b[j++];

		}

		return c;

	}

  

 

你可能感兴趣的:(排序)