Java-->Arrays類、選擇排序、二分查詢
阿新 • • 發佈:2022-05-06
- 初識Arrays類的API:
1 public static void main(String[] args) {
2 int[] arr = {5, 4, 6, 8, 0, 1, 3, 2};
3 System.out.println("陣列地址:" + arr);
4 System.out.println("陣列內容:" + Arrays.toString(arr));
5 Arrays.sort(arr);
6 System.out.println("排序(升序):" + Arrays.toString(arr));
7 System.out.println("二分查詢位置:" + Arrays.binarySearch(arr, 2));
8 System.out.println("二分查詢位置:" + Arrays.binarySearch(arr, 10));
9 }
示例結果:
- Comparator介面對應的比較器對應用:
1 public static void main(String[] args) {
2 //1、使用API完成陣列的升序
3 int[] ages = {21,22,31,15};
4 Arrays.sort(ages);
5 System.out.println(Arrays.toString(ages));
6 System.out.println("-------API只能升序,如何降序?-------");
7 //自定義排序(只能支援引用型別):降序
8 Integer[] ages2 = {12,15,45,30,6};
9 /**
10 * 引數一 : 被比較的陣列
11 * 引數兒 : 匿名內部欸,代表一個比較器物件
12 */
13 Arrays.sort(ages2, new Comparator<Integer>() {
14 @Override
15 public int compare(Integer o1, Integer o2) {
16 // if(o1 > o2){
17 // return 1;
18 // }
19 // else if(o1 < o2){
20 // return -1;
21 // }
22 // else {
23 // return 0;
24 // }
25 //return o1 - o2;
26 return -(o1 - o2);
27 }
28 });
29 System.out.println(Arrays.toString(ages2));
30
31 System.out.println("-----------------");
32 Student[] students = new Student[3];
33 students[0] = new Student("張三",21,1.75);
34 students[1] = new Student("李四",19,1.68);
35 students[2] = new Student("王五",20,1.81);
36 System.out.println(Arrays.toString(students));
37
38 //Arrays.sort(students) //會程式崩潰
39 Arrays.sort(students, new Comparator<Student>() {
40 @Override
41 public int compare(Student o1, Student o2) {
42 //return o1.getAge() - o2.getAge(); //按照年齡升序排序
43 //return o2.getAge() - o1.getAge(); //按照年齡降序排序
44 //return Double.compare(o1.getHeight(),o2.getHeight()); //按照身高升序
45 return Double.compare(o2.getHeight(),o1.getHeight()); //按照升高降序
46 }
47 });
48 System.out.println(Arrays.toString(students));
49 }
Student類:
1 public class Student {
2 private String name;
3 private int age;
4 private double height;
5
6 public String getName() {
7 return name;
8 }
9
10 public void setName(String name) {
11 this.name = name;
12 }
13
14 public int getAge() {
15 return age;
16 }
17
18 public void setAge(int age) {
19 this.age = age;
20 }
21
22 public double getHeight() {
23 return height;
24 }
25
26 public void setHeight(double height) {
27 this.height = height;
28 }
29
30 public Student() {
31 }
32
33 public Student(String name, int age, double height) {
34 this.name = name;
35 this.age = age;
36 this.height = height;
37 }
38
39 @Override
40 public String toString() {
41 return "Student{" +
42 "name='" + name + '\'' +
43 ", age=" + age +
44 ", height=" + height +
45 '}';
46 }
47 }
示例結果:
- 常見演算法(選擇排序、二分查詢)
- 選擇排序:
1 public static void main(String[] args) {
2 int[] arr = {1,5,3,2};
3 for (int i = 0; i < arr.length - 1; i++) { //外層迴圈控制輪數
4 // i = 0 j: 0, 1, 2, 3
5 // i = 1 j: 1, 2, 3
6 // i = 2 j: 2, 3
7 // i = 3 j: 3
8 //相鄰兩數選出較小值排在左邊
9 for (int j = i+1; j < arr.length; j++) {
10 if(arr[i] > arr[j]){
11 int tmp = arr[i];
12 arr[i] = arr[j];
13 arr[j] = tmp;
14 }
15 }
16 }
17 System.out.println(Arrays.toString(arr));
18 }
示例結果:
- 二分查詢:
1 public static void main(String[] args) {
2 int[] arr = {1,6,51,98,121,150,194,201,205,255};
3 int key = 201;
4 //定義左右邊界
5 int left = 0;
6 int right = arr.length - 1;
7 //迴圈條件
8 while (left <= right){
9 int midIndex = (left + right) / 2;
10 //中間元素的值大於所要查詢的資料
11 if(arr[midIndex] > key){
12 right = midIndex - 1;
13 }
14 //中間元素的值小於所要查詢的資料
15 else if(arr[midIndex] < key){
16 left = midIndex + 1;
17 }
18 //中間元素的值等於所要查詢的資料
19 else{
20 System.out.println("找到了,索引為:" + midIndex);
21 break;
22 }
23 }
24 if(left > right){
25 System.out.println("找不到");
26 }
27 }