1.插入排序
插入式排序屬於內部排序法,是對於欲排序的元素以插入的方式找尋該元素的適當位置,以達到排序的目的。
1.2.插入排序法思想
插入排序(Insertion Sorting)的基本思想是:把n個待排序的元素看成爲一個有序表和一個無序表,開始時有序表中只包含一個元素,無序表中包含有n-1個元素,排序過程中每次從無序表中取出第一個元素,把它的排序碼依次與有序表元素的排序碼進行比較,將它插入到有序表中的適當位置,使之成爲新的有序表。
1.3.插入排序思路圖
1.4.代碼實現
package cn.smallmartial.sort;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
/**
* @Author smallmartial
* @Date 2019/6/7
* @Email [email protected]
*/
public class InsertSort {
public static void main(String[] args) {
// int[] arr = {101,34,119,1};
int[] arr = new int[80000];
for (int i = 0; i < arr.length -1 ; i++) {
arr[i] = (int)(Math.random()*80000);
}
Date data = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dataStr = simpleDateFormat.format(data);
System.out.println("排序前的時間"+dataStr);
insertSort(arr);
Date data2 = new Date();
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String data2Str = simpleDateFormat.format(data2);
System.out.println("排序前的時間"+data2Str);
}
public static void insertSort(int[] arr){
for (int i = 1; i < arr.length; i++) {
int insertVal = arr[i];
int insertIndex = i - 1;
while (insertIndex >= 0 && insertVal <arr[insertIndex]){
arr[insertIndex +1] = arr[insertIndex];
insertIndex--;
}
arr[insertIndex + 1] = insertVal;
// System.out.println("第一輪插入:");
// System.out.println(Arrays.toString(arr));
}
// int insertVal = arr[1];
// int insertIndex = 1 - 1;
//
// while (insertIndex >= 0 && insertVal <arr[insertIndex]){
// arr[insertIndex +1] = arr[insertIndex];
// insertIndex--;
// }
// arr[insertIndex + 1] = insertVal;
// System.out.println("第一輪插入:");
// System.out.println(Arrays.toString(arr));
// //第二輪
// insertVal = arr[2];
// insertIndex = 2 - 1;
//
// while (insertIndex >= 0 && insertVal <arr[insertIndex]){
// arr[insertIndex +1] = arr[insertIndex];
// insertIndex--;
// }
// arr[insertIndex + 1] = insertVal;
// System.out.println("第二輪插入:");
// System.out.println(Arrays.toString(arr));
//
// //第3輪
// insertVal = arr[3];
// insertIndex = 3 - 1;
//
// while (insertIndex >= 0 && insertVal <arr[insertIndex]){
// arr[insertIndex +1] = arr[insertIndex];
// insertIndex--;
// }
// arr[insertIndex + 1] = insertVal;
// System.out.println("第三輪插入:");
// System.out.println(Arrays.toString(arr));
}
}
1.5運行結果
1.6簡單插入排序存在的問題
我們看簡單的插入排序可能存在的問題.
數組 arr = {2,3,4,5,6,1} 這時需要插入的數 1(最小), 這樣的過程是:
{2,3,4,5,6,6}
{2,3,4,5,5,6}
{2,3,4,4,5,6}
{2,3,3,4,5,6}
{2,2,3,4,5,6}
{1,2,3,4,5,6}
結論: 當需要插入的數是較小的數時,後移的次數明顯增多,對效率有影響.
2.希爾排序
2.1希爾排序法介紹
希爾排序是希爾(Donald Shell)於1959年提出的一種排序算法。希爾排序也是一種插入排序,它是簡單插入排序經過改進之後的一個更高效的版本,也稱爲縮小增量排序。
2.2希爾排序法基本思想
希爾排序是把記錄按下標的一定增量分組,對每組使用直接插入排序算法排序;隨着增量逐漸減少,每組包含的關鍵詞越來越多,當增量減至1時,整個文件恰被分成一組,算法便終止
2.3希爾排序示意圖
2.4代碼分析
package cn.smallmartial.sort;
import java.util.Arrays;
/**
* @Author smallmartial
* @Date 2019/6/7
* @Email [email protected]
*/
public class SherllSort {
public static void main(String[] args) {
int[] arr ={8,9,1,7,2,3,5,4,6,0};
shellSort(arr);
}
//逐步分析,交換法
public static void shellSort(int[] arr){
//
int temp = 0;
//第一輪排序
//將10個數據分成5組
for (int i = 5; i < arr.length; i++) {
//共5組 ,2個元素
for (int j = i - 5; j >= 0 ; j -= 5) {
if(arr[j]>arr[j+5]){
temp = arr[j];
arr[j] = arr[j+5];
arr[j+5] = temp;
}
}
}
System.out.println("第一輪結果;");
System.out.println(Arrays.toString(arr));
//第二輪,將5組分成 2組
for (int i = 2; i < arr.length; i++) {
//共5組 ,2個元素
for (int j = i - 2; j >= 0 ; j -= 2) {
if(arr[j]>arr[j+2]){
temp = arr[j];
arr[j] = arr[j+2];
arr[j+2] = temp;
}
}
}
System.out.println("第二輪結果;");
System.out.println(Arrays.toString(arr));
//第三輪,將2組分成 1組
for (int i = 1; i < arr.length; i++) {
//共5組 ,2個元素
for (int j = i - 1; j >= 0 ; j -= 1) {
if(arr[j]>arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println("第三輪結果;");
System.out.println(Arrays.toString(arr));
}
}
運行結果:
2.5代碼優化
int temp = 0;
int count = 0;
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]) {
temp = arr[j];
arr[j] = arr[j + gap];
arr[j + gap] = temp;
}
}
}
System.out.println("第"+(++count)+"輪結果;");
System.out.println(Arrays.toString(arr));
}
2.6 測試8萬數據
package cn.smallmartial.sort;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
/**
* @Author smallmartial
* @Date 2019/6/7
* @Email [email protected]
*/
public class SherllSort {
public static void main(String[] args) {
// int[] arr ={8,9,1,7,2,3,5,4,6,0};
int[] arr = new int[80000];
for (int i = 0; i < arr.length -1 ; i++) {
arr[i] = (int)(Math.random()*80000);
}
Date data = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dataStr = simpleDateFormat.format(data);
System.out.println("排序前的時間"+dataStr);
shellSort(arr);
Date data2 = new Date();
// SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String data2Str = simpleDateFormat.format(data2);
System.out.println("排序前的時間"+data2Str);
}
//逐步分析,交換法
public static void shellSort(int[] arr) {
//交換法
int temp = 0;
int count = 0;
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]) {
temp = arr[j];
arr[j] = arr[j + gap];
arr[j + gap] = temp;
}
}
}
// System.out.println("第"+(++count)+"輪結果;");
//System.out.println(Arrays.toString(arr));
}
}
}
運行結果
2.7移位法
public static void shellSort2(int[] arr){
//int temp = 0;
int count = 0;
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;
}
arr[j] = temp;
}
}
}
}
運行時間