题目原文:

Suppose that the subarray a[0] to a[n-1] is sorted and the subarray a[n] to a[2*n-1] is sorted. How can you merge the two subarrays so that a[0] to a[2*n-1] is sorted using an auxiliary array of length n (instead of 2n)

分析:

对两个大小分别为n的有序子数组进行归并,要求空间复杂度为n,正常情况下归并排序在此处的空间复杂度为2n,但是由于两个子数组分别是有序的,故用大小为n的额外子空间辅助归并是个很合理的要求,实现如下:

 import java.util.Arrays;
import edu.princeton.cs.algs4.StdRandom; public class MergeSortedSubArray {
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
public static void merge(Comparable[] array){
int n = array.length/2;
Comparable[] aux = new Comparable[n];
for(int i=0;i<n;i++){ //取左半边sorted的元素至辅助数组,因为未来归并左侧位置可能会被右侧元素占据
aux[i] = array[i];
}
System.out.println(Arrays.toString(aux));
int l = 0;
int r = n;
for(int k = 0; k<2*n;k++){
if(l >= n) break;//辅助元素数组全部用完,array右侧不需要挪动位置了
else if(r>=2*n) array[k]=aux[l++];//array原右侧元素全部放置合适位置,后面只需把辅助数组的元素挪到array右侧
else if(less(array[r],aux[l])) array[k] = array[r++];
else array[k] = aux[l++];
}
} public static void main(String[] args){
int n = 10;
int[] subarray1 = new int[n];
int[] subarray2 = new int[n];
for (int i = 0; i < n; i++) {
subarray1[i] = StdRandom.uniform(100);
subarray2[i] = StdRandom.uniform(100);
}
Arrays.sort(subarray1);
Arrays.sort(subarray2);
Integer[] array = new Integer[2*n];
for(int i = 0; i<n;i++){
array[i] = subarray1[i];
array[n+i] = subarray2[i];
}
System.out.println(Arrays.toString(array));
merge(array);
System.out.println(Arrays.toString(array));
}
}

Coursera Algorithms week3 归并排序 练习测验: Merging with smaller auxiliary array的更多相关文章

  1. Coursera Algorithms week3 归并排序 练习测验: Shuffling a linked list

    题目原文: Shuffling a linked list. Given a singly-linked list containing n items, rearrange the items un ...

  2. Coursera Algorithms week3 归并排序 练习测验: Counting inversions

    题目原文: An inversion in an array a[] is a pair of entries a[i] and a[j] such that i<j but a[i]>a ...

  3. Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)

    题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respec ...

  4. Coursera Algorithms week3 快速排序 练习测验: Decimal dominants(寻找出现次数大于n/10的元素)

    题目原文: Decimal dominants. Given an array with n keys, design an algorithm to find all values that occ ...

  5. Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts

    题目原文: Nuts and bolts. A disorganized carpenter has a mixed pile of n nuts and n bolts. The goal is t ...

  6. Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题

    题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...

  7. Coursera Algorithms week1 算法分析 练习测验: 3Sum in quadratic time

    题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case ...

  8. Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法

    第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...

  9. Coursera Algorithms week4 基础标签表 练习测验:Inorder traversal with constant extra space

    题目原文: Design an algorithm to perform an inorder traversal of a binary search tree using only a const ...

随机推荐

  1. Linux 一些小知识点汇总(持续更新....)

    一.符号 1.$@:传递的参数. 2.$# :传递参数的数量. 3.$?:指上一次执行命令后的返回值.一般0表示运行成功. 补充:$?只表示上一个命令执行后的退出状态,当命令执行后,又执行了其他命令, ...

  2. 基于zk“临时顺序节点“的分布式锁

    import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import java.io.IOException; im ...

  3. 视频cover占满

    /* 关键属性 */ object-fit: fill; //被替换的内容的大小,以填补该元素的内容框:对象的具体对象的大小是元素的使用宽度和高度. object-fit: contain;被替换的内 ...

  4. php省市区三级联动

    效果 步骤 前端:通过ajax请求获取数据,使用了jquery 页面一开始加载所有省份信息 ->当选择省下拉框后触发改变监听时间-change ->当选择市下拉框后触发改变监听时间-cha ...

  5. matlab数值数据的表示方法,输出数据以及相关函数

    数据类型的分类: 1.整型 无符号整型和带符号整形 带符号整形的最大值是127 >>x=int8(129) 输出结果是x=127 >>x=unit8(129) 输出结果是x=1 ...

  6. vc++6.0创建console32之.c的应用程序详解

    文件-->新建-->win32-->取一个名字,确定 文件-->新建-->c++Source-->取一个名字,记住以.c为后缀,确定 编写简单的程序调试

  7. cf 337 div2 c

    C. Harmony Analysis time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  8. JS页面跳转和js对iframe进行页面跳转、刷新

    一.js方式的页面跳转1.window.location.href方式    <script language="JavaScript" type="text/ja ...

  9. Bequeath Connection and SYS Logon

    The following example illustrates how to use the internal_logon and SYSDBA arguments to specify the ...

  10. STL源码分析之内存池

    前言 上一节只分析了第二级配置器是由多个链表来存放相同内存大小, 当没有空间的时候就向内存池索取就行了, 却没有具体分析内存池是怎么保存空间的, 是不是内存池真的有用不完的内存, 本节我们就具体来分析 ...