forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.java
43 lines (26 loc) · 884 Bytes
/
BubbleSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//WHAT IS BUBBLE SORT:
//Bubble Sort is one of the simplest sorting techniques in Java to sort the array elements.
//The swapping of elements continues until the array is sorted and no more swapping is required.
//We can also sort the elements in the descending order in which the smallest element goes at the end of the array in each iteration.
//THIS EXPLAINATION IS PROVIDED TO HELP BEGGINNER'S UNDERSTAND THE CODE
package arrays;
public class BubbleSort {
public static void main(String[] args) {
int a[] = {3, 1, -2, 7, 4, 0};
int n = a.length;
for (int i=0; i<n-1; i++) {
int minimumIndex = i;
for (int j=i; j<n; j++) {
if (a[j]<a[minimumIndex]) {
minimumIndex = j;
}
}
int temp = a[i];
a[i] = a[minimumIndex];
a[minimumIndex] = temp;
}
for (int e:a) {
System.out.print(e+" ");
}
}
}