MCPcopy Create free account
hub / github.com/PrajaktaSathe/Java / BubbleSort

Class BubbleSort

Programs/BubbleSort.java:2–23  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1// Program to sort an array in Java (using bubble sort) -
2public class BubbleSort {
3 public static void main(String[] args) {
4 int arr[] = {10, 45, 15, 13, 54}; // Initializing an array
5 int temp;
6 // Two for loops to traverse the array -
7 for (int i = 0; i < arr.length - 1; i++) {
8 for (int j = i + 1; j < arr.length; j++) {
9 // check if element is greater -
10 if (arr[i] > arr[j]) {
11 temp = arr[i]; // assign arr[i] element to temp position
12 arr[i] = arr[j]; // assign arr[j] element to arr[i] position
13 arr[j] = temp; // assign temp value to arr[j] position
14 }
15 }
16 }
17 System.out.println("Sorted array: ");
18 // For loop to print the sorted array -
19 for (int i = 0; i < arr.length; i++) {
20 System.out.println(arr[i]);
21 }
22 }
23}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected