1.冒泡排序
public class A {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] array = {3,1,5,4,6,1,9,7,2,4};
//before
System.out.print("before: ");
printArray(array);
//sort
bubbleSort(array);
//after
System.out.print(" after: ");
printArray(array);
}
public static void bubbleSort(int[] array){
for(int i=0; i<array.length-1; i++){
for(int j=0; j<array.length-1-i; j++){
if(array[j]>array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
System.out.print(i+1 + "round: ");
printArray(array);
}
}
public static void printArray(int[] array) {
for(int i : array) {
System.out.print(i + " ");
}
System.out.println();
}
}
排序过程和结果:
2.快速排序
public class A {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] array = {3,1,5,4,6,1,9,7,2,4};
//before
System.out.print("before: ");
printArray(array);
//sort
quickSort(array, 0, array.length-1);
//after
System.out.print(" after: ");
printArray(array);
}
public static void quickSort(int[] array, int left, int right){
if(left>right) return;
int i, j;
i = left;
j = right;
int pivot = array[left];
while(i<j) {
while(i<j && pivot < array[j]) j--;
while(i<j && pivot >= array[i]) i++;//相等的话i也要向后移动
if(i<j) {
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
array[left] = array[i];
array[i] = pivot;
printArray(array);
quickSort(array, left, i-1);
quickSort(array, i+1, right);
}
public static void printArray(int[] array) {
for(int i : array) {
System.out.print(i + " ");
}
System.out.println();
}
}
排序过程和结果:
3. 直接插入
public class A {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] array = {3,1,5,4,6,1,9,7,2,4};
//before
System.out.print("before: ");
printArray(array);
//sort
insertSort(array);
//after
System.out.print(" after: ");
printArray(array);
}
public static void insertSort(int[] array){
if(array.length <= 1) return;
for(int i=0; i<array.length-1; i++) {
for(int j=i+1; j>0; j--) {
if(array[j-1] <= array[j])
break;
int temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
}
printArray(array);
}
}
public static void printArray(int[] array) {
for(int i : array) {
System.out.print(i + " ");
}
System.out.println();
}
}
排序过程和结果:
4.shell排序
这篇文章解释地很清楚:https://www.cnblogs.com/jsgnadsj/p/3458054.html
5. 选择排序
public class A {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] array = {3,1,5,4,6,1,9,7,2,4};
//before
System.out.print("before: ");
printArray(array);
//sort
selectSort(array);
//after
System.out.print(" after: ");
printArray(array);
}
public static void selectSort(int[] array) {
for(int i=0; i<array.length-1; i++) {
int minIndex = i;
for(int j=i+1; j<array.length; j++) {
if(array[j] < array[minIndex]) {
minIndex = j;
}
}
if(minIndex == i) continue;
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
printArray(array);
}
}
public static void printArray(int[] array) {
for(int i : array) {
System.out.print(i + " ");
}
System.out.println();
}
}
排序过程和结果:
6.堆排序
https://www.cnblogs.com/chengxiao/p/6129630.html
7.归并排序
https://www.cnblogs.com/chengxiao/p/6194356.html
8.基数排序
https://www.cnblogs.com/skywang12345/p/3603669.html