Java排序总结
测试的数据
public static Integer[] sort = new Integer[]{0, 3, 2, 1, 9, 8, 5, 6, 7, 4};
1. 冒泡排序
大小比较函数
public static void rightBig(Integer[] sort, int left, int right) {
if (sort[left] > sort[right]) {
Integer temp = sort[left];
sort[left] = sort[right];
sort[right] = temp;
}
}
实现:
/**
* 冒泡是 左右相邻比较
* <p>
* 时间复杂度 O(n²) 。
*
* @param args
*/
public static void main(String[] args) {
//second:i中的sort.length-1 是因为如果10个数 9个大数已经冒泡上去了那么最后一个就是最小的 不需要再排序了
for (int i = 0; i < sort.length - 1; i++) {
//first:从内层循环分析
//10个数 相邻的两个数 进行比较 循环一次 则把最大的数冒泡到右边;
//sort.length - 1是防止 数组越界 最后一个不需要后一个数作对比
for (int j = 0; j < (sort.length - 1 - i); j++) {
rightBig(sort, j, j + 1);
}
}
System.out.println("value:" + Arrays.asList(sort));
}
2. 选择排序
实现:
/**
* 选择排序。 一个位置 L与所有的位置All_L,一次进行对比 和交换。一轮对比完,那个位置的值既最小
* <p>
* 时间复杂度 O(n²) 。 (n-1)次循环*(n)次循环
*
* @param args
*/
public static void main(String[] args) {
//第一层循环确定 位置L
for (int i = 0; i < sort.length - 1; i++) {
//All_L 的的循环。需要在位置L的后边开始
for (int j = i + 1; j < sort.length; j++) {
rightBig(sort, i, j);
}
}
System.out.println("value:" + Arrays.asList(sort));
}
3. 插入排序
实现:
public static void insertsort(Integer arr[]) {
for (int i = 1; i < arr.length; i++) {
if (arr[i - 1] > arr[i]) {//注意[0,i-1]都是有序的。如果待插入元素比arr[i-1]还大则无需再与[i-1]前面的元素进行比较了,反之则进入if语句
int temp = arr[i];
int j;
for (j = i - 1; j >= 0 && arr[j] > temp; j--) {
arr[j + 1] = arr[j];//把比temp大或相等的元素全部往后移动一个位置
}
arr[j + 1] = temp;//把待排序的元素temp插入腾出位置的(j+1)
}
}
}
public static void main(String[] args) {
System.out.println("排序之前:" + Arrays.asList(sort));
insertsort(sort);
System.out.println("排序之后:" + Arrays.asList(sort));
}
4.希尔排序
实现:
/**
* 希尔排序
*
* @param arrays 需要排序的序列
*/
public static void sort(Integer[] arrays) {
if (arrays == null || arrays.length <= 1) {
return;
}
//增量
int incrementNum = arrays.length / 2;
while (incrementNum >= 1) {
for (int i = 0; i < arrays.length; i++) {
//进行插入排序
for (int j = i; j < arrays.length - incrementNum; j = j + incrementNum) {
if (arrays[j] > arrays[j + incrementNum]) {
int temple = arrays[j];
arrays[j] = arrays[j + incrementNum];
arrays[j + incrementNum] = temple;
}
}
}
//设置新的增量
incrementNum = incrementNum / 2;
}
}
public static void main(String[] args) {
sort(sort);
System.out.println("value:" + Arrays.asList(sort));
}
5.快速排序
实现:
public static void printArray(int[] array) {
System.out.print("{");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
if (i < array.length - 1) {
System.out.print(", ");
}
}
System.out.println("}");
}
public static void quickSort(int a[], int start, int end) {
int i, j;
i = start;
j = end;
if ((a == null) || (a.length <= 1))
return;
while (i < j) {//查找基准点下标
while (i < j && a[j] > a[i])
j--;
if (i < j) { // 右侧扫描,找出第一个比key小的,交换位置
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
while (i < j && a[i] < a[j])
// 左侧扫描(此时a[j]中存储着key值)
i++;
if (i < j) { // 找出第一个比key大的,交换位置
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
if (i - start > 1) { // 递归调用,把key前面的完成排序
quickSort(a, 0, i - 1);
}
if (end - j > 1) {
quickSort(a, j + 1, end); // 递归调用,把key后面的完成排序
}
}
public static void main(String[] args) {
int[] array = {-7,-1,9, 6, 5, 4, 1, 0, 8, 7, 3, 2, -2, -3};
System.out.print("Before sort:");
printArray(array);
quickSort(array, 0, array.length - 1);
System.out.print("After sort:");
printArray(array);
}
6.二分查找(折半插入查找)
实现:
public static void main(String[] args) {
int[] a = {4, 2, 1, 6, 3, 6, 0, -5, 1, 1};
int i, j;
int low, high, mid;
int temp;
for (i = 1; i < a.length; i++) {
temp = a[i];
low = 0;
high = i - 1;
//定位 前面那些位置 那个位置应该是temp应该在的位置。
while (low <= high) {
//临界条件是 low<=temp<=high low和high相连的时候。
//既 low>high的时候 over了
//low high 相邻的时候 low=mid+1; low==high;->a[mid=high]>temp->high=high-1. 所以high再+1.找到temp放入的位置。
// low>high的时候 那么high+1 的位置就是 temp应该呆的位置。
//一半的一半的 和 temp对比。
mid = (low + high) / 2;
if (a[mid] > temp)
high = mid - 1;
else
low = mid + 1;
}
//定位的位置都向后挪动一个位置
for (j = i - 1; j > high; j--)
a[j + 1] = a[j];
//temp弄进来即可。
a[high + 1] = temp;
}
for (i = 0; i < 10; i++) {
System.out.printf("%d ", a[i]);
}
}