package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func Swap(target []int, index int) {
	// Swaps the position of index with index+1 in a slice of integers
	temp := target[index]
	target[index] = target[index+1]
	target[index+1] = temp
}

func BubbleSort(target []int) {
	// Performs Bubble Sort on a target slice of integers
	for n := len(target); n > 1; n-- {
		for i := 1; i < n; i++ {
			if target[i] < target[i-1] {
				Swap(target, i-1)
			}
		}
	}
}

func ParseIntegers(integers string) []int {
	_integers := []int{}
	for _, v := range strings.Split(integers, " ") {
		_int, err := strconv.Atoi(v)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to parse integer %s", v)
		}
		_integers = append(_integers, _int)
	}
	return _integers
}

func main() {
	var userIntegers []int
	inputReader := bufio.NewReader(os.Stdin)
	fmt.Println("Type in a sequence of up to 10 integers separated by white space:")
	integersString, _ := inputReader.ReadString('\n')
	userIntegers = ParseIntegers(strings.Trim(integersString, "\n"))
	BubbleSort(userIntegers)

	fmt.Println(userIntegers)
}
