-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
34 lines (29 loc) · 773 Bytes
/
BubbleSort.java
File metadata and controls
34 lines (29 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.sorting;
public class BubbleSort {
public static void main(String[] args) {
int arr[] = { 7, 8, 3, 1, 2 };
// int arr[] = {1,2,3,7,8};
BeforeSorting(arr);
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
AfterSorting(arr);
}
public static void BeforeSorting(int arr[]) {
System.out.println("Before sorting:");
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
public static void AfterSorting(int arr[]) {
System.out.println();
System.out.println("After sorting:");
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
}