七大排序演算法(冒泡,選擇,插入,二分法排序,希爾,快速,合併,堆排序)的java實現(14/8/3更新加入二分排序)
阿新 • • 發佈:2019-02-04
氣泡排序
思路:就是每次將最大或最小的元素放到陣列的最後,so easy!時間複雜度為(O(n^2))public class BubbleSort { public static void bubbleSort(int[] a) { for (int j = 1; j < a.length; j++) { for (int i = 0; i < a.length - j; i++) { if (a[i] > a[i + 1]) { int temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } } } } public static void main(String[] args) { int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 }; bubbleSort(a); for(int i = 0; i < a.length; i++){ System.out.print(a[i]+" "); } } }
選擇排序
思路:類似於冒泡,每次將後面最小的元素放在前面。時間複雜度為((O(n^2)))public class SelectSort { public static void selectSort(int[] a) { int min; for (int i = 0; i < a.length - 1; i++) { min = i; for (int j = min + 1; j < a.length; j++) { if (a[min] > a[j]) { int temp = a[min]; a[min] = a[j]; a[j] = temp; } } } } public static void main(String[] args) { int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 }; selectSort(a); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } }
插入排序
思路:從第二個元素開始,和之前的已排好序的字陣列的元素比較,找到合適的位置,然後後面的元素向後移動一位,再將該元素插入到前面合適的位置。時間複雜度為(O(n^2))public class InsertSort { public static void insertSort(int[] a) { for (int i = 1; i < a.length; i++) { int key = a[i]; int j = i - 1; while (j >= 0 && a[j] > key) { a[j+1] = a[j]; j--; } a[j+1] = key; } } public static void main(String[] args) { int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 }; insertSort(a); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } }
二分法排序
思路:插入排序的多此一舉,居然先用二分法查詢插入位置(多此一舉),然後再所有插入位置(二分法查出來的)後面的元素全部後移,太蛋疼了,插入法直接邊找邊後移多容易啊,這個事蛋疼的做法,下午想了一下做出來。(14、08、3)package sort;
public class BinarySort {
public static int binarySerch(int[] arr, int start, int end, int value) {
int mid = -1;
while (start <= end) {
mid = (start + end) / 2;
if (arr[mid] < value)
start = mid + 1;
else if (arr[mid] > value)
end = mid - 1;
else
break;
}
if (arr[mid] < value)
return mid + 1;
else if (value < arr[mid])
return mid;
return mid + 1;
}
public static void binarySort(int[] arr, int start, int end) {
for (int i = start + 1; i <= end; i++) {
int value = arr[i];
int insertLoc = binarySerch(arr, start, i - 1, value) ;
for (int j = i; j > insertLoc; j--) {
arr[j] = arr[j - 1];
}
arr[insertLoc] = value;
}
}
public static void main(String[] args) {
int[] arr = { 3, 5, 0, 8, 7, 9, 1, 2, 6, 4 };
binarySort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
希爾排序
思路:類似於插入排序,只是每次所取的步長為(陣列的長度 / 2 / i)。時間複雜度為(n*log n)。public class ShellSort {
public static void shellSort(int[] a) {
for (int gap = a.length / 2; gap > 0; gap /= 2)
for (int i = gap; i < a.length; i++) {
int key = a[i];
int j = i - gap;
while (j >= 0 && a[j] > key) {
a[j + gap] = a[j];
j -= gap;
}
a[j + gap] = key;
}
}
public static void main(String[] args) {
int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7,
2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 };
shellSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
快速排序
思路:關鍵在於求出partition()函式。我給出了兩種方法:1、從前到後。2、從前到中間,從後到中間。時間複雜度為(n * log n)最壞情況為 OK!show your my codes!public class QuickSort {
/*public static int partition(int[] a, int p, int r) {
int x = a[r];
int i = p - 1;
for (int j = p; j < r; j++) {
if (a[j] <= x) {//如果a[j]<x就將後面a[i]後面a[i+1](值大於x)與a[j](值<a[j])進行交換
i++;
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
i++;
int temp = a[i];
a[i] = a[r];
a[r] = temp;
return i;
}*/
public static int partition(int a[], int p, int r) {
int x = a[p];
int i = p;
int j = r ;
while (i < j) {
while (a[j] >= x && i<j)
j--;
a[i] = a[j];//把小於X的那個數拿到前面的a【i】中
while (a[i] <= x && i<j)
i++;
a[j] = a[i];//把大於X的那個數拿到後面的a【j】中
}
a[j] = x;//將X拿到分割處
return j;
}
public static void quickSort(int[] a, int p, int r) {
if (p < r) {
int q = partition(a, p, r);
quickSort(a, p, q-1);
quickSort(a, q + 1, r);
}
}
public static void main(String[] args) {
int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7,
3, 8, 31 };
quickSort(a, 0, a.length-1);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
合併排序
思路:用分治的思想,將陣列分至最小,再合併即可,不懂自己google吧!時間複雜度為(n * log n )是一個穩定的排序演算法。public class MergeSort {
public static void merge(int A[], int p, int q, int r) {
int[] L = new int[q - p + 1];
int[] R = new int[r - q];
for (int i = p, j = 0; i <= q; i++, j++) {
L[j] = A[i];
}
for (int i = q + 1, j = 0; i <= r; i++, j++) {
R[j] = A[i];
}
int pos = p;
int i = 0, j = 0;
for (; i < L.length && j < R.length;) {
if (L[i] <= R[j]) {
A[pos++] = L[i++];
} else {
A[pos++] = R[j++];
}
}
if (i < L.length) {
for (; i < L.length;)
A[pos++] = L[i++];
} else if (j < R.length) {
for (; j < R.length;)
A[pos++] = R[j++];
}
}
public static void mergeSort(int[] A, int p, int r) {
if (p < r) {
int q = (p + r) / 2;
mergeSort(A, p, q);
mergeSort(A, q + 1, r);
merge(A, p, q, r);
}
}
public static void main(String[] args) {
int A[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7,
3, 8, 31 };
mergeSort(A, 0, A.length - 1);
for (int i = 0; i < A.length; i++) {
System.out.print(A[i] + " ");
}
}
}
堆排序
思路:建立一個堆,大頂堆或者小頂堆都可以。每次將堆頂元素放到陣列最後,然後堆的規模減小一個,再維持第一個元素的大頂堆性質。時間複雜度為(n * log n)。public class HeapSort {
//求雙親位置
static int parent(int i) {
return i / 2;
}
//求左孩子位置
static int left(int i) {
return 2 * i;
}
//求右孩子位置
static int right(int i) {
return 2 * i + 1;
}
//維持大頂堆的性質
static void maxHelpify(int[] A, int i) {
int l = left(i);
int r = right(i);
int largest = 0;
if (l <= A[0] && A[l] > A[i])
largest = l;
else
largest = i;
if (r <= A[0] && A[r] > A[largest])
largest = r;
if (largest != i) {
int temp = A[largest];
A[largest] = A[i];
A[i] = temp;
maxHelpify(A, largest);
}
}
//建立大頂堆
static void buildMaxHeap(int[] A){
for(int i=(A.length-1)/2; i>0; i--){
maxHelpify(A, i);
}
}
//堆排序
public static void heapSort(int[] A){
buildMaxHeap(A);//建立大頂堆
//每次把堆頂的數放到陣列最後,然後把堆的大小減1,再次維持第一個數的大頂堆性質
for(int i = A.length - 1; i>=2; i--){
int temp = A[1];
A[1] = A[i];
A[i] = temp;
A[0]--;
maxHelpify(A, 1);
}
}
public static void main(String[] args) {
int A[] = new int[3];
A[0] = A.length-1;//a[0]存放堆的大小
for(int i = 1; i < A.length; i++){
A[i] = (int) (Math.random()*10);
}
heapSort(A);
for (int i = 1; i < A.length; i++) {
System.out.print(A[i] + " ");
}
}
}
其他:還有一種計數排序。
比較簡單:就是將陣列下標作為元素的value,特殊情況下使用。如排序N個人的年齡就可以用計數排序。將年齡看作陣列的下標,定義一個大小為100的陣列,將年齡與之比較,如果年齡==下標就將,該下標的value+1.時間複雜度為(N)。