排序大专题
O(n^2) 级的排序算法
冒泡排序
BubbleSort类
package com.allSort.BubbleSort;
public class BubbleSort {
public static void bubblesort(int a[]) {
for (int i = 1; i <= a.length; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] < a[j + 1]) {//这样结果是升序,
//如果要降序,就用“>”号即可
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
}
test类(用来测试BubbleSort类当中的冒泡排序算法的)
package com.allSort.BubbleSort;
import java.util.Arrays;
public class test {
public static void main(String[] args) {
int[] array = new int[]{55, 33, 22, 66, 11};
//输出排序前的array数组
System.out.print("排序前:");
System.out.println(Arrays.toString(array));
//调用BubbleSort类中的sort方法对array数组进行排序
BubbleSort.bubblesort(array);
//输出冒泡排序后的array数组
System.out.print("排序后:");
System.out.println(Arrays.toString(array));
}
}
选择排序
选择排序(SelecSort)是一种简单直观的排序算法。它的工作原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。
SelectSort类
package com.allSort.SelectSort;
public class SelectSort {
public static void selectSort(int a[]){
for (int i = 0; i < a.length; i++) {
int index = i;//最小值的下标的索引
for (int j = i + 1; j < a.length ; j++) {
if(a[j] < a[index]){
index = j;
}
}
int temp = a[i];
a[i] = a[index];
a[index] = temp;
}
}
}
test类(用来测试SelectSort类当中的选择排序算法的)
package com.allSort.SelectSort;
import com.allSort.BubbleSort.BubbleSort;
import java.util.Arrays;
public class test {
public static void main(String[] args) {
int[] array = new int[]{55, 33, 22, 66, 11};
//输出排序前的array数组
System.out.print("排序前:");
System.out.println(Arrays.toString(array));
//调用SelectSort类中的selectSort方法对array数组进行排序
SelectSort.selectSort(array);
//输出排序选择后的array数组
System.out.print("排序后:");
System.out.println(Arrays.toString(array));
}
}
插入排序
InsertSort 类
package com.allSort.InsertSort;
public class InsertSort {
public static void insertSort(int a[]) {
if(a.length < 2 || a == null){
return;
}
for (int i = 1; i < a.length; i++) {//i表示未排序的第一个元素
for (int j = i - 1; j >= 0; j--) {//j代表已排序的最后一个元素
if (a[j] > a[j + 1]) {
swap(a, j, j + 1);
}
}
}
}
//i、j如果是同一个位置的话会出错
public static void swap(int a[], int i, int j) {
a[i] = a[i] ^ a[j];
a[j] = a[i] ^ a[j];
a[i] = a[i] ^ a[j];
}
}
test类(用来测试InsertSort类当中的插入排序算法的)
package com.allSort.InsertSort;
import java.util.Arrays;
public class test {
public static void main(String[] args) {
int[] array={10,6,9,3,5};
InsertSort.insertSort(array);
System.out.println(Arrays.toString(array));
}
}
O(nlogn) 级的排序算法
归并排序
MergeSort类
package com.allSort.MergeSort;
public class MergeSort {
public static void mergesort(int a[],int L,int R){
if(L == R){
return;
}
int mid = L + ((R - L) >> 1);
mergesort(a,L,mid);
mergesort(a,mid + 1,R);
Merge(a,L,mid,R);
}
public static void Merge(int a[],int L,int mid,int R){
int [] help = new int[R - L + 1];
int i = 0;
int p1 = L;
int p2 = mid + 1;
while(p1 <= mid && p2 <= R){
help[i++] = a[p1] <= a[p2] ? a[p1++] : a[p2++];
}
while (p1 <= mid){
help[i++] = a[p1++];
}
while(p2 <= R){
help[i++] = a[p2++];
}
for (int j = 0; j < help.length; j++) {
a[L + j] = help[j];
}
}
}
test类(用来测试MergeSort类当中的归并排序算法的)
package com.allSort.MergeSort;
import com.allSort.InsertSort.InsertSort;
import java.util.Arrays;
public class test {
public static void main(String[] args) {
int[] array={10,6,9,3,5};
MergeSort.mergesort(array,0,4);
System.out.println(Arrays.toString(array));
}
}
快速排序(3.0版本)
QuickSort类
package com.allSort.QuickSort;
public class QuickSort {
public static void quickSort(int a[], int L, int R) {
if (L < R) {
swap(a, L + (int) (Math.random()) * (R - L + 1), R);
int[] p = partition(a, L, R);
quickSort(a, L, p[0] - 1);
quickSort(a, p[1] + 1, R);
}
}
//这个最终返回的是一个长度为2的数组
//这个数组的含义是,p[0]是与切分值相等的区域的左边界,p[1]是与切分值相等的区域的右边界
public static int[] partition(int[] a, int L, int R) {
int less = L - 1; // <区的右边界
int more = R + 1;// >区的左边界
while (L < more) {//L表示当前数的位置 ,arr[R]表示划分值
if (a[L] < a[R]) {
swap(a, ++less, L++);
} else if (a[L] > a[R]) {
swap(a, --more, L);
} else {
L++;
}
}
return new int [] {less + 1,more-1};
}
public static void swap(int a[], int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
test 类(用来测试QuickSort类当中的快速排序算法的)
package com.allSort.QuickSort;
import com.allSort.MergeSort.MergeSort;
import java.util.Arrays;
public class test {
public static void main(String[] args) {
int[] array={4,3,5,6,5,0,1,7,8,5};
QuickSort.quickSort(array,0,array.length-1);
System.out.println(Arrays.toString(array));
}
}