zoukankan      html  css  js  c++  java
  • 快速排序之Java实现

    快速排序之Java实现

    代码:

      1 package cn.com.zfc.lesson21.sort;
      2 
      3 /**
      4  * 
      5  * @title QuickSort
      6  * @describe 快速排序
      7  * @author 张富昌
      8  * @date 2016年10月2日下午2:45:37
      9  */
     10 public class QuickSort {
     11     // 快速排序是平均时间最快的排序算法。
     12     // 基本思想:任选待排序列中的一个数据元素(通常选取第一个数据元素)作为枢轴,用它和所有的剩余元素进行比较,将所有较它小的元素排在它的前面;
     13     // 将所有较它大的元素排在它的后面,经过一趟排序后,可按次数据元素所在位置为界,将可序列化分成两个部分;
     14     // 再对这两个部分重复上述过程直至每一个部分中只剩下一个数据元素为止。
     15 
     16     public static void main(String[] args) {
     17         // 声明整型数组
     18         int[] array = new int[10];
     19         // 使用循环和随机数初始化数组
     20         for (int i = 0; i < array.length; i++) {
     21             array[i] = (int) Math.round(Math.random() * 100);
     22         }
     23         System.out.println("原始数组为:");
     24         for (int i : array) {
     25             System.out.print(i + " ");
     26         }
     27         System.out.println();
     28         System.out.println("排序后的数组为:");
     29         for (int i : quickSort(array)) {
     30             System.out.print(i + " ");
     31         }
     32     }
     33 
     34     /**
     35      * 
     36      * 功能:对数组进行快速排序,并且返回该数组
     37      *
     38      * 参数:int[] arr
     39      *
     40      * 返回类型:int[]
     41      */
     42     public static int[] quickSort(int[] arr) {
     43         quickSortHelp(arr, 0, arr.length - 1);
     44         return arr;
     45     }
     46 
     47     /**
     48      * 
     49      * 功能:对数组 arr[low...high] 中的记录进行快速排序
     50      *
     51      * 参数:int[] arr, int low, int high
     52      *
     53      * 返回类型:void
     54      */
     55     public static void quickSortHelp(int[] arr, int low, int high) {
     56         if (low < high) {
     57             // 子序列 elem[low...high] 的长度大于 1
     58             int pivotLoc = partition(arr, low, high);
     59             // 对子序列 arr[low...pivotLoc-1] 递归排序
     60             quickSortHelp(arr, low, pivotLoc - 1);
     61             // 对子序列 arr[pivotLoc+1...high] 递归排序
     62             quickSortHelp(arr, pivotLoc + 1, high);
     63         }
     64     }
     65 
     66     /**
     67      * 
     68      * 功能:使枢轴元移到正确的位置,要求枢轴左边的元素不大于枢轴,枢轴右边的元素不小于枢轴,并返回枢轴的位置
     69      *
     70      * 参数:int[] arr, int low,int high
     71      *
     72      * 返回类型:int
     73      */
     74     public static int partition(int[] arr, int low, int high) {
     75         while (low < high) {
     76             // arr[low] 为枢轴,使 low左边的元素不大于 elem[high]
     77             while (low < high && arr[low] <= arr[high]) {
     78                 high--;
     79             }
     80             // 交换 arr[low] 和 arr[high]的值
     81             swap(arr, low, high);
     82             // arr[high] 为枢轴,使 low 左边的元素不大于 arr[high]
     83             while (low < high && arr[low] <= arr[high]) {
     84                 low++;
     85             }
     86             // 交换 arr[low]和 arr[high]的值
     87             swap(arr, low, high);
     88         }
     89         // 返回枢轴的位置
     90         return low;
     91     }
     92 
     93     /**
     94      * 
     95      * 功能:交换两个数的值
     96      *
     97      * 参数:int x,int y
     98      *
     99      * 返回类型:void
    100      */
    101     public static void swap(int[] arr, int i, int j) {
    102         int temp = arr[i];
    103         arr[i] = arr[j];
    104         arr[j] = temp;
    105     }
    106 }

    运行结果:

  • 相关阅读:
    elasticsearch painless脚本评分
    elasticsearch relevance score相关性评分的计算
    java 多线程间通信(二)
    java 多线程间通信(一)
    java CountDownLatch、CyclicBarrier和 Semaphore用法
    java 深入剖析ThreadLocal
    java中String、StringBuffer、StringBuilder的区别
    Leetcode: LRU Cache
    Leetcode: Anagrams(颠倒字母而成的字)
    Leetcode: First Missing Positive
  • 原文地址:https://www.cnblogs.com/zfc-java/p/7941078.html
Copyright © 2011-2022 走看看