Java教程

排序算法

本文主要是介绍排序算法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

选择排序

 

 1 public class MySelectionSort {
 2 
 3 
 4     public static void main(String[] args) {
 5         for (int i = 0; i < 100; i++) {
 6             int[] array = generateRandomArray(10, 10);
 7             System.out.println("随机数组:" + Arrays.toString(array));
 8             int[] copyArray = Arrays.copyOf(array, array.length);
 9             Arrays.sort(copyArray);
10             System.out.println("Arrays工具类排序:" + Arrays.toString(copyArray));
11             selectionSort(array);
12             System.out.println("选择排序法排序:" + Arrays.toString(array));
13             IntArrayComparator intArrayComparator = new IntArrayComparator();
14             int compare = intArrayComparator.compare(copyArray, array);
15             if (compare == 0){
16                 System.out.println("----------------------------排序成功------------------------");
17             }else{
18                 System.out.println("----------------------------排序失败------------------------");
19                 throw new RuntimeException("排序失败");
20             }
21         }
22 
23     }
24 
25     /**
26      * 生成一个[-maxValue,maxValue]的随机数组
27      *
28      * @param maxSize  最大长度
29      * @param maxValue 最大值
30      * @return 随机数组
31      */
32     private static int[] generateRandomArray(int maxSize, int maxValue) {
33         int[] array = new int[maxSize];
34         for (int i = 0; i < maxSize; i++) {
35             array[i] = (int) ((maxValue + 1) * Math.random()) - (int) ((maxSize + 1) * Math.random());
36         }
37         return array;
38     }
39 
40     /**
41      * 排序数组
42      *
43      * @param array
44      */
45     private static void selectionSort(int[] array) {
46         //比较流程
47         //比较0->1,2,3,4...array.length-1
48         //比较1->2,3,4,5...array.length-1
49         //比较2->3,4,5,6...array.length-1
50         //.
51         //.
52         //.
53         //比较array.length-2->array.length-1
54 
55         //最外层[0,array.length-2]
56         for (int i = 0; i < array.length-1; i++) {
57             int minIndex = i;
58             //内层[比最外层下标大一,array.length-1]
59             for (int j = i + 1; j < array.length; j++) {
60                 if (array[minIndex] > array[j]) {
61                     minIndex = j;
62                 }
63             }
64             swap(array, i, minIndex);
65         }
66     }
67 
68     public static void swap(int[] array, int i, int j) {
69         if(i!=j){
70             int swap = array[i];
71             array[i] = array[j];
72             array[j] = swap;
73         }
74     }
75 
76     public static class IntArrayComparator implements Comparator<int[]> {
77         @Override
78         public int compare(int[] o1, int[] o2) {
79             if (o1 == null || o2 == null) {
80                 throw new RuntimeException("比较的两个数据不能为null");
81             }
82             if (o1.length != o2.length) {
83                 return -1;
84             }
85             for (int i = 0; i < o1.length; i++) {
86                 if (o1[i] != o2[i]) {
87                     return 1;
88                 }
89             }
90             return 0;
91         }
92     }
93 }

 

这篇关于排序算法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!