![](http://static.javashuo.com/static/loading.gif)
// 冒泡排序
public static void bubbleSort(int str[]) {
int length = str.length;
int temp;
for (int i = 0; i < length - 1; i++) {
for(int j = 0; j < length - i - 1; j++) {
if (str[j] > str[j + 1]) {
temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
}
/**
* 插入排序
*
* 從第一個元素開始,該元素能夠認爲已經被排序
* 取出下一個元素,在已經排序的元素序列中從後向前掃描
* 若是該元素(已排序)大於新元素,將該元素移到下一位置
* 重複步驟3,直到找到已排序的元素小於或者等於新元素的位置
* 將新元素插入到該位置中
* 重複步驟2
* @param numbers 待排序數組
*/
![](http://static.javashuo.com/static/loading.gif)
// 插入排序
public static void insertSort(int[] str) {
int length = str.length;
int temp;
for(int i = 0; i < length; i++) {
for (int j = i ; j > 0; j--) {
if(str[j] < str[j - 1]) {
temp = str[j];
str[j] = str[j - 1];
str[j - 1] = temp;
}
}
}
}
}
/**
* 選擇排序算法
* 在未排序序列中找到最小元素,存放到排序序列的起始位置
* 再從剩餘未排序元素中繼續尋找最小元素,而後放到排序序列末尾。
* 以此類推,直到全部元素均排序完畢。
* @param numbers
*/
![](http://static.javashuo.com/static/loading.gif)
// 選擇排序
public static void selectSort(int str[]) {
int length = str.length;
int temp;
for ( int i = 0; i < length - 1; i++) {
int index = i;
for(int j = i + 1; j < length; j++) {
if(str[index] > str[j]) {
index = j;
}
}
if (index != i) {
temp = str[i];
str[i] = str[index];
str[index] = temp;
}
}
}
/**
* 歸併排序
* 簡介:將兩個(或兩個以上)有序表合併成一個新的有序表 即把待排序序列分爲若干個子序列,每一個子序列是有序的。而後再把有序子序列合併爲總體有序序列
* 時間複雜度爲O(nlogn)
* 穩定排序方式
* @param nums 待排序數組
* @return 輸出有序數組
*/
![](http://static.javashuo.com/static/loading.gif)
// 歸併排序
public static void mergeSort(int[] arr) {
// 新建一個等長的數組
int[] temp = new int[arr.length];
sort(arr, 0, arr.length - 1, temp);
}
private static void sort(int[] arr, int left, int right, int[] temp) {
if(left < right) {
int mid = (left + right)/2;
// 左邊歸併排序,使得左子序列有序
sort(arr, left, mid, temp);
// 右邊歸併排序,使得右子序列有序
sort(arr, mid + 1, right, temp);
// 將兩個有序子數組合並操做
merge(arr, left, mid, right, temp);
}
}
/**
* 查找出中軸(默認是最低位low)的在numbers數組排序後所在位置
*
* @param numbers 帶查找數組
* @param low 開始位置
* @param high 結束位置
* @return 中軸所在位置
*/
![](http://static.javashuo.com/static/loading.gif)
// 快速排序
public static void quickSort(int[] str, int left, int right) {
int mid;
if (left < right) {
mid = partition(str, left, right);
quickSort(str, left, mid - 1);
quickSort(str, mid + 1, right);
}
}
public static int partition(int str[],int left,int right) {
while (left < right) {
int temp;
if (str[left] > str[right]) {
temp = str[left];
str[left] = str[right];
str[right] = temp;
left++;
} else if (str[left] > str[right]) {
temp = str[left];
str[left] = str[right];
str[right] = temp;
right--;
} else {
right--;
continue;
}
}
System.out.println(left+":"+right);
return right;
}
//**希爾排序的原理:根據需求,若是你想要結果從大到小排列,它會首先將數組進行分組,而後將較大值移到前面,較小值
/* 移到後面,最後將整個數組進行插入排序,這樣比起一開始就用插入排序減小了數據交換和移動的次數,能夠說希爾排序是增強
* 版的插入排序
* 拿數組5, 2, 8, 9, 1, 3,4來講,數組長度爲7,當increment爲3時,數組分爲兩個序列
* 5,2,8和9,1,3,4,第一次排序,9和5比較,1和2比較,3和8比較,4和比其下標值小increment的數組值相比較
* 此例子是按照從大到小排列,因此大的會排在前面,第一次排序後數組爲9, 2, 8, 5, 1, 3,4
* 第一次後increment的值變爲3/2=1,此時對數組進行插入排序,
*實現數組從大到小排
*/
![](http://static.javashuo.com/static/loading.gif)
// 希爾排序
public static void shellSort(int[] str) {
// 設置步長,默認爲數組長度的一半
int step = str.length / 2;
while (step >= 1) {
for (int i = step; i < str.length; i += step) {
int tmp = str[i];
int j;
for (j = i; j > 0 && str[j - step] > tmp; j -= step) {
str[j] = str[j - step];//元素後移
}
str[j] = tmp;//插入的位置,注意此時j在for循環中已經進行了一次--
}
step /= 2;
}
}
/**
* 堆排序是一種樹形選擇排序,是對直接選擇排序的有效改進。
堆的定義下:具備n個元素的序列 (h1,h2,...,hn),當且僅當知足(hi>=h2i,hi>=2i+1)或(hi<=h2i,hi<=2i+1) (i=1,2,...,n/2)時稱之爲堆。在這裏只討論知足前者條件的堆。由堆的定義能夠看出,堆頂元素(即第一個元素)必爲最大項(大頂堆)。
徹底二 叉樹能夠很直觀地表示堆的結構。堆頂爲根,其它爲左子樹、右子樹。
思想:初始時把要排序的數的序列看做是一棵順序存儲的二叉樹,調整它們的存儲序,使之成爲一個 堆,
這時堆的根節點的數最大。而後將根節點與堆的最後一個節點交換。而後對前面(n-1)個數從新調整使之成爲堆。
依此類推,直到只有兩個節點的堆,並對 它們做交換,最後獲得有n個節點的有序序列。從算法描述來看,
堆排序須要兩個過程,一是創建堆,二是堆頂與堆的最後一個元素交換位置。因此堆排序有兩個函數組成。
一是建堆的滲透函數,二是反覆調用滲透函數實現排序的函數。
*/
![](http://static.javashuo.com/static/loading.gif)
// 堆排序public static void heapSort(int[] arr) { // 一、構建大頂堆 for(int i = arr.length/2-1; i>=0;i--) { // 從第一個非葉子節點從下至上,從右至左調整結構 adjustHeap(arr, i, arr.length); } // 二、調整堆結構+交換堆頂元素和末尾元素 for(int j = arr.length-1; j > 0; j--) { //將堆頂元素與末尾元素進行交換 int temp = arr[0]; arr[0] = arr[j]; arr[j] = temp; //從新對堆進行調整 adjustHeap(arr, 0, j); }}private static void adjustHeap(int[] arr, int i, int length) { int temp = arr[i]; for(int k = 2*i +1; k < length; k = 2*k +1) { if(k + 1 < length && arr[k] < arr[k + 1]) { k++; } if(temp < arr[k]) { arr[i] = arr[k]; i = k; } else { break; } } arr[i] = temp;}