MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / BubbleSort

Class BubbleSort

Java/Array/BubbleSort.java:45–86  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

43import java.util.Scanner;
44
45public class BubbleSort {
46
47 public static void printArray(int [] arr) {
48 System.out.println("The sorted array is: ");
49 int n = arr.length;
50 for (int i = 0; i < n; i++) {
51 System.out.print(arr[i] + " ");
52 }
53 }
54
55 public static void bubbleSort(int [] arr) {
56 int n = arr.length;
57 for (int i = 0; i < n - 1- i; i++) {
58 for (int j = 0; j < n - 1; j++) {
59 if ( arr[j] > arr[j + 1]) {
60 int temp = arr[j];
61 arr[j] = arr[j + 1];
62 arr[j + 1] = temp;
63 }
64 }
65 }
66 }
67
68 public static int[] takeInput() {
69 Scanner sc = new Scanner(System.in);
70 System.out.println("Enter the length of the array: ");
71 int n = sc.nextInt();
72 int [] arr = new int[n];
73 System.out.println("Enter the elements of the array: ");
74 for (int i = 0; i < n; i++) {
75 arr[i] = sc.nextInt();
76 }
77 return arr;
78 }
79
80 public static void main(String[] args) {
81 int [] arr = takeInput();
82 bubbleSort(arr);
83 printArray(arr);
84
85 }
86}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected