Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 231e28a

Browse files
Create BubbleSort.java (Py-Contributors#918)
1 parent 32619ab commit 231e28a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.Arrays;
2+
3+
class BubbleSort{
4+
5+
public static void main(String [] kichu){
6+
int [] arr = {7,6,5,3,4,2,1};
7+
Bubble_Sort(arr);
8+
System.out.println(Arrays.toString(arr));
9+
10+
}
11+
12+
/*
13+
1. Bubble Sort
14+
At every step comparing adjacent elements
15+
At the end of the compare and swaping the largest element will be at the end. */
16+
static void Bubble_Sort(int [] arr){
17+
int n = arr.length - 1;
18+
boolean isAsc;
19+
20+
for(int i = 0; i < n; ++i){
21+
isAsc = false;
22+
for(int j = 1; j < n-i; ++j){
23+
if(arr[j] < arr[j-1]){
24+
swap(arr,j,j-1);
25+
isAsc = true;
26+
}
27+
}
28+
// if no greater element found in the entire array means it is sorted
29+
if(!isAsc) return;
30+
}
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /