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 ...
随机推荐
- ubuntu14.3安装phpmyadmin
一.安装 sudo apt-get install phpmyadmin 二.软连接 cd /var/www/html/ sudo ln -s /usr/share/phpmyadmin phpmya ...
- Android本地消息推送
项目介绍:cocos2dx跨平台游戏 项目需求:实现本地消息推送,需求①:定点推送:需求②:根据游戏内逻辑实现推送(比如玩家体力满时,需要计算后到点推送):需求③:清理后台程序或重启后依然能够实现本地 ...
- Ubuntu 18.04 如何固定图标到任务栏
参考 https://blog.csdn.net/u014160286/article/details/81631863
- js 计算是今天多少周
/** * 判断年份是否为润年 * * @param {Number} year */ function isLeapYear(year) { return (year % 400 == 0) || ...
- 6.3.3 使用 shelve 模块操作二进制文件
Python标准库shelve也提供了二进制文件操作的功能,可以像字典赋值一样来写入二进制文件,也可以像字典一样读取二进制文件,有点类似于NoSQL数据库MongoDB. import shelve ...
- F - Many Moves
F - Many Moves Time limit : 2sec / Memory limit : 256MB Score : 900 points Problem Statement There a ...
- SCU Right turn
Right turn frog is trapped in a maze. The maze is infinitely large and divided into grids. It also c ...
- extjs 4 chart 时间轴格式的处理
var dayStore = Ext.create('Ext.data.JsonStore', { fields: [{ name: 'name', type: 'date', dateFormat: ...
- 洛谷—— P1092 虫食算
https://www.luogu.org/problem/show?pid=1092 题目描述 所谓虫食算,就是原先的算式中有一部分被虫子啃掉了,需要我们根据剩下的数字来判定被啃掉的字母.来看一个简 ...
- Spring MVC-视图解析器(View Resolverr)-资源包视图解析器(Resource Bundle View Resolver)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_resourcebundleviewresolver.htm 说明:示例基于Spr ...