Java实现基本排序算法

来源:互联网 发布:java测试类 main方法 编辑:程序博客网 时间:2024/05/28 19:24

主要参考严蔚敏的数据结构中的算法来实现。如果不明白 可以查看。

还参考如下链接中的类容。

  http://www.iteye.com/topic/1116214
  http://www.iteye.com/topic/547734
  http://www.iteye.com/topic/547735

如果想了解原理可以查看上述资料。现在就直接上代码

/** * http://www.iteye.com/topic/1116214 * http://www.iteye.com/topic/547734 * http://www.iteye.com/topic/547735*/public class Sort {private static Sort sort = null;private Sort() {}public static Sort getInstance() {if (sort == null) {sort = new Sort();}return sort;}private void move(int[] array, int from, int end) {for (int i = end; i >= from; i--) {array[i] = array[i - 1];}}  /**      * 交换数组中的两个元素的位置      * @param array 待交换的数组      * @param i 第一个元素      * @param j 第二个元素      */      private void swap(int[] array, int i, int j) {          if (i != j) {//只有不是同一位置时才需交换              int tmp = array[i];              array[i] = array[j];              array[j] = tmp;          }      }  public void display(int[] array) {for (int i = 0; i < array.length; i++) {System.out.print("  " + array[i]);}System.out.println();} public void insertSort(int[] array) { for (int i = 1; i < array.length; i++) { int temp = array[i]; for (int j = 0; j < i; j++) { if (array[i] < array[j]) { move(array, j, i); array[j] = temp; } } } }  private void shellInsert(int[] array, int step) { for(int i = step + 1; i < array.length; i++) { if (array[i] < array[i - step]) { int temp = array[i]; int j = -1; for (j = i - step; j > 0 && temp < array[j]; j = j - step) { array[j + step] = array[j]; } array[j + step] = temp; } } }  public void shellSort(int[] array, int[] steps) { for(int k = 0; k < steps.length; k++) { shellInsert(array, steps[k]); } }  /*  * 结束条件:在一趟排序过程中没有进行过交换记录的操作。  * **/ public void bubbleSort(int[] array) { display(array); for (int i = 0; i < array.length; i++) { boolean hasSwap = false; for (int j = 1; j < array.length - i; j++) { if (array[j] < array[j - 1]) { swap(array, j, j - 1); hasSwap = true; } } display(array); if (!hasSwap) { return; } } }  /*  * 交换数组中[low, high]的记录,枢轴记录到位,并返回其所在的位置。  * 此时,在它之前(后)的记录均不大(小)于它。  * **/ private int partition(int[] array, int low, int high) { int temp = array[low]; while(low < high) { while (low < high && array[high] >= temp) { --high; } array[low] = array[high]; while (low < high && array[low] <=  temp) { ++low; } array[high] = array[low]; } array[low] = temp; return low; }  private void qSort(int[] array, int low, int high) { if (low < high) { int mid = partition(array, low, high); qSort(array, low, mid - 1); qSort(array, mid + 1, high); } }  public void quickSort(int[] array) { qSort(array, 0, array.length - 1); }  public void selectSort(int[] array) { for (int i = 0; i < array.length; i++) { for (int j = i + 1; j < array.length; j++) { if (array[i] > array[j]) { swap(array, i, j); } } } }  /*  * n个元素的序列{k1,k2,k3,...,kn}当且仅当满足下面关系时,称之为堆。  *   * ki <= k2i 且 ki <= k2i + 1  *   * 或  *   * ki >= 2i 且  ki >= k2i + 1  *   * 可以将数组看成一个完全二叉树。  *   * **/ private void heapAdjust(int array[], int start, int end) { // 已知array[start,end]中除array[start]之外均满足堆的定义,本函数调整 // array[start],使array[start, end]成为一个大顶堆。 int temp = array[start]; for (int i = 2 * start + 1; i <= end; i = i * 2 + 1) {     // 沿较大的孩子节点向下筛选 if (i + 1 <= end && array[i] < array[i + 1]) { // i 为较大的记录的下标 i++;                                   }  if(temp >= array[i]) { // 如果大于两个子节点中较大的则结束。 break; } array[start] = array[i]; start = i;                               // temp 应该插在start } array[start] = temp; }  public void heapSort(int array[]) { display(array); for (int i = (array.length -1) / 2; i >= 0; i--) { // 将数组建成大顶堆 // 从最后一个非终端节点开始 heapAdjust(array, i, array.length -1); } System.out.println("大堆: "); display(array);  for (int i = array.length - 1; i > 0; i--) { swap(array, 0, i); heapAdjust(array, 0, i - 1); } } }


public class Main {static int[] array = { 10, 1, 3, 7, 5, 4, 10, 8, 9, 2, 6};//static int[] arrayTest = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };static int[] steps = { 7, 3, 1 };private static Sort sort = null;/** * @param args */public static void main(String[] args) {sort = Sort.getInstance();// 插入排序//sort.insertSort(array);//sort.display(array);//sort.shellSort(array, steps);//sort.display(array);// 交换排序//sort.bubbleSort(array);//sort.display(array);//sort.quickSort(array);//sort.display(array);//sort.selectSort(array);//sort.display(array);sort.heapSort(array);sort.display(array);}}
后续继续更新。