内部排序
①插入排序法
public static void insertSort(int[] array) {//插入排序
for (int i = 1; i < array.length; i++) {
//保证i前面有元素,可以进行交换
int tmp = array[i];
int j = i - 1;
//令j从i的前一个下标开始遍历,比较array[j]和保存的array[i]进行比较
for (; j >=0; j--) {
if (array[j] > tmp) {
array[j + 1] = array[j];
} else {
//如果一个数不满足比tmp大,说明前面都比tmp小
break;
}
}
//将tmp放入数组中
array[j + 1] = tmp;
}
System.out.println("插入排序: ");
System.out.println(Arrays.toString(array));
}
②希尔排序法
public static void shell(int[] array) {//希尔排序
int[]arr = {5,3,1};//进行分组
for (int i =0;i<arr.length;i++){
shellChild(array,arr[i]);
}
System.out.println("希尔排序: ");
System.out.println(Arrays.toString(array));
}
public static void shellChild(int[] array, int key) {//与插入排序思想类似
for (int i = key; i < array.length; i++) {
int tmp = array[i];
int j = i - key;
for (; j >= 0; j -= key) {
if (array[j] > tmp) {
array[j + key] = array[key];
} else {
break;
}
array[j + key] = tmp;
}
}
}
③ 选择排序
```java
public static void selectSort(int[] array){//选择排序
for (int i = 0;i<array.length-1;i++){
for (int j = i+1;j<array.length;j++){
if (array[j]<array[i]){
int tmp = array[j];
array[j] = array[i];
array[i] = tmp;
}
}
}
System.out.println("选择排序: ");
System.out.println(Arrays.toString(array));
}
④堆排序
//堆排序
public static void adjust(int[] array,int parent,int len){//建大堆的基础比较建堆
int child = parent*2+1;
while (child<len){
if (child+1<len&&array[child]<array[child+1]){
child++;
}
if (array[parent]<array[child]){
int num = array[parent];
array[parent] = array[child];
array[child] = num;
parent = child;
child = parent*2+1;
}
else {
break;
}
}
}
public static void createHeap(int[]array){//将大堆中每一个小堆进行排序
for (int parent = (array.length-1-1)/2;parent>=0;parent--) {
adjust(array, parent, array.length);
}
}
public static void HeapSort(int[]array){//将数组首元素和尾元素进行交换,重新进行大堆排序
createHeap(array);
int end = array.length-1;
while (end>0){
int tmp = array[0];
array[0] = array[end];
array[end] = tmp ;
adjust(array,0,end);
end--;
}
System.out.println(Arrays.toString(array));
}
⑤冒泡排序
public static void bubbleSort(int[]array){
for (int i = 0;i<array.length-1;i++){
for (int j=0;j<array.length-1;j++){
if (array[j]>array[j+1]){
int tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}
}
}
System.out.println("冒泡排序: ");
System.out.println(Arrays.toString(array));
}
⑥快排
public static void bubbleSort(int[]array){
for (int i = 0;i<array.length-1;i++){
for (int j=0;j<array.length-1;j++){
if (array[j]>array[j+1]){
int tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}
}
}
System.out.println("冒泡排序: ");
System.out.println(Arrays.toString(array));
}
⑦归并
public static void mergeSort(int[] array){
mergeSortRec(array,0,array.length-1);
System.out.println("归并");
System.out.println(Arrays.toString(array));
}
public static void mergeSortRec(int[] array,int low,int high){//完成递归
if (low>=high)return;
int mid = (low+high)/2;
mergeSortRec(array,low,mid);
mergeSortRec(array,mid+1,high);
merge(array,low,high,mid);
}
public static void merge(int[]array,int low,int high,int mid){
int[] elem = new int[high-low+1];
int k = 0;
int s1 = low;
int e1 = mid;
int s2 = mid+1;
int e2 = high;
while (s1<=e1&&s2<=e2){
if (array[s1]<=array[s2]){
elem[k] = array[s1];
s1++;
k++;
}else {
elem[k] = array[s2];
s2++;
k++;
}
}
while (s1<=e1){
elem[k]= array[s1];
s1++;
k++;
}
while (s2<=e2){
elem[k] = array[s2];
s2++;
k++;
}
for (int i = 0;i<elem.length;i++){
array[i+low] = elem[i];
}
}