熟能生巧!
冒泡、选择、插入
public class OrdinarySortRepeat {
public static void main(String[] args) {
int[] arr = getRandomInts(10);
int[] arr2 = Arrays.copyOf(arr,10);
int[] arr3 = Arrays.copyOf(arr,10);
int[] arr4 = Arrays.copyOf(arr,10);
System.out.println("origin:"+Arrays.toString(arr));
bubbleSort(arr);
System.out.println("bubbleSort:"+Arrays.toString(arr));
selectSort(arr2);
System.out.println("selectSort:"+Arrays.toString(arr2));
insertSort(arr3);
System.out.println("insertSort:"+Arrays.toString(arr3));
insertSort2(arr4);
System.out.println("insertSort2:"+Arrays.toString(arr4));
}
public static int[] getRandomInts(int length){
int[] numbers = new int[length];
Random random = new Random();
for (int i = 0; i < length; i++) {
numbers[i] = random.nextInt(9999);
}
return numbers;
}
public static void swap(int[] arr, int a, int b){
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
public static void bubbleSort(int[] arr){
for(int i = 0; i < arr.length-1; i++){
boolean isNoSorted = true;
for(int j = 0; j < arr.length-i-1; j++){
if(arr[j] > arr[j+1]){
swap(arr,j,j+1);
isNoSorted = false;
}
}
if(isNoSorted){
return;
}
}
}
public static void selectSort(int[] arr){
for(int i = 0; i < arr.length-1; i++){
for(int j = i+1; j < arr.length;j++){
if(arr[i] > arr[j]){
swap(arr,i,j);
}
}
}
}
public static void insertSort(int[] arr){
for(int i = 1; i < arr.length; i++){
int temp = arr[i];
int j = i-1;
while(j >= 0 && arr[j] > temp){
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
public static void insertSort2(int[] arr){
for(int i = 1;i < arr.length; i++){
int temp = arr[i];
int j;
for(j = i-1; j >= 0; j--){
if(arr[j] > temp){
arr[j+1] = arr[j];
}else{
break;
}
}
arr[j+1] = temp;
}
}
}
快速排序
public class QuickSortRepeat {
public static void main(String[] args) {
int[] arr = getRandomInts(10);
int[] arr2 = Arrays.copyOf(arr,10);
int[] arr3 = Arrays.copyOf(arr,10);
int[] arr4 = Arrays.copyOf(arr,10);
System.out.println("origin:"+Arrays.toString(arr));
quickSort(arr,0,arr.length-1);
System.out.println("quickSort:"+Arrays.toString(arr));
quickSort2(arr2,0,arr.length-1);
System.out.println("quickSort2:"+ Arrays.toString(arr2));
quickSort3(arr3,0,arr.length-1);
System.out.println("quickSort3:"+Arrays.toString(arr3));
quickSort4(arr4,0,arr.length-1);
System.out.println("quickSort4:"+Arrays.toString(arr4));
}
public static int[] getRandomInts(int length){
int[] numbers = new int[length];
Random random = new Random();
for (int i = 0; i < length; i++) {
numbers[i] = random.nextInt(9999);
}
return numbers;
}
public static void swap(int[] arr, int a, int b){
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
/**
* 根据阮一峰博客:http://www.ruanyifeng.com/blog/2011/04/quicksort_in_javascript.html
* @param arr
* @param left
* @param right
*/
public static void quickSort(int[] arr, int left, int right){
if(left >= right){
return;
}
int standard = arr[left];
int index = left;
for(int i = left+1; i <= right; i++){
if(arr[i] < standard){
arr[index] = arr[i];
for(int j = i-1; j > index;j--){
arr[j+1] = arr[j];
}
arr[++index] = standard;
}
}
quickSort(arr,left,index-1);
quickSort(arr,index+1,right);
}
/**
* 根据马桶上看算法:http://developer.51cto.com/art/201403/430986.htm
* @param arr
* @param left
* @param right
*/
public static void quickSort2(int[] arr,int left, int right){
if(left >= right){
return;
}
int standard = arr[left];
int i = left;
int j = right;
while(i < j){
while(i < j && arr[j] >= standard){
j--;
}
while(i < j && arr[i] <= standard){
i++;
}
if(i < j){
swap(arr,i,j);
}else{
break;
}
}
if(i != left){
arr[left] = arr[i];
arr[i] = standard;
}
quickSort2(arr,left,i-1);
quickSort2(arr,i+1,right);
}
/**
* 根据博客园:https://www.cnblogs.com/coderising/p/5708801.html
* @param arr
* @param left
* @param right
*/
public static void quickSort3(int[] arr, int left, int right){
if(left >= right){
return;
}
int standard = arr[left];
int i = left;
int j = right;
while(i < j){
while(i < j && arr[j] >= standard){
j--;
}
arr[i] = arr[j];
while(i < j && arr[i] <= standard){
i++;
}
arr[j] = arr[i];
}
arr[i] = standard;
quickSort3(arr,left,i-1);
quickSort3(arr,i+1,right);
}
/**
* 同上,对quickSort3进行优化(三数取中),也是根据博客园:https://www.cnblogs.com/coderising/p/5708801.html
* @param arr
* @param left
* @param right
*/
public static void quickSort4(int[] arr, int left, int right){
if(left >= right){
return;
}
int mid = (left+right)/2;
if(arr[right] < arr[left]){
swap(arr,left,right);
}
if(arr[right] < arr[mid]){
swap(arr,mid,right);
}
if(arr[left] < arr[mid]){
swap(arr,left,mid);
}
int standard = arr[left];
int i = left;
int j = right;
while(i < j){
while(i < j && arr[j] >= standard){
j--;
}
arr[i] = arr[j];
while(i < j && arr[i] <= standard){
i++;
}
arr[j] = arr[i];
}
arr[i] = standard;
quickSort3(arr,left,i-1);
quickSort3(arr,i+1,right);
}
}