Skip to content

Commit 8944462

Browse files
authored
Create BubbleSorting.java
1 parent 8bfc0d8 commit 8944462

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Array Programs/BubbleSorting.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.Scanner;
2+
3+
class BubbleSorting {
4+
public static void main(String args[])
5+
{
6+
Scanner scan = new Scanner(System.in);
7+
// Taking the length of array as input from user
8+
System.out.println("Enter the size of the array:");
9+
int len = scan.nextInt();
10+
// Creating array of the entered length
11+
int arr[] = new int[len];
12+
// Taking array contents as input from user
13+
System.out.println("Enter the array elements:");
14+
for (int i = 0; i <= arr.length-1; i++) {
15+
System.out.println("Enter an element:");
16+
arr[i] = scan.nextInt();
17+
}
18+
// Printing the array contents before sorting
19+
System.out.println("Array contents before sorting:");
20+
for(int i = 0; i <= arr.length-1; i++) {
21+
System.out.print(arr[i] + " ");
22+
}
23+
// Sorting the array contents in ascending order using bubble sort
24+
int help;
25+
for (int i = 0; i <= arr.length-2; i++) {
26+
for (int j = 0; j <= arr.length-2-i; j++) {
27+
if(arr[j] > arr[j+1]) {
28+
help = arr[j];
29+
arr[j] = arr[j+1];
30+
arr[j+1] = help;
31+
}
32+
}
33+
}
34+
// Printing the sorted array contents
35+
System.out.println();
36+
System.out.println("Sorted array contents are:");
37+
for(int i = 0; i <= arr.length-1; i++) {
38+
System.out.print(arr[i] + " ");
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)