Tag: bubble sort

This is coding algorithm – Scala code bubble sort for loop

object Sort{ def bubbleSort () : Unit = { var myArray = Array(4, 7, 8, 3, 1, 9, 2, 5, 0, 6) var buf:Int = 0; for(i <- 0 to (myArray.length-1)) { for (j <- 1 to (myArray.length)){ if (myArray(j) < myArray(j-1)){ buf = myArray(j) myArray(j) = myArray(j-1) myArray(j-1) = buf } } } println("SORTED") … Continue reading This is coding algorithm – Scala code bubble sort for loop

Sort numbers in ascending order using Python

This is Python code to sort the numbers in ascending order input: 3,0,1,4,2 out:0,1,2,3,4 Example is my: numbers = input("Enter numbers separated by a comma: ") numbers = [int(n) for n in numbers.split(',')] end = len(numbers) - 1 while end != 0: for i in range(end): if numbers[i] > numbers[i + 1]: numbers[i], numbers[i + … Continue reading Sort numbers in ascending order using Python

bubble sort scala

/** blue sort*/ def Bluesort(array : Array[Int]): Array[Int] = { var flag = false for(i<- 0 until array.length -1) if(array(i+1) > array(i)){ //tang dan,giam dan > val tmp = array(i) array(i) = array(i+1) array(i+1) = tmp flag = true } // Repeat until we don't have anymore swaps if(flag) Bluesort(array) else array } def main(args: … Continue reading bubble sort scala