快速排序 演算法 Java 實現

來源:互聯網
上載者:User

我的快速排序 演算法 Java實現。

View Code

 1 /* 2  * 目的:實現 快速排序 演算法 3  * 功能:對一組資料進行排序 4  * 作者:陳沛銳 5  * 時間:2013.04.04 6  *  7  * 經驗積累: 8  * 1.遞迴函式中提前寫好if()語句,防止漏寫; 9  */10 package part02.chapter07;11 12 import java.text.SimpleDateFormat;13 import java.util.Date;14 import java.util.Random;15 import java.util.Scanner;16 17 public class _1exercise {18 19     public static void main(String[] args) {20         System.out.println("請輸入您要產生的資料量(整數表示):");21         Scanner myScanner = new Scanner(System.in);22         int array_length = myScanner.nextInt();23         int[] A = new int[array_length];24         System.out.println("請輸入資料中的最大值(整數表示):");25         int array_max = myScanner.nextInt();26         System.out.println("請輸入資料中的最小值(整數表示):");27         int array_min = myScanner.nextInt();28         Random ran = new Random();29         System.out.println("隨機產生的資料如下:");30         for (int i = 0; i < array_length; i++) {31             A[i] = ran.nextInt(array_max - array_min + 1) + array_min;32             System.out.print(A[i] + " ");33         }34         System.out.println();35         SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");36         System.out.println(sdf.format(new Date()));37         QUICKSORT quickSort = new QUICKSORT();38         quickSort.sort(A, 0, array_length - 1);39         System.out.println(sdf.format(new Date()));40         System.out.println();41         System.out.println("通過快速排序後的資料如下:");42         for (int i = 0; i < array_length; i++) {43             System.out.print(A[i] + " ");44         }45     }46 47 }48 49 // 快速排序50 class QUICKSORT {51     public void sort(int[] A, int p, int r) {52         if (p < r) {// 注意遞迴條件的判斷53             int j = partition(A, p, r);54             sort(A, p, j - 1);55             sort(A, j + 1, r);56         }57     }58 59     public int partition(int[] A, int p, int r) {60         int i = p - 1;61         // 為了防止在for語句中每次都取出A[r]的值62         int x = A[r];63         int temp = 0;64         for (int j = p; j < r; j++) {65             if (A[j] < x) {66                 i++;67                 temp = A[i];68                 A[i] = A[j];69                 A[j] = temp;70             }71         }72         // 將i+1下標的值與A[j]進行交換73         A[r] = A[i + 1];74         A[i + 1] = x;75         return i + 1;76     }77 }78 /**79  * a sample output:80  * 請輸入您要產生的資料量(整數表示):81 1082 請輸入資料中的最大值(整數表示):83 2084 請輸入資料中的最小值(整數表示):85 186 隨機產生的資料如下:87 4 11 3 13 15 7 3 13 3 16 88 通過快速排序後的資料如下:89 3 3 3 4 7 11 13 13 15 16 90 */

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.