排序算法分类
稳定与不稳定
内排序与外排序
排序方法分类
插入类排序:插入排序、希尔排序
交换类排序:冒泡排序、快速排序
选择类排序:选择排序、堆排序
归并排序
基数排序
排序算法代码实现
插入排序
public class HelloWorld {
public static void main(String []args) {
System.out.println("Hello World!");
int[] arr = {8,9,5,6,4,7,2,1};
insertSort(arr);
print(arr);
}
public static void print(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+",");
}
System.out.println("");
}
public static void insertSort(int[] arr){
if(arr.length>1){
for(int i=1;i<arr.length;i++){
int tmp = arr[i];
int j=i-1;
for(;j>=0 && arr[j]>tmp;j--){
arr[j+1] = arr[j];
}
arr[j+1] = tmp;
print(arr);
}
}
}
}
希尔排序
//希尔排序方法一
public static int[] shellSort(int[] arr){
int n = arr.length/2;
while(n>0){
for(int i=n;i<arr.length;i++){
for(int j=i;j-n>=0 && arr[j-n]>arr[j];j-=n){
int tmp = arr[j];
arr[j] = arr[j-n];
arr[j-n] = tmp;
}
}
n = n/2;
}
return arr;
}
//希尔排序方法二:子排序更像插入排序
public static int[] shellSort(int[] arr){
int n = arr.length/2;
while(n>0){
for(int i=n;i<arr.length;i++){
int tmp = arr[i];
int j = i;
while(j-n>=0 && arr[j-n]>tmp){
arr[j] = arr[j-n];
j -= n;
}
arr[j] = tmp;
}
n = n/2;
}
return arr;
}
冒泡排序
public static int[] bubbleSort(int[] arr){
for(int i=arr.length;i>0;i--){
for(int j=0;j+1<i;j++){
if(arr[j]>arr[j+1]){
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
outArr(arr);
}
return arr;
}
快速排序
//快速排序
public static void quicksort(int[] arr, int p, int q) {
if (p >= q) return;
int pivot = partion(arr, p, q);
quicksort(arr, p, pivot - 1); // 左边有序
quicksort(arr, pivot + 1, q); // 右边有序
}
public static int partion(int[] arr, int p, int q) {
int pivot = arr[q];
int pp = p;
int qq = q;
while (pp < qq) {
while (pp < qq && arr[pp] <= pivot) {
pp++;
}
if (pp < qq) {
int tmp = arr[pp];
arr[pp] = arr[qq];
arr[qq] = tmp;
}
while (pp < qq && arr[qq] >= pivot) {
qq--;
}
if (pp < qq) {
int tmp = arr[pp];
arr[pp] = arr[qq];
arr[qq] = tmp;
}
}
arr[pp] = pivot;
return pp;
}
选择排序
//选择排序 选择出最小的元素放到前面
public static int[] selectSort(int[] arr){
for(int i=0;i<arr.length;i++){
int min = i;
//注意这里的j从i+1开始
for(int j=i+1;j<arr.length;j++){
if(arr[j]<arr[min]){
min = j;
}
}
int tmp = arr[i];
arr[i] = arr[min];
arr[min] = tmp;
}
return arr;
}
堆排序
//堆排序
//借助数组构造二叉树
public class HelloWorld {
public static void main(String []args) {
int[] arr = {7,4,2,5,9,3,1,6};
int[] rst = heapSort(arr);
printArr(rst);
}
public static void printArr(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+",");
}
System.out.println();
}
//辅助函数
public static boolean more(int[] arr,int i,int j){
return arr[i]>arr[j]?true:false;
}
public static boolean less(int[] arr,int i,int j){
return arr[i]<arr[j]?true:false;
}
public static void exch(int[] arr,int i,int j){
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
//下沉操作
public static void skin(int[] arr,int k,int n){
while(2*k<=n){
int j = 2*k;
if(j<n && less(arr,j,j+1)){
j++;
}
if(!less(arr,k,j)){
break;
}
exch(arr,k,j);
k = j;
}
}
public static int[] heapSort(int[] a){
int n = a.length;
//建造一个可以表示二叉树的数组(构造堆)
int[] arr = new int[n+1];
for(int i=0;i<n;i++){
arr[i+1] = a[i];
}
//printArr(arr);
//使堆有序化
int k = n/2;
while(k>=1){
//k对应的arr[k]下沉
skin(arr,k,n);
k--;
//printArr(arr);
}
while(n>1){
exch(arr,1,n);
printArr(arr);
skin(arr,1,--n);
//printArr(arr);
}
return arr;
}
}
堆排序运行过程:
归并排序
public class HelloWorld {
public static void main(String []args) {
System.out.println("Hello World!");
int[] arr = {7,6,5,4,3,8,2,1};
System.out.print("start:");
print(arr);
mergeSort(arr,0,arr.length-1);
System.out.print("rst:");
print(arr);
}
public static void print(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+",");
}
System.out.println();
}
public static void merge(int[] arr,int start,int mid,int end){
int[] tmp = new int[end-start+1];
int i=start;
int j=mid+1;
int k=0;
while(i<=mid && j<=end){
if(arr[i]<=arr[j]){
tmp[k++] = arr[i++];
}else{
tmp[k++] = arr[j++];
}
}
while(i<=mid){
tmp[k++] = arr[i++];
}
while(j<=end){
tmp[k++] = arr[j++];
}
for(int v=0;v<tmp.length;v++){
arr[start+v] = tmp[v];
}
System.out.print("merge:");
print(arr);
}
public static void mergeSort(int[] arr,int start,int end){
int mid = (start+end)/2;
if(start<end){
mergeSort(arr,start,mid);
mergeSort(arr,mid+1,end);
merge(arr,start,mid,end);
}
}
}
排序结果:
Hello World!
start:7,6,5,4,3,8,2,1,
merge:6,7,5,4,3,8,2,1,
merge:6,7,4,5,3,8,2,1,
merge:4,5,6,7,3,8,2,1,
merge:4,5,6,7,3,8,2,1,
merge:4,5,6,7,3,8,1,2,
merge:4,5,6,7,1,2,3,8,
merge:1,2,3,4,5,6,7,8,
rst:1,2,3,4,5,6,7,8,