移位法的核心思想
1、首先要找到插入的元素并保存
2、如果是希望实现递增排序,将原来要插入的元素与根据定义的步长元素去比较
3、比较时添加一个循环,条件为(与插入元素比较的元素下标不能越界,且比待插入元素大)为true便进去循环,循环体内为元素的移动
4、如果插入元素比要比较的元素大,也就是第3步跳出循环,则说明找到了插入的位置,直接插入
插入排序
1、使用while实现
// 插入排序
public static void insertSort(int arr[]) {
// 使用for循环进行简化
for (int i = 1; i < arr.length; i++) {
// 使用逐步推导的方式
// 第一轮{101,34,119,1} ===> {34,101,119,1}
// 定义待插入的数
int insertVal = arr[i];
int insertIndex = i - 1; // 即arr[1]的前面这个数的下标
// 给insertVal 找到插入的位置
// 1、insertIndex >= 0 保证再给insertVal 找插入位置,不越界
// 2、insertVal < arr[insertIndex] 待插入的数,还没有找到插入位置
// 3、就需要将arr[insertIndex]后移
while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
arr[insertIndex + 1] = arr[insertIndex]; // {101,34,119,1} ===>{101,101,119,1}
insertIndex--;
}
// 当退出while循环时,说明插入的位置找到,insertIndex+1
if (!(insertIndex + 1 == i)) {
// insertIndex+1==i 说明原地插入
arr[insertIndex + 1] = insertVal;
}
// System.out.printf("第%d轮插入后%s\n", i, Arrays.toString(arr));
}
}
2、使用双重for实现
public static void doubleFor(int arr[]) {
int j;
for (int i = 1; i < arr.length; i++) {
int insertVal = arr[i]; // 注意啊,这里一定要保存值,而不是保存索引下标
for (j = i - 1; j >= 0 && arr[j] > insertVal; j--) {
arr[j + 1] = arr[j];
}
if (!((j+1)==i)){
arr[j+1] = insertVal;
}
System.out.printf("第%d躺插入排序:%s\n", i, Arrays.toString(arr));
}
}
3、速度测试
8w个数据进行递增排序
public static void main(String[] args) {
int arr[] = new int[80000];
for (int i = 0; i < 80000; i++) {
arr[i] = (int) (Math.random() * 800000);
}
long start = System.currentTimeMillis();
InsertSort.doubleFor(arr);
long end = System.currentTimeMillis();
System.out.printf("插入排序花费了%d毫秒", end - start);
}
希尔排序
1、插入时采用交换法
public static void shellSortTest2(int[] arr) {
// 设置步长
for (int gap = arr.length / 2; gap > 0; gap /= 2) {
// 根据步长进行分组
for (int i = gap; i < arr.length; i++) {
// 根据步长找到值并比较
for (int j = i - gap; j >= 0; j -= gap) {
if (arr[j] > arr[j + gap]) {
int temp = arr[j];
arr[j] = arr[j + gap];
arr[j + gap] = temp;
}
}
}
}
// System.out.println("希尔排序后:" + Arrays.toString(arr));
}
8w个数据进行测试
int arr[] = new int[80000];
for (int i = 0; i < 80000; i++) {
arr[i] = (int) (Math.random() * 80000);
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long start =System.currentTimeMillis();
ShellSort.shellSortTest2(arr);
long end =System.currentTimeMillis();
System.out.printf("共耗时:%d秒", TimeUnit.MILLISECONDS.toSeconds(end - start));
2、采用移位法
public static void shellSort(int[] arr) {
// 设置步长,并逐渐减少增量
for (int gap = arr.length / 2; gap > 0; gap /= 2) {
// 从第gap个元素,逐个对其所在的组进行直接插入排序
for (int i = gap; i < arr.length; i++) {
int j = i;
int temp = arr[j];
if (arr[j] < arr[j - gap]) {
while (j - gap >= 0 && temp < arr[j - gap]) {
// 移动
arr[j] = arr[j - gap];
j -= gap;
}
// 当退出while后,就给temp找到插入的位置了
arr[j] = temp;
}
}
}
}
使用800w的数据进行测试
int arr[] = new int[8000000];
for (int i = 0; i < 8000000; i++) {
arr[i] = (int) (Math.random() * 80000);
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long start =System.currentTimeMillis();
ShellSort.shellSort(arr);
long end = System.currentTimeMillis();
System.out.printf("共耗时:%d秒", TimeUnit.MILLISECONDS.toSeconds(end - start));
总结:
从两个实现希尔排序的算法中可以看出,采用移位法后效率的提升是显而易见的,移位法避免了频繁的交换数据,跟选择排序比较完在交换的思想是类似的,而冒泡排序是比较并交换这也是选择排序跟冒泡排序性能上有差别的原因。