zoukankan      html  css  js  c++  java
  • 基本排序算法——快速排序java实现

    简单的快速排序算法,我竟然花费了如此多的时间来写作,好好学习。

     1 /**
     2  * 
     3  */
     4 package basic.sort;
     5 
     6 import java.util.Arrays;
     7 import java.util.Random;
     8 
     9 public class QuickSort {
    10 
    11     
    12     public static <AnyType extends Comparable<? super AnyType>>
    13     void quickSort(AnyType[] arr){
    14         if(arr == null || arr.length ==0){
    15             return ;
    16         }
    17         AnyType key = arr[0];
    18         int low = 0,high =arr.length -1;
    19         while(low < high){
    20             while(arr[high].compareTo(key) > 0 && high > 0){
    21                 --high ;
    22             }
    23             while(arr[low].compareTo(key) <= 0 && low < arr.length-1){
    24                 ++low;
    25             }
    26             if(low < high){
    27                 AnyType tmp = arr[low];
    28                 arr[low] = arr[high];
    29                 arr[high] = tmp;
    30             }
    31         }
    32         arr[0] = arr[high];
    33         arr[high] = key;
    34         
    35         //println(Arrays.toString(arr));
    36         
    37         quickSort(arr, 0 ,high-1);
    38         quickSort(arr,high+1,arr.length-1);
    39     }
    40     
    41     public static <AnyType extends Comparable<? super AnyType>>
    42     void quickSort(AnyType[] arr, int l,int h){
    43         if(arr == null || arr.length ==0
    44                 || l >= h){
    45             return ;
    46         }
    47         AnyType key = arr[l];
    48         int low = l+1; 
    49         int high = h;
    50         
    51         while(low < high){
    52             while(arr[high].compareTo(key) > 0 && high > l ){
    53                 --high ;
    54             }
    55             while(arr[low].compareTo(key) < 0 && low < h){
    56                 ++low;
    57             }
    58             if(low < high){
    59                 AnyType tmp = arr[low];
    60                 arr[low] = arr[high];
    61                 arr[high] = tmp;
    62             }
    63         }
    64         arr[l] = arr[high];
    65         arr[high] = key;
    66         
    67         quickSort(arr, l ,high-1);
    68         quickSort(arr,high+1,h);
    69     }
    70     
    71     
    72     
    73     public static void main(String[] args) {
    74         Random rand = new Random();
    75         Integer[] arr = new Integer[10];
    76         for(int i = 0 ;i <10 ;i++){
    77             arr[i] = rand.nextInt(1000);
    78         }
    79         println(Arrays.toString(arr));
    80         quickSort(arr);
    81         println(Arrays.toString(arr));
    82     }
    83     
    84     public static void println(String str){
    85         System.out.println(str);        
    86     }
    87 
    88 }

    认真努力

  • 相关阅读:
    对类对象使用new时地址分配的情况
    c++堆与栈的简单认识
    多态公有继承
    Cookie & Session & JSP入门
    Response & ServletContext
    Java网络编程篇文章阅读顺序
    URL编程
    UDP网络编程
    TCP网络编程
    InetAddress类的使用
  • 原文地址:https://www.cnblogs.com/xuddong/p/3290469.html
Copyright © 2011-2022 走看看