zoukankan      html  css  js  c++  java
  • 关于快速排序的Java代码实现

    快速排序(Quicksort)是对冒泡排序的一种改进。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。

    实现方式一:

     1 package test1;
     2 
     3 public class QuicSort {
     4     /*
     5      * 使用快速排序
     6      * arras:要排序的数组
     7      * low:数组的开始下标
     8      * hight:数组的末尾下标
     9      */
    10     public void sort(int[] arras,int low,int hight){
    11         int i = low;
    12         int j = hight;
    13         if(i>j){
    14             return ;
    15         }
    16         //基准元素
    17         int key = arras[low];
    18         while(true){//让一趟里面的全部元素都比较完毕
    19             //j往前走
    20             while(j>i){
    21                 if(arras[j] < key){
    22                     //交换
    23                     int temp = arras[j];
    24                     arras[j] = arras[i];
    25                     arras[i] = temp;
    26                     break;
    27                 }else{
    28                     j--;
    29                 }
    30             }
    31             //i往后走
    32             while(j>i){
    33                 if(arras[i] > key){
    34                     //交换
    35                     int temp = arras[j];
    36                     arras[j] = arras[i];
    37                     arras[i] = temp;
    38                     break;
    39                 }else{
    40                     i++;
    41                 }
    42             }
    43             if(i ==j){
    44                 break;
    45             }
    46         }
    47         //基准左边的数组排序
    48         sort(arras,low,i-1);
    49         //基准右边的数组排序
    50         sort(arras,i+1,hight);
    51     }
    52     /*
    53      * 打印数组里面的元素
    54      */
    55     public void print(int[] arras){
    56         for(int i=0 ; i<arras.length;i++){
    57             if(i==arras.length-1){
    58                 System.out.println(arras[i]);
    59             }else{
    60                 System.out.print(arras[i]+",");
    61             }
    62         }
    63     }
    64     public static void main(String[] args) {
    65         int[] as = {49,38,65,97,76,13,27};
    66         QuicSort qs = new QuicSort();
    67         qs.sort(as,0,as.length-1);
    68         qs.print(as);
    69     }
    70 }

    实现方式二:

     1 package test1;
     2 
     3 public class QuickSort1 {
     4     int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99,
     5             98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
     6     public QuickSort1() {
     7         quick(a);
     8         for (int i = 0; i < a.length; i++)
     9             System.out.println(a[i]);
    10     }
    11     public int getMiddle(int[] list, int low, int high) {
    12         int tmp = list[low]; // 数组的第一个作为中轴
    13         while (low < high) {
    14             while (low < high && list[high] >= tmp) {
    15                 high--;
    16             }
    17             list[low] = list[high]; // 比中轴小的记录移到低端
    18             while (low < high && list[low] <= tmp) {
    19                 low++;
    20             }
    21             list[high] = list[low]; // 比中轴大的记录移到高端
    22         }
    23         list[low] = tmp; // 中轴记录到尾
    24         return low; // 返回中轴的位置
    25     }
    26     public void _quickSort(int[] list, int low, int high) {
    27         if (low < high) {
    28             int middle = getMiddle(list, low, high); // 将list数组进行一分为二
    29             _quickSort(list, low, middle - 1); // 对低字表进行递归排序
    30             _quickSort(list, middle + 1, high); // 对高字表进行递归排序
    31         }
    32     }
    33     public void quick(int[] a2) {
    34         if (a2.length > 0) { // 查看数组是否为空
    35             _quickSort(a2, 0, a2.length - 1);
    36         }
    37     }
    38 }
  • 相关阅读:
    chrome浏览器中安装以及使用Elasticsearch head 插件
    windows10 升级并安装配置 jmeter5.3
    linux下部署Elasticsearch6.8.1版本的集群
    【Rollo的Python之路】Python 爬虫系统学习 (八) logging模块的使用
    【Rollo的Python之路】Python 爬虫系统学习 (七) Scrapy初识
    【Rollo的Python之路】Python 爬虫系统学习 (六) Selenium 模拟登录
    【Rollo的Python之路】Python 爬虫系统学习 (五) Selenium
    【Rollo的Python之路】Python 爬虫系统学习 (四) XPath学习
    【Rollo的Python之路】Python 爬虫系统学习 (三)
    【Rollo的Python之路】Python sys argv[] 函数用法笔记
  • 原文地址:https://www.cnblogs.com/DreamDrive/p/4082659.html
Copyright © 2011-2022 走看看