Skip to content

Commit 065f46f

Browse files
committed
[Function add]
1.SelectedSort and InsertSort.
1 parent 208078a commit 065f46f

File tree

2 files changed

+130
-14
lines changed

2 files changed

+130
-14
lines changed

Algorithm(4th_Edition)/algorithm_note.txt

+83-14
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@
379379
int pRoot = find(p);
380380
int qRoot = find(q);
381381
if(pRoot == qRoot) return;
382-
if(size[pRoot] > size[qRoot]){
382+
if(size[pRoot] > size[qRoot]){ //比较根节点中,大小更大的,将小树连接到大树上。
383383
a[qRoot] = pRoot;
384384
size[pRoot] += size[qRoot];
385385
}
@@ -401,7 +401,88 @@
401401
3.一棵树的高度是所有节点的最大深度。
402402

403403

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+
404484

485+
405486

406487

407488

@@ -418,19 +499,7 @@
418499

419500

420501

421-
422-
423-
424-
425-
426-
427-
428-
429-
430-
431-
432-
433-
502+
434503

435504

436505

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ca.mcmaster.chapter.two.Sort;
2+
3+
public class Sort {
4+
@SuppressWarnings("rawtypes")
5+
public static void selectionSort(Comparable[] a){
6+
int length = a.length;
7+
for(int i = 0; i < length; i++){
8+
int min = i;
9+
for(int j = i + 1; j < length; j++)
10+
if(less(a[j], a[min])) min = j;
11+
swap(a, i, min);
12+
}
13+
}
14+
15+
public static void insertSort(Comparable[] a){
16+
int length = a.length;
17+
for(int i = 1; i < length; i ++){
18+
for(int j = i; j > 0 && less(a[j], a[j-1]); j--)
19+
swap(a, j, j-1);
20+
}
21+
}
22+
public static Boolean less(Comparable a, Comparable b){
23+
return a.compareTo(b) < 0;
24+
}
25+
public static void swap(Comparable[] a, int i, int j){
26+
Comparable temp = a[i];
27+
a[i] = a[j];
28+
a[j] = temp;
29+
}
30+
public static void show(Comparable[] a){
31+
for(int i = 0; i < a.length; i++){
32+
System.out.print(a[i] + " ");
33+
}
34+
System.out.println();
35+
}
36+
public static Boolean isSorted(Comparable[] a){
37+
for(int i = 1; i < a.length; i++)
38+
if(less(a[i], a[i-1])) return false;
39+
return true;
40+
}
41+
public static void main(String[] args) {
42+
Integer[] a = new Integer[]{2,3,5,2,45,1,4,2,5,7,3,7,432,96,7,23,8};
43+
// selectionSort(a);
44+
insertSort(a);
45+
show(a);
46+
}
47+
}

0 commit comments

Comments
 (0)