swift實現冒泡排序

來源:互聯網
上載者:User

剛剛坐著沒事幹,就用swift語言寫了寫冒泡排序,還望高手指點、拍磚!哈哈!

廢話不多說,上代碼!

 1 import Cocoa 2  3 var array = [123,234,12,346,4,75,67,234,23,1233,3,5,986,98,567,345,234,234] 4  5 println("排序前的值:") 6  7 for item in array 8 { 9     var ii = item10     println(ii)11 }12 13 for var i = 0; i < array.count - 1; ++i {14     for var j = 0; j < array.count - 1 - i; ++j{15         if array[j] > array[j + 1] {16             var temp = array[j + 1]17             array[j + 1] = array[j]18             array[j] = temp19         }20     }21 }22 23 println("排序後的值:")24 25 for item in array26 {27     var ii = item28     println(ii)29 }

運行結果:

這裡的for迴圈使用的是C裡面傳統的for迴圈方式,請各位指教、以及拍磚!

聯繫我們

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