JAVA排序--[快速排序]

來源:互聯網
上載者:User

標籤:

 1 package com.array; 2  3 public class Sort_Quick { 4     /* 5      * 項目名稱:快速排序 ;  6      * 項目要求:用JAVA對數組進行排序,並運用快速排序演算法;  7      * Sevck; 8      */ 9     public void sort(int left, int right, int array[]) {10         int l = left;11         int r = right;12         int pirot = array[(left + right) / 2];13         int temp = 0;14         while (l < r) {15             while (array[l] < pirot && l >= left)16                 l++;17             while (array[r] > pirot && r <= right)18                 r--;19             if (l >= r)20                 break;21             temp = array[l];22             array[l] = array[r];23             array[r] = temp;24         }25 26         if (l == r) {27             l++;28             r--;29         }30 31         if (left < r)32             sort(left, r, array);33         if (right > l)34             sort(l, right, array);35     }36 37     public static void main(String[] args) {38         int arr1[] = { 7, 3, 2, 9, 15, 1, 14 };39         Sort_Quick qs = new Sort_Quick();40         qs.sort(0, arr1.length - 1, arr1);41 42         System.out.println("The current array is:");43 44         for (int i = 0; i < arr1.length; i++) {45             System.out.print("  " + arr1[i]);46         }47     }48 }

這個需要說說了,之前快排有處寫錯了,自己調試了好久沒解決,(結果發現的時候不細心--寫成++)。

看了半天沒看到,arr1.length - 1 怎麼會變成7...找雞哥問了,瞬間解決了,還被吐槽了代碼寫的好爛。。一點都不好看

不過看完雞哥的確實是自歎不如,好好學習了他的編碼風格...

JAVA排序--[快速排序]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.