Java實現幾種簡單排序__Java

來源:互聯網
上載者:User
Java的簡單排序主要有:插入排序,選擇排序,冒泡排序,順序排序。現在將其代碼粘貼如下:
public class Arrays {public static void main(String[] args) {int a[]=new int[]{7,6,5,4,3,2,1};//insertSort(a);//bubbleSort(a);selectedSort(a);print(a);}public static void print(int a[]){for(int i=0;i<a.length;i++){System.out.print(a[i]+" ");}System.out.println();}public static void bubbleSort(int a[]){//冒泡排序for(int i=0;i<a.length-1;i++){for(int j=i+1;j<a.length;j++){if(a[i]>a[j]){int temp=a[i];a[i]=a[j];a[j]=temp;}}}}public static void selectedSort(int a[]){//選擇排序for(int i=0;i<a.length-1;i++){//最多有a.length-1趟排序int min=i;for(int j=i+1;j<a.length;j++){if(a[j]<a[min]){min=j;}}int temp=a[min];a[min]=a[i];a[i]=temp;}}public static void insertSort(int a[]){//插入排序,只能採用覆蓋的方法int i,j;for(i=1;i<a.length;i++){int temp=a[i];for(j=i;j>0;j--){if(a[j-1]>temp){a[j]=a[j-1];}}a[j]=temp;}}}
<span style="font-family:Arial;background-color: rgb(255, 255, 255);">幾種排序的簡單比較:</span>
<span style="font-family:Arial;background-color: rgb(255, 255, 255);">插入排序:一般來說,在這幾種排序中效率最高;</span>
<span style="font-family:Arial;background-color: rgb(255, 255, 255);">選擇排序:相對於冒泡排序來說,降低了交換的次數;</span>
<span style="font-family:Arial;background-color: rgb(255, 255, 255);">冒泡排序:最為簡單,但是相對來說效率最低</span>

聯繫我們

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