高级农民
- 积分
- 1752
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-3-18
- 最后登录
- 1970-1-1
|
1
(a)-(d) 写纸上懒得打一遍了
(e) When the array size is small (typically N < 15).
2
(a) Selection sort
(b) Insertion sort
(c) Merge sort
(d) Insertion sort
3
| | Insertion sort | Selection sort | Merge sort | Heapsort | | Worst case | N^2 | N^2 | N log N | N log N | | Best case | N | N^2 | N log N | N |
4- import java.util.Arrays;
- public class MergeTwo {
- public static int[] mergeTwo(int[] a, int[] b) {
- int ap = 0;
- int bp = 0;
- int[] c = new int[a.length + b.length];
- for (int i = 0; i < c.length; i++) {
- if (ap < a.length && bp < b.length) {
- if (a[ap] < b[bp]) {
- c[i] = a[ap];
- ap++;
- } else {
- c[i] = b[bp];
- bp++;
- }
- } else if (ap < a.length) {
- c[i] = a[ap];
- ap++;
- } else if (bp < b.length) {
- c[i] = b[bp];
- bp++;
- }
- }
- return c;
- }
- public static void main(String[] args) {
- int[] a = {-18, -7, 0, 1, 3, 5, 11, 29, 253};
- int[] b = {-22, -7, 0, 2, 5, 9, 22, 111, 789, 1000, 2333};
- System.out.println(Arrays.toString(mergeTwo(a, b)));
- }
- }
复制代码 |
|