求秩排序(計數排序)

來源:互聯網
上載者:User

package chapter1;</p><p>public class Ranksort {</p><p>/**<br /> * 求秩排序<br /> * @param args<br /> */</p><p>/**<br /> * 求秩函數<br /> * 一個序列中的元素的秩(rank)是序列中較小元素的數量加上出現在<br /> * 其左側的相等元素的數量<br /> */</p><p>public static void rank(int[] a, int[] r) {<br />if (r.length < a.length) {<br />throw new IllegalArgumentException("length of rank array cannot be "+<br />"less than the number of objects");<br />}<br />for (int i = 0; i < a.length; i++) {<br />r[i] = 0;<br />}<br />for (int i = 1; i < a.length; i++) {<br />for (int j = 0; j < i; j++) {<br />if (a[j] <= a[i]) {<br />r[i]++;<br />} else {<br />r[j]++;<br />}<br />}<br />}<br />}</p><p>/**<br /> * 秩的排序<br /> * @param args<br /> */</p><p>public static void rearrange(int[] a, int[] r) {<br />int[] u = new int[a.length];<br />for (int i = 0; i < a.length; i++) {<br />u[r[i]] = a[i];<br />}<br />for (int i = 0; i < a.length; i++) {<br />a[i] = u[i];<br />}<br />}</p><p>public static void main(String[] args) {<br />int a[] = {4, 3, 9, 3, 7};<br />int r[] = new int[5];<br />rank(a, r);<br />for (int i : r) {<br />System.out.print(i + " ");<br />}<br />System.out.println();<br />rearrange(a, r);<br />for (int l : a) {<br />System.out.print(l + " ");<br />}<br />}</p><p>}<br />

聯繫我們

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