希尔排序:
public class XiErSort {
public static void main(String[] args) {
int[] arr = {1, 4, 5, 6, 7, 9, 8, 0, 2, 3, -9};
sortMethod2(arr);
System.out.println(Arrays.toString(arr));
}
public static void sortMethod(int[] arr) {
int temp = 0;
for (int grap = arr.length / 2; grap > 0; grap /= 2) {
for (int i = grap; i < arr.length; i++) {
for (int j = i - grap; j >= 0; j -= grap) {
if (arr[j] > arr[j + grap]) {
temp = arr[j];
arr[j] = arr[j + grap];
arr[j + grap] = temp;
}
}
}
}
}
public static void sortMethod2(int[] arr) {
for (int grep = arr.length / 2; grep > 0; grep /= 2) {
for (int j = grep; j < arr.length - 1; j++) {
int temp = arr[j];
while (j - grep >= 0 && arr[j] > arr[j - grep]) {
arr[j] = arr[j - grep];
j = j - grep;
}
arr[j] = temp;
}
}
}
}
冒泡排序:
public class BubbleSort {
public static void main(String[] args) {
int arr[] = {-1, 2, 4, -3, 5, 0};
sortbubble(arr);
System.out.println(Arrays.toString(arr));
}
private static void sortbubble(int[] arr) {
int temp=0;
boolean flag=false;
for (int i=0;i<arr.length-1;i++){
for (int j=0;j<arr.length-1-i;j++){
if (arr[j]<arr[j+1]){
flag=true;
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
if (!flag){
break;
}else{
flag=false;
}
}
}
}
选择排序:
public class ChooseSort {
public static void main(String[] args) {
int[] arr = {1, 4, 6, 2, 7, 3};
sortMethod(arr);
System.out.println(Arrays.toString(arr));
}
public static void sortMethod(int[] arr) {
for (int i=0;i<arr.length-1;i++){
int min=arr[i];
int minIndex=i;
for (int j=i+1;j<arr.length;j++){
if (min>arr[j]){
min=arr[j];
minIndex=j;
}
}
if (minIndex!=i) {
arr[minIndex] = arr[i];
arr[i]=min;
}
}
}
}
插入排序:
public class InsertSort {
public static void main(String[] args) {
int[] arr = {1, 4, 9, 7, 3, 6, 2};
SortMethod(arr);
System.out.println(Arrays.toString(arr));
}
public static void SortMethod(int[] arr) {
int insertVal=0;
int insertIndex=0;
for (int i=0;i<arr.length-1;i++){
insertVal=arr[i+1];
insertIndex=i;
while (insertIndex>=0 && insertVal>arr[insertIndex]){
arr[insertIndex+1]=arr[insertIndex];
insertIndex--;
}
arr[insertIndex+1]=insertVal;
}
}
}
快速排序:
public class Bubble_Quick {
public static void main(String[] args) {
int[] arr = new int[8000000];
for (int i = 0; i < 8000000; i++) {
arr[i] = (int) (Math.random() * 8000000);
}
System.out.println("排序前");
Date data1 = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1Str = simpleDateFormat.format(data1);
System.out.println("排序前的时间是=" + date1Str);
quickSort(arr, 0, arr.length-1);
Date data2 = new Date();
String date2Str = simpleDateFormat.format(data2);
System.out.println("排序前的时间是=" + date2Str);
}
public static void quickSort(int[] arr,int left, int right) {
int l = left;
int r = right;
int pivot = arr[(left + right) / 2];
int temp = 0;
while( l < r) {
while( arr[l] < pivot) {
l += 1;
}
while(arr[r] > pivot) {
r -= 1;
}
if( l >= r) {
break;
}
temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
if(arr[l] == pivot) {
r -= 1;
}
if(arr[r] == pivot) {
l += 1;
}
}
if (l == r) {
l += 1;
r -= 1;
}
if(left < r) {
quickSort(arr, left, r);
}
if(right > l) {
quickSort(arr, l, right);
}
}
}