冒泡排序
思路:就是每次将最大或最小的元素放到数组的最后,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)。