Java數組排序(冒泡,選擇,插入,希爾)

來源:互聯網
上載者:User

public class SortAll { </p><p>/**<br /> * 冒泡排序,選擇排序,插入排序,希爾(Shell)排序 Java的實現<br /> * 2008.11.09<br /> * @author YangL. (http://www.idcn.org)<br /> */<br />public static void main(String[] args) {<br /> int[] i = { 1, 5, 6, 12, 4, 9, 3, 23, 39, 403, 596, 87 };<br /> System.out.println("----冒泡排序的結果:");<br /> maoPao(i);<br /> System.out.println();<br /> System.out.println("----選擇排序的結果:");<br /> xuanZe(i);<br /> System.out.println();<br /> System.out.println("----插入排序的結果:");<br /> chaRu(i);<br /> System.out.println();<br /> System.out.println("----希爾(Shell)排序的結果:");<br /> shell(i);<br />} </p><p>// 冒泡排序<br />public static void maoPao(int[] x) {<br /> for (int i = 0; i < x.length; i++) {<br /> for (int j = i + 1; j < x.length; j++) {<br /> if (x[i] > x[j]) {<br /> int temp = x[i];<br /> x[i] = x[j];<br /> x[j] = temp;<br /> }<br /> }<br /> }<br /> for (int i : x) {<br /> System.out.print(i + " ");<br /> }<br />} </p><p>// 選擇排序<br />public static void xuanZe(int[] x) {<br /> for (int i = 0; i < x.length; i++) {<br /> int lowerIndex = i;<br /> // 找出最小的一個索引<br /> for (int j = i + 1; j < x.length; j++) {<br /> if (x[j] < x[lowerIndex]) {<br /> lowerIndex = j;<br /> }<br /> }<br /> // 交換<br /> int temp = x[i];<br /> x[i] = x[lowerIndex];<br /> x[lowerIndex] = temp;<br /> }<br /> for (int i : x) {<br /> System.out.print(i + " ");<br /> }<br />} </p><p>// 插入排序<br />public static void chaRu(int[] x) {<br /> for (int i = 1; i < x.length; i++) {// i從一開始,因為第一個數已經是排好序的啦<br /> for (int j = i; j > 0; j--) {<br /> if (x[j] < x[j - 1]) {<br /> int temp = x[j];<br /> x[j] = x[j - 1];<br /> x[j - 1] = temp;<br /> }<br /> }<br /> }<br /> for (int i : x) {<br /> System.out.print(i + " ");<br /> }<br />} </p><p>// 希爾排序<br />public static void shell(int[] x) {<br /> // 分組<br /> for (int increment = x.length / 2; increment > 0; increment /= 2) {<br /> // 每個組內排序<br /> for (int i = increment; i < x.length; i++) {<br /> int temp = x[i];<br /> int j = 0;<br /> for (j = i; j >= increment; j -= increment) {<br /> if (temp < x[j - increment]) {<br /> x[j] = x[j - increment];<br /> } else {<br /> break;<br /> }<br /> }<br /> x[j] = temp;<br /> }<br /> } </p><p> for (int i : x) {<br /> System.out.print(i + " ");<br /> }<br />}<br />}<br />public class SortAll { </p><p> /**<br /> * 冒泡排序,選擇排序,插入排序,希爾(Shell)排序 Java的實現<br /> * 2008.11.09<br /> * @author YangL. (http://www.idcn.org)<br /> */<br /> public static void main(String[] args) {<br /> int[] i = { 1, 5, 6, 12, 4, 9, 3, 23, 39, 403, 596, 87 };<br /> System.out.println("----冒泡排序的結果:");<br /> maoPao(i);<br /> System.out.println();<br /> System.out.println("----選擇排序的結果:");<br /> xuanZe(i);<br /> System.out.println();<br /> System.out.println("----插入排序的結果:");<br /> chaRu(i);<br /> System.out.println();<br /> System.out.println("----希爾(Shell)排序的結果:");<br /> shell(i);<br /> } </p><p> // 冒泡排序<br /> public static void maoPao(int[] x) {<br /> for (int i = 0; i < x.length; i++) {<br /> for (int j = i + 1; j < x.length; j++) {<br /> if (x[i] > x[j]) {<br /> int temp = x[i];<br /> x[i] = x[j];<br /> x[j] = temp;<br /> }<br /> }<br /> }<br /> for (int i : x) {<br /> System.out.print(i + " ");<br /> }<br /> } </p><p> // 選擇排序<br /> public static void xuanZe(int[] x) {<br /> for (int i = 0; i < x.length; i++) {<br /> int lowerIndex = i;<br /> // 找出最小的一個索引<br /> for (int j = i + 1; j < x.length; j++) {<br /> if (x[j] < x[lowerIndex]) {<br /> lowerIndex = j;<br /> }<br /> }<br /> // 交換<br /> int temp = x[i];<br /> x[i] = x[lowerIndex];<br /> x[lowerIndex] = temp;<br /> }<br /> for (int i : x) {<br /> System.out.print(i + " ");<br /> }<br /> } </p><p> // 插入排序<br /> public static void chaRu(int[] x) {<br /> for (int i = 1; i < x.length; i++) {// i從一開始,因為第一個數已經是排好序的啦<br /> for (int j = i; j > 0; j--) {<br /> if (x[j] < x[j - 1]) {<br /> int temp = x[j];<br /> x[j] = x[j - 1];<br /> x[j - 1] = temp;<br /> }<br /> }<br /> }<br /> for (int i : x) {<br /> System.out.print(i + " ");<br /> }<br /> } </p><p> // 希爾排序<br /> public static void shell(int[] x) {<br /> // 分組<br /> for (int increment = x.length / 2; increment > 0; increment /= 2) {<br /> // 每個組內排序<br /> for (int i = increment; i < x.length; i++) {<br /> int temp = x[i];<br /> int j = 0;<br /> for (j = i; j >= increment; j -= increment) {<br /> if (temp < x[j - increment]) {<br /> x[j] = x[j - increment];<br /> } else {<br /> break;<br /> }<br /> }<br /> x[j] = temp;<br /> }<br /> } </p><p> for (int i : x) {<br /> System.out.print(i + " ");<br /> }<br /> }<br />}<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.