Skip to content

Commit 2ee67f9

Browse files
authored
bubble sort
1 parent 772fb91 commit 2ee67f9

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Main.java

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

0 commit comments

Comments
 (0)