Shembull: Bubble Sort

package main

import (
    "fmt"
)

var list = []int{1, 900, 2, 4, 8, 6, 3, 2, 4, 8, 6, 7, 3, 0, -300}

func bubbleSort(input []int) []int {
    n := len(input)
    swapped := true
    for swapped {
        swapped = false
        for i := 1; i < n; i++ {
            if input[i-1] > input[i] {
                input[i], input[i-1] = input[i-1], input[i]
                swapped = true
            }
        }

        n--
    }

    return input
}

func main() {
    fmt.Println("Lista origjinale:")
    fmt.Println(list)
    fmt.Println("Lista e sortuar:")
    fmt.Println(bubbleSort(list))
}

https://play.golang.org/p/T_igbGHGwL4

Rezultati:

Lista origjinale:
[1 900 2 4 8 6 3 2 4 8 6 7 3 0 -300]
Lista e sortuar:
[-300 0 1 2 2 3 3 4 4 6 6 7 8 8 900]
All Rights Reserved Theme by 404 THEME.