Answers for "bubble sort concept"

3

bubble sort code

func Sort(arr []int) []int {
	for i := 0; i < len(arr)-1; i++ {
		for j := 0; j < len(arr)-i-1; j++ {
			if arr[j] > arr[j+1] {
				temp := arr[j]
				arr[j] = arr[j+1]
				arr[j+1] = temp
			}
		}
	}
	return arr
}
Posted by: Guest on August-16-2020
1

bubble sort

// I Love Java
import java.io.*;
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.toList;

public class Bubble_Sort_P {
    public static void main(String[] args) throws IOException {
        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));

        List<Integer> arr = Stream.of(buffer.readLine().replaceAll("\\s+$", " ").split(" ")).map(Integer::parseInt)
                .collect(toList());

        calculate(arr);

        System.out.println(arr);
    }

    public static void calculate(List<Integer> arr) {
        for (int i = 0; i <= arr.size() - 2; i++) {
            if (arr.get(i) > arr.get(i + 1)) {
                int tem = arr.get(i);
                arr.set(i, arr.get(i + 1));
                arr.set(i + 1, tem);
                calculate(arr);
            }
        }
    }
}
Posted by: Guest on June-10-2021
0

Algorithm of bubble sort

begin BubbleSort(list)

   for all elements of list
      if list[i] > list[i+1]
         swap(list[i], list[i+1])
      end if
   end for
   
   return list
   
end BubbleSort
Posted by: Guest on May-27-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language