Skip to content

Commit 763be29

Browse files
authored
Merge pull request #1 from jnimit99/jnimit99-patch-1
create bubble.java
2 parents d7389fc + cc07b0e commit 763be29

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Sorting algorithms/bubble.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class BubbleSort
2+
{
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+
{
10+
// swap temp and arr[i]
11+
int temp = arr[j];
12+
arr[j] = arr[j+1];
13+
arr[j+1] = temp;
14+
}
15+
}
16+
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+
public static void main(String args[])
26+
{
27+
BubbleSort ob = new BubbleSort();
28+
int arr[] = {64, 34, 25, 12, 22, 11, 90};
29+
ob.bubbleSort(arr);
30+
System.out.println("Sorted array");
31+
ob.printArray(arr);
32+
}
33+
}

0 commit comments

Comments
 (0)