Demo entry 6647884

BubbleSort

   

Submitted by anonymous on Oct 22, 2017 at 05:37
Language: Swift. Code size: 698 Bytes.

//冒泡排序法 从小到大
func bubbleSort(myArray:[Int]) -> [Int] {
    var newArray = myArray
    for i in 0..<newArray.count{    // 0 1 2 3 4
        var temp = 0
        print("----")
        for j in (i..<newArray.count-1).reversed(){//  3 2 1 0
            print(newArray[j])
            if newArray[j] > newArray[j+1]{
                temp = newArray[j]
                newArray[j] = newArray[j+1]
                newArray[j+1] = temp
            }
        }
        
    }
    return newArray
}


var myArray:[Int] = [99,100,56,33,43]
var newArray = [Int]()

//newArray = selectionSort(myArray: myArray)
//print(newArray)

newArray = bubbleSort(myArray:myArray)
print(newArray)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).