Skip to content

Commit 4c6e8d2

Browse files
authored
Added the program for Bubble Sort and updated readme file
1 parent 703fcf0 commit 4c6e8d2

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ This is a repository containing various Java Programs to understand the basic co
88

99
Java Codes for solving some of the Arrays related problems.
1010

11+
* [Searching](https://github.com/muskanmi/Data-Structures-Java/tree/main/Searching):
12+
13+
Java Codes for various searching algorithms.
14+
15+
* [Sorting](https://github.com/muskanmi/Data-Structures-Java/tree/main/Sorting):
16+
17+
Java Codes for various sorting algorithms.
18+
1119
* [Recursion](https://github.com/muskanmi/Data-Structures-Java/tree/main/recursion):
1220

1321
Java Codes for solving some problems of recursion.

Sorting/BubbleSort.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.io.*;
2+
import java.util.Scanner;
3+
4+
public class BubbleSort {
5+
static void bubbleSort(int[] arr) {
6+
int n = arr.length;
7+
int t = 0;
8+
for(int i=0; i<n; i++){
9+
for(int j=1; j<(n-i); j++){
10+
if(arr[j-1] > arr[j]){
11+
t = arr[j-1];
12+
arr[j-1] = arr[j];
13+
arr[j] = t;
14+
}
15+
}
16+
}
17+
}
18+
19+
public static void main(String a[]){
20+
Scanner scanner = new Scanner(System.in);
21+
22+
System.out.print("Enter the number of elements in the array: ");
23+
int n = scanner.nextInt();
24+
int arr[] = new int[n];
25+
26+
System.out.println("Enter the elements of the array:");
27+
for (int i=0; i<n; i++) {
28+
arr[i] = scanner.nextInt();
29+
}
30+
31+
bubbleSort(arr);
32+
33+
System.out.println("Sorted Array:");
34+
for (int i=0; i<n; i++) {
35+
System.out.print(arr[i]+ " ");
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)