|
379 | 379 | int pRoot = find(p);
|
380 | 380 | int qRoot = find(q);
|
381 | 381 | if(pRoot == qRoot) return;
|
382 |
| - if(size[pRoot] > size[qRoot]){ |
| 382 | + if(size[pRoot] > size[qRoot]){ //比较根节点中,大小更大的,将小树连接到大树上。 |
383 | 383 | a[qRoot] = pRoot;
|
384 | 384 | size[pRoot] += size[qRoot];
|
385 | 385 | }
|
|
401 | 401 | 3.一棵树的高度是所有节点的最大深度。
|
402 | 402 |
|
403 | 403 |
|
| 404 | +------------------------------------------------------------------------------------------------------------------------- |
| 405 | +---------------------------------------Chapter Two----------------------------------------------------------------------- |
| 406 | +------------------------------------------------------------------------------------------------------------------------- |
| 407 | +1. 接口中内容的定义: |
| 408 | + 1.JDK8.0以前的接口中: |
| 409 | + ->变量默认是public, static, final的。 |
| 410 | + ->方法默认是public, abstract的。 |
| 411 | + public interface JDK8BeforeInterface { |
| 412 | + public static final int field1 = 0; |
| 413 | + int field2 = 0; |
| 414 | + public abstract void method1(int a) throws Exception; |
| 415 | + void method2(int a) throws Exception; |
| 416 | + } |
| 417 | + 2.JDK8.0及以后: |
| 418 | + ->允许我们在接口中定义static方法和default方法。 |
| 419 | + ->静态方法,只能通过接口名调用,不可以通过实现类的类名或者实现类的对象调用 |
| 420 | + ->default方法,只能通过接口实现类的对象来调用。 |
| 421 | + public interface JDK8Interface { |
| 422 | + // static修饰符定义静态方法 |
| 423 | + static void staticMethod() { |
| 424 | + System.out.println("接口中的静态方法"); |
| 425 | + } |
| 426 | + // default修饰符定义默认方法 |
| 427 | + default void defaultMethod() { |
| 428 | + System.out.println("接口中的默认方法"); |
| 429 | + } |
| 430 | + } |
| 431 | + |
| 432 | +2. Sort的通用方法: |
| 433 | + 实现:ca.mcmaster.chapter.two.Sort.Sort |
| 434 | + public class Sort { |
| 435 | + @SuppressWarnings("rawtypes") |
| 436 | + public static void selectSort(Comparable[] a){}; |
| 437 | + public static Boolean less(Comparable a, Comparable b){ |
| 438 | + return a.compareTo(b) < 0; |
| 439 | + } |
| 440 | + public static void swap(Comparable[] a, int i, int j){ |
| 441 | + Comparable temp = a[i]; |
| 442 | + a[i] = a[j]; |
| 443 | + a[j] = temp; |
| 444 | + } |
| 445 | + public static void show(Comparable[] a){ |
| 446 | + for(int i = 0; i < a.length; i++){ |
| 447 | + System.out.print(a[i] + " "); |
| 448 | + } |
| 449 | + System.out.println(); |
| 450 | + } |
| 451 | + public static Boolean isSorted(Comparable[] a){ |
| 452 | + for(int i = 1; i < a.length; i++) |
| 453 | + if(less(a[i], a[i-1])) return false; |
| 454 | + return true; |
| 455 | + } |
| 456 | + } |
| 457 | + |
| 458 | +3. 选择排序SelectionSort:O(n^2) |
| 459 | + 实现:ca.mcmaster.chapter.two.Sort.Sort#selectionSort(Comparable[]) |
| 460 | + 从数组头开始遍历数组,找到剩余数组中的最小元素,将最小元素和当前元素交换位置。 |
| 461 | + 和输入数据的情况没有关系,数据已有的排序度对算法时间没有影响。 |
| 462 | + public static void selectionSort(Comparable[] a){ |
| 463 | + int length = a.length; |
| 464 | + for(int i = 0; i < length; i++){ |
| 465 | + int min = i; |
| 466 | + for(int j = i + 1; j < length; j++) |
| 467 | + if(less(a[j], a[min])) min = j; |
| 468 | + swap(a, i, min); //交换元素的操作在集合之外,保证了对于外层遍历中的每个元素,交换只进行一次。 |
| 469 | + } |
| 470 | + } |
| 471 | + |
| 472 | +4. 插入排序InsertSort:O(n^2) |
| 473 | + 实现:ca.mcmaster.chapter.two.Sort.Sort#insertSort(Comparable[]) |
| 474 | + 从第二个元素遍历整个数组,判断当前元素和之前所有元素的大小,插入正确的位置。 |
| 475 | + 输入数据排序度更高,运行时间越短。 |
| 476 | + public static void insertSort(Comparable[] a){ |
| 477 | + int length = a.length; |
| 478 | + for(int i = 1; i < length; i ++){ //遍历数组中的所有元素a[i]。 |
| 479 | + for(int j = i; j > 0 && less(a[j], a[j-1]); j--) //从当前遍历到的位置开始,往前遍历,两个元素比较大小,如果小可以将当前a[i]向前移动。 |
| 480 | + swap(a, j, j-1); //虽然看上去是在对a[j]进行遍历,实际上是如果a[i]和前一元素相比更小,就将a[i]向前移动。 |
| 481 | + } |
| 482 | + } |
| 483 | + |
404 | 484 |
|
| 485 | + |
405 | 486 |
|
406 | 487 |
|
407 | 488 |
|
|
418 | 499 |
|
419 | 500 |
|
420 | 501 |
|
421 |
| - |
422 |
| - |
423 |
| - |
424 |
| - |
425 |
| - |
426 |
| - |
427 |
| - |
428 |
| - |
429 |
| - |
430 |
| - |
431 |
| - |
432 |
| - |
433 |
| - |
| 502 | + |
434 | 503 |
|
435 | 504 |
|
436 | 505 |
|
|
0 commit comments