Coursera Algorithms week3 归并排序 练习测验: Merging with smaller auxiliary array
题目原文:
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的更多相关文章
- Coursera Algorithms week3 归并排序 练习测验: Shuffling a linked list
题目原文: Shuffling a linked list. Given a singly-linked list containing n items, rearrange the items un ...
- 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 ...
- 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 ...
- Coursera Algorithms week3 快速排序 练习测验: Decimal dominants(寻找出现次数大于n/10的元素)
题目原文: Decimal dominants. Given an array with n keys, design an algorithm to find all values that occ ...
- 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 ...
- Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题
题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...
- 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 ...
- Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法
第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...
- 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 ...
随机推荐
- microsoft ajax registered - to fix microsoft ajax update panel post back
<dnn:DnnScriptBlock runat="server"> <script type="text/javascript"& ...
- win10 ubuntu18双系统环境搭建
感谢前辈辛勤总结,根据这3篇文章成功配置了双系统 https://blog.csdn.net/qq_24624539/article/details/81775635 https://blog.csd ...
- S3C2440中断
韦东山老师一期中断课程学习: 总结: 程序启动后工作流程,程序从0地址开始执行Reset --> 重定位 -->ldr pc,=main [绝对跳转到SDRAM中执行main()函数] ...
- vue 微信授权解决方案
背景 前后端分离项目 - SpringSocial 绑定与解绑社交账号如微信.QQ2018-08-14更新时隔四个月第一次更新,因为项目重构有一次接触到了微信授权,思路已经比原来清晰的多了,将重新修改 ...
- Linux - redis-cluster搭建
目录 Linux - redis-cluster搭建 Linux - redis-cluster搭建 1.准备6个数据库节点,也就是6个redis实例,也就是6个配置文件 配置文件如下 redis-7 ...
- WPF Style设置和模板化Template
WPF样式设置和模板化是一套功能(样式,模板,触发器和演示图版),可以为产品设置统一外观.类似于html的css,可以快速的设置一系列属性值到控件. 案例:ButtonStyle 这里创建了一个目标类 ...
- 实验十二 团队作业8:软件测试与Alpha冲刺 第四天
项目 内容 这个作业属于哪个课程 老师链接 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 Always Run! 作业学习目标 (1)掌握软件测试基础技术 (2)学习 ...
- ARC 73 E - Ball Coloring
E - Ball Coloring Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Statement Ther ...
- python浅拷贝与深拷贝
今天写程序,人为制造了一个由浅拷贝引起的bug,有必要归纳一下.先附上源代码: class PerformanceTest(object): def __init__(self): ....... s ...
- UVa - 11452 - Dancing the Cheeky-Cheeky
先上题目: F. Dancing the Cheeky-Cheeky Context The Cheeky-Cheeky is a new song. They dance it in Mula, ...