Coursera Algorithms week3 归并排序 练习测验1: 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的额外子空间辅助归并是个很合理的要求,实现如下:

 1 import java.util.Arrays;
 2 import edu.princeton.cs.algs4.StdRandom;
 3
 4 public class MergeSortedSubArray {
 5     private static boolean less(Comparable v, Comparable w) {
 6         return v.compareTo(w) < 0;
 7     }
 8     public static void merge(Comparable[] array){
 9         int n = array.length/2;
10         Comparable[] aux = new Comparable[n];
11         for(int i=0;i<n;i++){ //取左半边sorted的元素至辅助数组,因为未来归并左侧位置可能会被右侧元素占据
12             aux[i] = array[i];
13         }
14         System.out.println(Arrays.toString(aux));
15         int l = 0;
16         int r = n;
17         for(int k = 0; k<2*n;k++){
18             if(l >= n) break;//辅助元素数组全部用完,array右侧不需要挪动位置了
19             else if(r>=2*n) array[k]=aux[l++];//array原右侧元素全部放置合适位置,后面只需把辅助数组的元素挪到array右侧
20             else if(less(array[r],aux[l])) array[k] = array[r++];
21             else array[k] = aux[l++];
22         }
23     }
24
25     public static void main(String[] args){
26         int n = 10;
27         int[] subarray1 = new int[n];
28         int[] subarray2 = new int[n];
29         for (int i = 0; i < n; i++) {
30             subarray1[i] = StdRandom.uniform(100);
31             subarray2[i] = StdRandom.uniform(100);
32         }
33         Arrays.sort(subarray1);
34         Arrays.sort(subarray2);
35         Integer[] array = new Integer[2*n];
36         for(int i = 0; i<n;i++){
37             array[i] = subarray1[i];
38             array[n+i] = subarray2[i];
39         }
40         System.out.println(Arrays.toString(array));
41         merge(array);
42         System.out.println(Arrays.toString(array));
43     }
44 }
时间: 2024-10-15 11:46:35

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

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, respectively, design an algorithm to find the kth largest key. The order  of growth of the worst case running time of your algorithm should be logn, where n

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 to find the corresponding pairs of nuts and bolts. Each nut fits exactly one bolt and each bolt fits exactly one nut. By fitting a nut and a bolt toget

Coursera Algorithms week2 栈和队列 练习测验: Stack with max

题目原文: Stack with max. Create a data structure that efficiently supports the stack operations (push and pop) and also a return-the-maximum operation. Assume the elements are reals numbers so that you can compare them. 分析: 该题目要求在实现正常stack的push和pop操作外,还

coursera Algorithms week1 练习测验2:Union-find with specific canonical element

题目原文: Add a method find() to the union-find data type so that find(i) returns the largest element in the connected component containing i. The operations, union(), connected(), and find() should all take logarithmic time or better. 1 import edu.princ

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 constant amount of extra space. 1 public void traverse(BST<Key,Value> bst) { 2 traverse(bst.root.left, bst.root); 3 } 4 5 private void traverse(Node curre

Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST

题目原文: Given a binary tree where each  ???????? contains a key, determine whether it is a binary search tree. Use extra space proportional to the height of the tree. 分析: 就是递归遍历BST,将每个节点x分别与x.left和x.right比大小. 1 public boolean isBST(BST<Key,Value> bst)

Coursera Algorithms week1 练习测验3:Successor with delete

题目原文: Given a set of n integers S = {0,1,-,N-1}and a sequence of requests of the following form: Remove x from S Find the successor of x: the smallest y in S such thaty>=x design a data type so that all operations(except construction) take logarithmi

Coursera Algorithms Programming Assignment 4: 8 Puzzle (100分)

题目原文:http://coursera.cs.princeton.edu/algs4/assignments/8puzzle.html 题目要求:设计一个程序解决8 puzzle问题以及该问题的推广,例如8-puzzle是3*3,程序要能解决n*n的同类问题(2 ≤ n < 128) 典型的8 puzzle如下: 算法设计参照A*搜索算法,即使不了解A*搜索算法,题目也已经将解法解释的很具体了. Best-first search:设计参照A* 搜索算法.定义一个 search node类,包

【Algorithms】归并排序(merge sort)

几个比较常见的排序算法里头,这个我是比较生疏的一个,有一天突然被问了一个问题是,归并排序最大的特点是什么,我才想起这个算法来.下午又看不进书啦,就实现一下,记下来. 归并排序采取的是分治策略,就是先将数据不断地进行二分,然后分别排序子序列之后再不断地合并在一起. 归并排序与快排一样,时间复杂度是O(nlogn),是一个比较高效率的排序算法. vector<int> mergeSort(vector<int>& nums, int m, int n) { if (m == n