插入排序
package Algorithm;
public class InsertSort {
private static int[] insertSort(int[] array) {
for(int i=1; i<array.length; i++) {
for(int j=i; j>0; j--) {
if(array[j] < array[j-1]) {
int tmp = array[j];
array[j] = array[j-1];
array[j-1] = tmp;
}
}
}
return array;
}
public static void main(String[] args) {
int[] array = {1, 1, 4, 5, 1, 4, 8, 1, 0};
int[] result = insertSort(array);
for (int value : result) {
System.out.print(value + " ");
}
}
}
快速排序
package Algorithm;
public class QuickSort {
public static void quickSort(int[] num, int left, int right) {
if(left >= right) return;
int key = num[left];
int i = left, j = right;
while(i < j) {
while (num[j] >= key && i < j) {
j--;
}
while (num[i] <= key && i < j) {
i++;
}
if(i < j) {
int tmp = num[i];
num[i] = num[j];
num[j] = tmp;
}
}
num[left] = num[i];
num[i] = key;
quickSort(num, left, i-1);
quickSort(num, i+1, right);
}
public static void main(String[] args) {
int[] num = {1, 5, 6, 3, 4, 1, 4, 5};
quickSort(num, 0, num.length-1);
for(int i : num) {
System.out.print(i + " ");
}
}
}