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]+" "); } } }
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] + " "); } } }
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] + " "); } } }
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] + " "); } } }
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] + " "); } } }
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] + " "); } } }
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)。
七大排序算法(冒泡,选择,插入,希尔,快速,合并,堆排序)的java实现
原文地址:http://blog.csdn.net/dafeng_blog/article/details/24807545