Demo entry 6647867

SelectionSort

   

Submitted by anonymous on Oct 22, 2017 at 04:27
Language: Swift. Code size: 618 Bytes.

import Cocoa

// 选择排序法 从小到大
func selectionSort(myArray:[Int]) -> [Int] {
    var newArray = myArray
    var index = 0
    var temp = 0
    for i in 0..<newArray.count{
        print(myArray[i])
        index = i
        for j in i+1..<(newArray.count){
            if newArray[index] > newArray[j]{
                index = j
            }
        }
        temp = newArray[i]
        newArray[i] = newArray[index]
        newArray[index] = temp
    }
    return newArray
}
var myArray:[Int] = [99,100,56,33,43]
var newArray = [Int]()
newArray = selectionSort(myArray: myArray)
print(newArray)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).