插入排序( 稳定排序,时间复杂度为O(n^2) )
一堆数字,从左向右依次遍历,从第二个元素开始,与左边的一个元素相比较,插入合适的位置,然后继续遍历第三个元素,与左边两个元素比较并插入,然后遍历第四个,第五个... ...,一直到整个数组排序完成,示例图及代码如下:
package baseSort;
import java.util.Scanner;
public class DirectInsertSort {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int array[] = new int[10]; //定义一个数组
System.out.println("请输入要排序的数:");
for(int index = 0; index < 10; index++) { //用一个循环控制数组的元素输入
int i = sc.nextInt();
array[index] = i;
}
directInsertSort(array);
printArray(array);
}
public static void directInsertSort(int[] array) {
int i = 0;
int j = 0;
int t = 0;
for(i = 1; i < array.length; i++) { //从下标为1的第二个元素开始遍历
int tmp = array[i]; //建立临时变量,存储需要插入的元素值
for(j = 0; j < i && array[j] <= tmp; j++) { //找到应该插入的位置
}
for(t = i; t > j; t--) { //将插入位置后面的元素后移一位
array[t] = array[t - 1];
}
array[j] = tmp; //将元素值插入最终位置
}
}
public static void printArray(int[] array) { //打印数组元素的方法
for(int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.print("\n");
}
}
选择排序( 非稳定排序,时间复杂度为O(n^2) )
一堆数字,从下标为0开始找,找出最小的一个数,放在下标为0的位置,再从下标为1的位置开始找最小的数,放在下标为1的位置,再从下标为2的位置开始找,找出最小的数放在下标为2的位置上......,直至将数字完全排序,示例图及代码如下
package baseSort;
import java.util.Scanner;
public class DirectSelectSort {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int array[] = new int[10]; //定义一个数组
System.out.println("请输入要排序的数:");
for(int index = 0; index < 10; index++) { //用一个循环控制数组的元素输入
int i = sc.nextInt();
array[index] = i;
}
directSelectSort(array);
printArray(array);
}
public static void directSelectSort(int[] array) {
for(int i = 0; i < array.length - 1; i++) { //从下标为0开始选择
int minIndex = i; //将当前位置记为最小数的下标
for(int j = i + 1; j < array.length; j++) { //将当前位置的数与之后的数相比较
if(array[minIndex] > array[j]) { //如果当前小标的值比最小下标的值更小,则更改当前下标为最小下标
minIndex = j;
}
}
if(minIndex != i) { //遍历完之后,如果下标和初始下标不同,则表明初始下标的值不是最小值
int tmp = array[i]; //故交换两下标的值
array[i] = array[minIndex];
array[minIndex] = tmp;
}
}
}
public static void printArray(int[] array) { //打印数组元素的方法
for(int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.print("\n");
}
}
交换排序(冒泡排序)( 稳定排序,时间复杂度为O(n^2) )
从下标为0开始,相邻两元素进行比较,并交换为小在前,大在后,直至排序完成,如图中方法,以此类推,代码如下
package baseSort;
import java.util.Scanner;
public class DirectSwapSort {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int array[] = new int[10]; //定义一个数组
System.out.println("请输入要排序的数:");
for(int index = 0; index < 10; index++) { //用一个循环控制数组的元素输入
int i = sc.nextInt();
array[index] = i;
}
directSwapSort(array);
printArray(array);
}
public static void directSwapSort(int[] array) {
for(int i =0; i < array.length - 1; i++) { //一共length个元素,则需要比较length-1次
for(int j = 0; j < array.length - 1; j++) { //从下标为0开始向后比较
if(array[j] > array[j + 1]) { //如果前面的元素比后面的大,则值交换
int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;
}
}
}
}
public static void printArray(int[] array) { //打印数组元素的方法
for(int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.print("\n");
}
}