zoukankan      html  css  js  c++  java
  • java算法----排序----(4)快速排序

     1 package log;
     2 
     3 public class Test4 {
     4 
     5     /**
     6      * java算法---快速排序
     7      * 
     8      * @param args
     9      */
    10     public static void main(String[] args) {
    11         // 需要排序的数组
    12         int arr[] = { 49, 20, 36, 51, 18, 94, 61, 31, 50 };
    13         // 循环输出该数组内容
    14         System.out.println("排序之前:");
    15         for (int a : arr) {
    16             System.out.print(a + "	");
    17         }
    18         System.out.println();
    19 
    20         if (arr.length > 0) {
    21             sort(arr, 0, arr.length - 1);
    22         }
    23         // 循环输出该数组内容
    24         System.out.println("排序之后:");
    25         for (int a : arr) {
    26             System.out.print(a + "	");
    27         }
    28         System.out.println();
    29 
    30     }
    31 
    32     public static int getMiddle(int[] list, int low, int high) {
    33         int temp = list[low];
    34         while (low < high) {
    35             while (low < high && list[high] >= temp) {
    36                 high--;
    37             }
    38             list[low] = list[high];
    39             while (low < high && list[low] <= temp) {
    40                 low++;
    41             }
    42             list[high] = list[low];
    43         }
    44         list[low] = temp;
    45         return low;
    46     }
    47 
    48     public static void sort(int[] list, int low, int high) {
    49         if (low < high) {
    50             int middle = getMiddle(list, low, high);
    51             sort(list, low, middle - 1);
    52             sort(list, middle + 1, high);
    53         }
    54     }
    55 
    56 }

    下面这事控制台的输出

  • 相关阅读:
    数论 欧几里德算法 以及 欧几里得拓展
    数论 快速幂的原理讲解
    汉诺塔模板
    C++ 迭代器运算
    C++ STL vector set map 简易用法
    C++ 使用指向函数的指针数组
    Codeforces 718C 线段树+矩乘
    BZOJ 2506 分块
    Codeforces 455D 分块+链表
    Codeforces 19E 树上差分
  • 原文地址:https://www.cnblogs.com/javallh/p/8762175.html
Copyright © 2011-2022 走看看