十大排序算法优化后-java实现=—带注释
package com.test.autimatic;
import java.util.Arrays;
public class Sort {
public static void main(String[] args) {
int[] arr = {21,3,4,2,1,22,21,26,7,8,6,7,51,5,23,33,35,36,87,88,0,4,5,6,6,5,5,7,7,7,980,1,9,19,0,88,66};
System.out.println(Arrays.toString(arr));
choiceSort(arr);
System.out.println(Arrays.toString(arr));
int[] arr1 = {21,3,4,2,1,22,21,26,7,8,6,7,51,5,23,33,35,36,87,88,0,4,5,6,6,5,5,7,7,7,980,1,9,19,0,88,66};
System.out.println(Arrays.toString(arr1));
insertSort(arr1);
System.out.println(Arrays.toString(arr1));
int[] arr2 = {21,3,4,2,1,22,21,26,7,8,6,7,51,5,23,33,35,36,87,88,0,4,5,6,6,5,5,7,7,7,980,1,9,19,0,88,66};
System.out.println(Arrays.toString(arr2));
xiErSort(arr2);
System.out.println(Arrays.toString(arr2));
int[] arr3 = {21,3,4,2,1,22,21,26,7,8,6,7,51,5,23,33,35,36,87,88,0,4,5,6,6,5,5,7,7,7,980,1,9,19,0,88,66};
System.out.println(Arrays.toString(arr3));
quickSort(arr3,0,arr3.length-1);
System.out.println(Arrays.toString(arr3));
int[] arr4 = {21,3,4,2,1,22,21,26,7,8,6,7,51,5,23,33,35,36,87,88,0,4,5,6,6,5,5,7,7,7,980,1,9,19,0,88,66};
System.out.println(Arrays.toString(arr4));
mergeSort(arr4,0,arr4.length-1,new int[arr4.length]);
System.out.println(Arrays.toString(arr4));
int[] arr5 = {21,3,4,2,1,22,21,26,7,8,6,7,51,5,23,33,35,36,87,88,0,4,5,6,6,5,5,7,7,7,980,1,9,19,0,88,66};
System.out.println(Arrays.toString(arr5));
heapSort(arr5);
System.out.println(Arrays.toString(arr5));
}
public static void choiceSort(int[] arr){
for (int i = 0; i < arr.length; i++) {
int t = i;
for (int j = i + 1; j < arr.length; j++) {
if(arr[j]<arr[t]){
t = j;
}
}
if(i != t){swap(arr,i,t);}
}
}
public static void insertSort(int[] arr){
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0 && arr[j] < arr[j-1]; j--) {
swap(arr,j,j-1);
}
}
}
public static void xiErSort(int[] arr){
for (int i = arr.length/2; i > 0 ; i/=2) {
for (int j = i; j < arr.length; j++) {
for (int k = j; k >= i && arr[k] < arr[k-i]; k-=i) {
swap(arr,k,k-i);
}
}
}
}
public static void quickSort(int[] arr,int i,int j){
if(i >= j){return;}
int t = arr[i];int start = i + 1;int mid = i;int end = j;
while (start<=end){
if(arr[start] < t){
swap(arr,start++,mid++);
}else if(arr[start] > t){
swap(arr, start, end--);
}else{
start++;
}
}
quickSort(arr, i, mid-1);
quickSort(arr, end+1, j);
}
public static void mergeSort(int[] arr,int start,int end,int[] temp){
if(start >= end){ return; }
int mid = start + (end - start)/2;
mergeSort(arr,start,mid,temp);
mergeSort(arr,mid+1,end,temp);
int k = start;int i = start;int j = mid+1;
System.arraycopy(arr,i,temp,i,end-start+1);
while (k <= end){
if(i > mid){
arr[k++] = temp[j++];
}else if(j > end){
arr[k++] = temp[i++];
}else if(temp[i] > temp[j]){
arr[k++] = temp[j++];
}else{
arr[k++] = temp[i++];
}
}
}
public static void heapSort(int[] arr){
for (int i = (arr.length/2)-1; i >= 0; i--) {
sink(arr,i,arr.length);
}
int n = arr.length - 1;
while (n >= 0){
swap(arr,0,n--);
sink(arr,0,n+1);
}
}
private static void sink(int[] arr,int n,int len){
while ((n*2+1)<len){
int t = n * 2 + 1;
if(t+1 < len && arr[t+1] > arr[t]){t++;}
if(arr[n] >= arr[t]){break;}
swap(arr,n,t);
n = t;
}
}
private static void swap(int[] arr,int a,int b){
int t = arr[a];
arr[a] = arr[b];
arr[b] = t;
}
}
|