包含:插入排序,选择排序,冒泡排序,快速排序,归并排序
class Solution {
public static void main(String[] args) {
int[] arr = {3,6,7,3,1,6,2,5,5,4};
//insertSort(arr);
//bubbleSort(arr);
//selectSort(arr);
quickSort(arr, 0, arr.length-1);
//mergeSort(arr, 0, arr.length-1);
for(int i=0; i<arr.length; i++) {
System.out.println(arr[i]);
}
}
/*private static void insertSort(int[] arr) {
for(int i=1; i<arr.length; i++) {
int start = arr[i];
int j;
for(j=i-1; j>=0 && arr[j]>start; j--) {
arr[j+1] = arr[j];
}
arr[j+1] = start;
}
}*/
/*
private static void bubbleSort(int[] arr) {
for(int i=0; i<arr.length-1; i++) {
boolean flag = true;
for(int j=0; j<arr.length-i-1; j++) {
if(arr[j] > arr[j+1] ) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag = false;
}
}
if(flag) {
break;
}
}
}*/
/*private static void selectSort(int[] arr) {
for(int i=0; i<arr.length-1; i++) {
int k = i;
for(int j=i+1; j<arr.length; j++) {
if(arr[j] < arr[k]) {
k = j;
}
}
if(arr[k] != arr[i]) {
int temp = arr[k];
arr[k] = arr[i];
arr[i] = temp;
}
}
}*/
/*private static void quickSort(int[] arr, int top, int bottom) {
if(top >= bottom) {
return;
}
int comp = arr[top];
int i = top;
int j = bottom;
int temp;
while(i < j) {
while(i < j && arr[j] >= comp) {
j --;
}
arr[i] = arr[j];
while(i < j && arr[i] < comp) {
i ++;
}
arr[j] = arr[i];
}
arr[i] = comp;
quickSort(arr, top, j-1);
quickSort(arr, j+1, bottom);
}*/
private static void quickSort(int[] arr, int top, int bottom) {
if(top >= bottom) {
return;
}
int comp = arr[top];
int i = top;
int j = bottom;
int temp;
while(i < j) {
while(i < j && arr[j] >= comp) {
j --;
}
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
while(i < j && arr[i] < comp) {
i ++;
}
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
quickSort(arr, top, j-1);
quickSort(arr, j+1, bottom);
}
/*private static void mergeSort(int[] arr, int top, int bottom) {
if(top == bottom) {
return;
}
int mid = (top + bottom) / 2;
mergeSort(arr, top, mid);
mergeSort(arr, mid+1, bottom);
merge(arr, top, mid, bottom);
}
private static void merge(int[] arr, int top, int mid, int bottom) {
int len = bottom - top + 1;
int[] tempArr = new int[len];
int j = top, k = mid+1;
int i = 0;
for(;j<=mid && k<=bottom;) {
if(arr[j] <= arr[k]) {
tempArr[i] = arr[j];
j ++;
i ++;
} else {
tempArr[i] = arr[k];
k ++;
i ++;
}
}
if(j == mid + 1) {
for(int l=k; l<=bottom; l++) {
tempArr[i ++] = arr[l];
}
} else {
for(int l=j; l<=mid; l++) {
tempArr[i ++] = arr[l];
}
}
for(i=0,j=top; i<len; i++,j++) {
arr[j] = tempArr[i];
}
}*/
}