Skip to content

Commit 30dd76d

Browse files
author
Abhishek-Jain-5
authored
Create BubbleSort.java
1 parent 0375fc7 commit 30dd76d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Java Library/BubbleSort.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Java program for implementation of Bubble Sort
2+
class BubbleSort
3+
{
4+
void bubbleSort(int arr[])
5+
{
6+
int n = arr.length;
7+
for (int i = 0; i < n-1; i++)
8+
for (int j = 0; j < n-i-1; j++)
9+
if (arr[j] > arr[j+1])
10+
{
11+
// swap temp and arr[i]
12+
int temp = arr[j];
13+
arr[j] = arr[j+1];
14+
arr[j+1] = temp;
15+
}
16+
}
17+
18+
/* Prints the array */
19+
void printArray(int arr[])
20+
{
21+
int n = arr.length;
22+
for (int i=0; i<n; ++i)
23+
System.out.print(arr[i] + " ");
24+
System.out.println();
25+
}
26+
27+
// Driver method to test above
28+
public static void main(String args[])
29+
{
30+
BubbleSort ob = new BubbleSort();
31+
int arr[] = {64, 34, 25, 12, 22, 11, 90};
32+
ob.bubbleSort(arr);
33+
System.out.println("Sorted array");
34+
ob.printArray(arr);
35+
}
36+
}

0 commit comments

Comments
 (0)