JAVA进阶19

1、冒泡排序

 1 package cn.zh.abstrac;
 2 
 3 import java.util.Arrays;
 4 
 5 //冒泡排序
 6 public class Demo019 {
 7     public static void main(String[] args) {
 8         int[] values = {22, 11, 33, 2, 4, 5, 66, 55, 44};
 9         int temp = 0;
10         for (int j = 0; j < values.length - 1 - j; j++) {
11             boolean flag = true;
12             for (int i = 0; i < values.length - 1; i++) {
13                 //比较大小,换顺序
14                 if (values[i] > values[i + 1]) {
15                     temp = values[i];
16                     values[i] = values[i + 1];
17                     values[i + 1] = temp;
18 
19                     flag = false;
20                 }
21             }
22             if (flag) {
23                 break;
24             }
25         }
26         System.out.println(Arrays.toString(values));
27     }
28 }
View Code

运行结果图

2、二分法查找法

 1 package cn.zh.abstrac;
 2 
 3 import java.util.Arrays;
 4 
 5 //二分法查找
 6 public class TestBinarySearch {
 7     public static void main(String[] args) {
 8         int[] arr = {30, 20, 50, 10, 80, 9, 7, 12, 100, 40, 8};
 9         Arrays.sort(arr);
10         System.out.println(Arrays.toString(arr));
11         System.out.println(myBinarySearch(arr,40));
12     }
13 
14     public static int myBinarySearch(int[] arr, int value){
15         int low = 0;
16         int high = arr.length - 1;
17 
18         while (low <= high) {
19             int mid = (low + high) / 2;
20             if (value == arr[mid]) {
21                 return mid;
22             }
23             if (value > arr[mid]) {
24                 low = mid + 1;
25             }
26             if (value < arr[mid]) {
27                 high = mid - 1;
28             }
29         }
30         return -1;
31     }
32 }
View Code

运行结果图

3、可变字符序列与不可变字符序列

注:循环累加用StringBuilder

 1 package cn.zh.abstrac;
 2 
 3 public class TestStringBuilder {
 4     public static void main(String[] args) {
 5         //使用String进行字符串的拼接
 6         String str1 = "";
 7         //本质上使用StringBuilder拼接,但是每次循环都会生成一个StringBuilder对象
 8         long num1 = Runtime.getRuntime().freeMemory(); //获取系统剩余内存空间
 9         //获取系统的当前时间
10         long time1 = System.currentTimeMillis();
11         for (int i = 0; i < 5000; i++) {
12             //相当于产生了10000个对象
13             str1 = str1 + i;
14         }
15         long num2 = Runtime.getRuntime().freeMemory();
16         long time2 = System.currentTimeMillis();
17         System.out.println("String占用内存:" + (num1 - num2));
18         System.out.println("String占用时间:" + (time2 - time1));
19 
20         //使用StringBuilder进行字符串的拼接
21         StringBuilder sb1 = new StringBuilder("");
22         long num3 = Runtime.getRuntime().freeMemory();
23         long time3 = System.currentTimeMillis();
24         for (int i = 0; i < 5000; i++) {
25             sb1.append(i);
26         }
27         long num4 = Runtime.getRuntime().freeMemory();
28         long time4 = System.currentTimeMillis();
29         System.out.println("StringBuilder占用内存:" + (num3 - num4));
30         System.out.println("StringBuilder占用时间:" + (time4 - time3));
31     }
32 }
View Code

运行结果图

4、

猜你喜欢

转载自www.cnblogs.com/Anemia-BOY/p/10678776.html