8種排序之間的關係
選擇排序
原理
(1)基本思想
在要排序的一組數中,選出最小的一個數與第一個位置的數交換;
然後在剩下的數當中再找最小的與第二個位置的數交換,如此循環到倒數第二個數和最後一個數比較爲止。
(2)實例
實現
public class Sort {
public static void main(String[] args) {
int [] value = {12,34,56,1,2,4};
System.out.println("排序前");
print(value);
selectSort(value);
System.out.println("排序後");
print(value);
}
/**
* 選擇排序
* 選出最大的元素與首個元素進行交換
* **/
public static void selectSort(int [] value){
for(int i = 0;i<value.length-1;i++){
//最小索引
int index = i;
//最小值
int min = value[i];
for(int j = i+1;j<value.length;j++){
//選出最小元素
if(value[j]<min) {
index = j;
min = value[j];
}
}
//對元素進行交換
swap(value,i,index);
System.out.print("第"+(i+1)+"次排序後的結果爲");
print(value);
}
}
public static void swap(int [] value,int i,int j){
int temp = value[i];
value[i] = value[j];
value[j] = temp;
}
public static void print(int [] value){
for(int number:value){
System.out.print(number+" ");
}
System.out.println();
}
}
結果
排序前
12 34 56 1 2 4
第1次排序後的結果爲1 34 56 12 2 4
第2次排序後的結果爲1 2 56 12 34 4
第3次排序後的結果爲1 2 4 12 34 56
第4次排序後的結果爲1 2 4 12 34 56
第5次排序後的結果爲1 2 4 12 34 56
排序後
1 2 4 12 34 56
冒泡排序
原理
(1)基本思想
在要排序的一組數中,對當前還未排好序的範圍內的全部數,自上而下對相鄰的兩個數依次進行比較和調整,讓較大的數往下沉,較小的往上冒。即:每當兩相鄰的數比較後發現它們的排序與排序要求相反時,就將它們互換。
(2)實例
實現
/**
* Created by dx on 2017/8/17.
*/
/**
* 冒泡排序
* **/
public class Sort {
public static void main(String[] args) {
int [] value = {12,34,56,1,2,4};
System.out.println("排序前");
print(value);
bubbleSort(value);
System.out.println("排序後");
print(value);
}
/**
* 冒泡排序
* 依次找出最大的元素,當後面的元素大於前面的元素,則進行交換
* **/
public static void bubbleSort(int [] value){
for(int i = value.length-1;i>=1;i--){
for(int j = 0;j<i;j++){
if(value[j]>value[j+1])
swap(value,j,j+1);
}
System.out.print("第"+(value.length-i)+"次排序的結果爲");
print(value);
}
}
public static void swap(int [] value,int i,int j){
int temp = value[i];
value[i] = value[j];
value[j] = temp;
}
public static void print(int [] value){
for(int number:value){
System.out.print(number+" ");
}
System.out.println();
}
}
結果
排序前
12 34 56 1 2 4
第1次排序的結果爲12 34 1 2 4 56
第2次排序的結果爲12 1 2 4 34 56
第3次排序的結果爲1 2 4 12 34 56
第4次排序的結果爲1 2 4 12 34 56
第5次排序的結果爲1 2 4 12 34 56
排序後
1 2 4 12 34 56
插入排序
原理
(1)基本思想
在要排序的一組數中,假設前面(n-1)[n>=2] 個數已經是排好順序的,現在要把第n個數插到前面的有序數中,使得這n個數
也是排好順序的。如此反覆循環,直到全部排好順序。
(2)實例
實現
/**
* Created by dx on 2017/8/17.
*/
/**
* 插入排序
**/
public class Sort {
public static void main(String[] args) {
int[] value = {12, 34, 56, 1, 2, 4};
System.out.print("排序前:");
print(value);
insertSort(value);
System.out.print("排序後:");
print(value);
}
/**
* 插入排序
* 將每個元素插入到有序數組中
**/
public static void insertSort(int[] value) {
int i=0,j;
int temp;
for (i=1;i<value.length;i++)
{
temp = value[i];
j = i - 1;
while (j>=0&&temp<value[j])
{
value[j+1] = value[j];
j--;
}
value[j+1] = temp;
System.out.print("第"+i+"排序後的結果爲");
print(value);
}
}
public static void print(int[] value) {
for (int number : value) {
System.out.print(number + " ");
}
System.out.println();
}
}
結果
排序前
12 34 56 1 2 4
第1排序後的結果爲12 34 56 1 2 4
第2排序後的結果爲12 34 56 1 2 4
第3排序後的結果爲1 12 34 56 2 4
第4排序後的結果爲1 2 12 34 56 4
第5排序後的結果爲1 2 4 12 34 56
排序後
1 2 4 12 34 56
簡單桶排序(計數排序)
原理
算法的步驟如下:
- 統計數組中每個值爲i的元素出現的次數,存入數組C的第i項
- 對所有的計數累加(從C中的第一個元素開始,每一項和前一項相加)
- 反向填充目標數組:將每個元素i放在新數組的第C(i)項,每放一個元素就將C(i)減去1
實現
/**
* Created by dx on 2017/8/17.
*/
/**
* 簡單桶排序
**/
public class Sort {
public static void main(String[] args) {
int[] value = {12, 34, 56, 1, 2, 4};
System.out.println("排序前");
print(value);
bucketSort(value);
}
/**
* 桶排序
* 利用桶對元素進行計數
**/
public static void bucketSort(int[] value) {
int[] bucketIndex = new int[1000];
//對桶進行初始化
for (int i = 0; i < bucketIndex.length; i++) {
bucketIndex[i] = 0;
}
//對元素進行計數
for (int item : value) {
bucketIndex[item]++;
}
int[] newValue = new int[value.length];
int i = 0;
//輸出桶中元素
for (int index = 0; index < bucketIndex.length; index++) {
while (bucketIndex[index] > 0) {
newValue[i++] = index;
bucketIndex[index]--;
}
}
System.out.println("排序後:");
print(newValue);
}
public static void print(int value[]) {
for (int item : value) {
System.out.print(item + " ");
}
System.out.println();
}
}
結果
排序前
12 34 56 1 2 4
排序後:
1 2 4 12 34 56
快速排序
原理
(1)基本思想
選擇一個基準元素,通常選擇第一個元素或者最後一個元素,通過一趟掃描,將待排序列分成兩部分,一部分比基準元素小,一部分大於等於基準元素,此時基準元素在其排好序後的正確位置,然後再用同樣的方法遞歸地排序劃分的兩部分。
步驟:
1.從數列中挑出一個元素作爲基準數(一般爲第一個數)。
2.分區過程,將比基準數大的放到右邊,小於或等於它的數都放到左邊。(注意這個過程先從右到左比較,再從左到右)
3.再對左右區間遞歸執行第二步,直至各區間只有一個數。
(2)實例:
實現
import java.util.*;
public class Main {
static int index = 0;
public static void main(String[] args) {
int [] value = {12,34,56,65,12,3,45,3};
quickSort(value,0,value.length-1);
}
public static void quickSort(int [] value,int left,int end){
if(left>=end)
return ;
int i = left;
int j = end;
int temp = value[left];
while(i<j){
while(value[j]>=temp&&i<j){
j--;
}
while(value[i]<=temp&&i<j){
i++;
}
if(i<j)
swap(value,i,j);
}
value[left] = value[i];
value[i] = temp;
System.out.print("第"+(index++)+"次排序的結果爲:");
print(value);
quickSort(value,left,i-1);
quickSort(value,i+1,end);
}
//交換兩個元素
public static void swap(int [] value,int i,int j){
int temp = value[i];
value[i] = value[j];
value[j] = temp;
}
//打印數組元素
public static void print(int [] value){
for(int item:value){
System.out.print(item+" ");
}
System.out.println("");
}
}
堆排序
原理
(1)基本思想
堆排序是一種樹形選擇排序,是對直接選擇排序的有效改進。
堆的定義如下:具有n個元素的序列(h1,h2,…,hn),當且僅當滿足(hi>=h2i,hi>=2i+1)或(hi<=h2i,hi<=2i+1)(i=1,2,…,n/2)時稱之爲堆。在這裏只討論滿足前者條件的堆。由堆的定義可以看出,堆頂元素(即第一個元素)必爲最大項(大頂堆)。完全二叉樹可以很直觀地表示堆的結構。堆頂爲根,其它爲左子樹、右子樹。初始時把要排序的數的序列看作是一棵順序存儲的二叉樹,調整它們的存儲序,使之成爲一個堆,這時堆的根節點的數最大。然後將根節點與堆的最後一個節點交換。然後對前面(n-1)個數重新調整使之成爲堆。依此類推,直到只有兩個節點的堆,並對它們作交換,最後得到有n個節點的有序序列。從算法描述來看,堆排序需要兩個過程,一是建立堆,二是堆頂與堆的最後一個元素交換位置。所以堆排序有兩個函數組成。一是建堆的滲透函數,二是反覆調用滲透函數實現排序的函數。
(2)實例:
初始序列:46,79,56,38,40,84
建堆:
交換,從堆中踢出最大數:
實現
import java.util.Arrays;
/**
* Created by dx on 2017/8/29.
*/
public class HeapSort {
/**
* 構建大頂堆
*/
public static void adjustHeap(int[] value, int parent, int len) {
//帶排序元素
int temp = value[parent];
int child = parent * 2 + 1;
while (child < len){
if(child + 1 < len && value[child + 1] > value[child]){
child += 1;
}
if(value[child] < temp)
break;
value[parent] = value[child];
parent = child;
child = child * 2 + 1;
}
value[parent] = temp;
}
public static void heapSort(int[] value) {
for (int i = value.length / 2 - 1; i >= 0; i--) {// 構建一個大頂堆
adjustHeap(value, i, value.length);
}
//i爲無序區的長度
for (int i = value.length - 1; i > 0; i--) {// 將堆頂記錄和當前未經排序子序列的最後一個記錄交換
swap(value, i, 0);
adjustHeap(value, 0, i);// 將value中前i-1個記錄重新調整爲大頂堆
}
}
public static void swap(int[] value, int i, int j) {
int temp = value[i];
value[i] = value[j];
value[j] = temp;
}
public static void main(String[] args) {
int value[] = {51, 46, 20, 18, 65, 97, 82, 30, 77, 50,51};
heapSort(value);
System.out.println(Arrays.toString(value));
}
}
歸併排序
原理
(1)基本排序:
歸併(Merge)排序法是將兩個(或兩個以上)有序表合併成一個新的有序表,即把待排序序列分爲若干個子序列,每個子序列是有序的。然後再把有序子序列合併爲整體有序序列。
- 先遞歸分解數列
- 再合併數列
(2)實例:
實現
/**
* Created by dx on 2017/9/3.
*/
import java.util.Arrays;
/**
* 歸併排序
**/
public class Sort {
public static void sort(int[] nums, int low, int high) {
if (low >= high)
return;
int mid = (low + high) / 2;
// 左邊
sort(nums, low, mid);
// 右邊
sort(nums, mid + 1, high);
// 左右歸併
merge(nums, low, mid, high);
}
public static void merge(int[] nums, int low, int mid, int high) {
int[] temp = new int[high - low + 1];
int i = low;// 左指針
int j = mid + 1;// 右指針
int k = 0;
// 把較小的數先移到新數組中
while (i <= mid && j <= high) {
if (nums[i] < nums[j]) {
temp[k++] = nums[i++];
} else {
temp[k++] = nums[j++];
}
}
// 把左邊剩餘的數移入數組
while (i <= mid) {
temp[k++] = nums[i++];
}
// 把右邊邊剩餘的數移入數組
while (j <= high) {
temp[k++] = nums[j++];
}
// 把新數組中的數覆蓋nums數組
for (int k2 = 0; k2 < temp.length; k2++) {
nums[k2 + low] = temp[k2];
}
}
// 歸併排序的實現
public static void main(String[] args) {
int[] nums = {2, 7, 8, 3, 1, 6, 9, 0, 5, 4};
sort(nums, 0, nums.length - 1);
System.out.println(Arrays.toString(nums));
}
}
希爾排序(最小增量排序)
原理
(1)基本思想
算法先將要排序的一組數按某個增量d(n/2,n爲要排序數的個數)分成若干組,每組中記錄的下標相差d.對每組中全部元素進行直接插入排序,然後再用一個較小的增量(d/2)對它進行分組,在每組中再進行直接插入排序。當增量減到1時,進行直接插入排序後,排序完成。
(2)實例:
實現
/**
* Created by dx on 2017/8/29.
*/
public class Shell {
public static void main(String[] args) {
int[] value = {12, 45, 678, 89, 23, 12, 45, 67, 78, 23, 65, 56, 38};
shellSort(value);
for (int i : value) {
System.out.println(i);
}
}
public static void shellSort(int[] value) {
//決定increment的大小
for (int increment = value.length / 2; increment > 0; increment /= 2) {
for (int i = increment; i < value.length; i++) {
int j = i - increment;
int temp = value[i];
while (j >= 0 && temp < value[j]) {
value[j + increment] = value[j];
j = j - increment;
}
value[j + increment] = temp;
}
}
}
}
基數排序
原理
(1)基本思想
將所有待比較數值(正整數)統一爲同樣的數位長度,數位較短的數前面補零。然後,從最低位開始,依次進行一次排序。這樣從最低位排序一直到最高位排序完成以後,數列就變成一個有序序列。
(2)實例
實現
/**
* Created by dx on 2017/9/3.
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 基數排序
**/
public class Sort {
public static void sort(int[] nums) {
if (nums.length == 1)
return;
//確定排序的趟數
int max = nums[0];
for (int item : nums) {
if (item > max)
max = item;
}
int times = 0;
//判斷位數
while (max > 0) {
max = max / 10;
times++;
}
//建立10個隊列,分別表示0-9
List<ArrayList> queue = new ArrayList<ArrayList>();
for (int i = 0; i < 10; i++) {
ArrayList<Integer> item = new ArrayList<Integer>();
queue.add(item);
}
//進行time次的分配和收集
for (int i = 0; i < times; i++) {
//分配數組元素
for (int j = 0; j < nums.length; j++) {
//得到數字的第times+1位數
int x = nums[j] % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i);
ArrayList arrayList = queue.get(x);
arrayList.add(nums[j]);
}
//元素計數器
int count = 0;
//收集隊列元素
for (int k = 0; k < 10; k++) {
while (queue.get(k).size() > 0) {
ArrayList arrayList = queue.get(k);
nums[count++] = (int) arrayList.get(0);
arrayList.remove(0);
}
}
}
}
// 基數排序的實現
public static void main(String[] args) {
int[] nums = {2, 7, 8, 3, 1, 6, 9, 0, 5, 4,345,234,576,678,234,56,78,9,45};
sort(nums);
System.out.println(Arrays.toString(nums));
}
}