一、八大排序算法
[TOC]
1.冒泡排序
画图技术不到位,等找到好的画图工具再来补齐后面的图吧!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public static void bubbleSort(int[] arr) { if(arr == null || arr.length < 2) { return; } for(int i = arr.length - 1; i > 0; i--) { for(int j = 0; j < i; j++) { if(arr[j] > arr[j + 1]) { swap(arr,j,j + 1); } } } } public static void swap(int[] arr, int j, int i) { arr[i] = arr[i] ^ arr[j]; arr[j] = arr[i] ^ arr[j]; arr[i] = arr[i] ^ arr[j]; }
|
2.选择排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static void selectSort(int[] arr) { if(arr == null || arr.length < 2) { return; } for(int i = 0; i < arr.length - 1; i++) { int minIndex = i; for(int j = i + 1; j < arr.length; j++) { if(arr[j] < arr[minIndex]) { minIndex = j; } } swap(arr,i,minIndex); } }
|
3.插入排序
1 2 3 4 5 6 7 8 9 10 11
| public static void insertSort(int[] arr) { if(arr == null || arr.length < 2) { return; } for(int i = 1; i < arr.length; i++) { for(int j = i - 1; j >=0 && arr[j] > arr[j + 1];j--) { swap(arr,j,j + 1); } } }
|
4.希尔排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static void shellSort(int[] arr) { if(arr == null || arr.length < 2) { return; } int h = 3; while(h >= 1) { for(int i = h; i < arr.length; i++) { for(int j = i - h; j >= 0 && arr[j] > arr[j + h]; j -= h) { swap(arr,j,j+h); } } h = h/3; } }
|
5.归并排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public static void mergeSort(int[]arr) { if(arr == null || arr.length < 2) { return; } mergeSort(arr,0,arr.length - 1); } public static void mergeSort(int[] arr,int low, int high) { if(low == high) { return; } int mid = low + (high - low)/2; mergeSort(arr, low, mid); mergeSort(arr,mid + 1, high); merge(arr,low,mid,high); } public static void merge(int[] arr,int low, int mid, int high) { int[] help = new int[high - low + 1]; int less = low; int more = mid + 1; int i = 0; while(less <= mid && more <= high) { if(arr[less] <= arr[more]){ help[i++] = arr[less++]; }else { help[i++] = arr[more++]; } } while(less <= mid) { help[i++] = arr[less++]; } while(more <= high) { help[i++] = arr[more++]; } for(i = 0; i < help.length; i++) { arr[low + i] = help[i]; } }
|
6.快速排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public static void quickSort(int[] arr) { if (arr == null || arr.length < 2) { return; } quickSort(arr, 0, arr.length - 1); }
public static void quickSort(int[] arr, int L, int r) { if (L < r) { int[] p = patition(arr, L, r); quickSort(arr, L, p[0] - 1); quickSort(arr, p[1] + 1, r); } }
public static int[] patition(int[] arr, int L, int r) { int less = L - 1; int more = r; while (L < more) { if (arr[L] > arr[r]) { swap(arr, --more, L); } else if (arr[L] < arr[r]) { swap(arr, ++less, L++); } else { L++; } } swap(arr, more, r); return new int[] { less + 1, more }; }
|
7.堆排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public static void heapSort(int[] arr) { if (arr == null || arr.length < 2) { return; }
int size = arr.length; for(int i = (size - 1)/2; i >= 0; i--) { heapify(arr,i,size); }
while (size > 0) { swap(arr, 0, --size); heapify(arr, 0, size); } }
public static void heapInsert(int[] arr, int index) {
}
public static void heapify(int[] arr, int index, int size) { while (index * 2 + 1 < size) { int left = index * 2 + 1; if(left + 1 < size && arr[left + 1] > arr[left]) { left++; } if(arr[left] <= arr[index]) { break; } swap(arr,index,left); index = left; } }
|
8.桶排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public static void bucketSort(int[] arr) { if (arr == null || arr.length < 2) { return; } int max = Integer.MIN_VALUE; for (int i = 0; i < arr.length; i++) { max = Math.max(max, arr[i]); } int[] bucket = new int[max + 1]; for (int i = 0; i < arr.length; i++) { bucket[arr[i]]++; } int j = 0; for (int i = 0; i < bucket.length; i++) { while (bucket[i]-- > 0) { arr[j++] = i; } } }
|
二、排序算法复杂度
三、对数器的使用
为了检测算法是否正确,自己的用例可能具有随机性,使用比较器来实现随机。另外在OJ中也可以自己控制随机数的范围。
/**
* 设置对数器,用来检验结果正确性
*/
// 1.先有一个绝对正确的方法, 一般是一个复杂度不高但是正确的方法
public static void comparator(int[] arr) {
Arrays.sort(arr);
}
// 2.实现一个随机样本产生器
/**
* @param maxSize 随机数组的最大长度, 例如传入10,产生[0,11)的随机数,对于int来说就是1-10
* @param maxValue 随机数组中元素的随机值的最大值
* @return 返回一个随机长度 并且元素也是随机的一个数组
*/
public static int[] generateRandomArray(int maxSize, int maxValue) {
int arr[] = new int[(int) ((maxSize + 1) * Math.random())];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) ((maxValue + 1) * Math.random());
}
return arr;
}
//3.实现一个比对方法
public static boolean isEqual(int arr1[],int arr2[]) {
if(arr1 == null && arr2 != null || arr1 != null && arr2 ==null) {
return false;
}
if(arr1 == null && arr2 == null) {
return true;
}
if(arr1.length != arr2.length) {
return false;
}
for(int i = 0; i < arr1.length; i++) {
if(arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
//比较很多次两个方法产生的结果 查看是否相同相同
public static void main(String[] args) {
int testTime = 10000; //测试次数
int maxSize = 30;//数组的最大尺寸
int maxValue = 100;//数组中元素的最大值
boolean succeed = true;
for (int i = 0; i < testTime; i++) {
int arr1[] = generateRandomArray(maxSize, maxValue);//产生随机数组
int arr2[] = arrayCopy(arr1);//拷贝数组,让这两个数组结构完全相同
bubbleSort(arr1); //需要测试的方法
comparator(arr2); //绝对正确的方法
if(!isEqual(arr1, arr2)) {
succeed = false;
break; //判断出不相同就调出循环
}
}
System.out.println(succeed ? "Succeed!" : "Error!");
int [] arr = generateRandomArray(maxSize, maxValue);
System.out.println(Arrays.toString(arr));
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
//拷贝数组
public static int[] arrayCopy(int[] arr) {
if(arr == null) {
return null;
}
int[] res = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
res[i] = arr[i];
}
return res;
}
四、比较器的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| public class Comparator { public static class IdAscendingComparator implements java.util.Comparator<Student>{
@Override public int compare(Student o1, Student o2) { if(o1.id < o2.id) { return -1; }else if(o1.id > o2.id){ return 1; }else { return 0; } } } public static void main(String[] args) { Student student1 = new Student("A", 1, 23); Student student2 = new Student("B", 2, 21); Student student3 = new Student("C", 3, 22); Student[] students = new Student[] { student3, student2, student1 }; System.out.println(Arrays.toString(students)); Arrays.sort(students, new IdAscendingComparator()); System.out.println(Arrays.toString(students)); } public static class Student { public String name; public int id; public int age; public Student(String name, int id, int age) { this.name = name; this.id = id; this.age = age; } @Override public String toString() { return "Student [name=" + name + ", id=" + id + ", age=" + age + "]"; } } }
|