forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSortAlgorithmEx.java
More file actions
32 lines (30 loc) · 970 Bytes
/
BubbleSortAlgorithmEx.java
File metadata and controls
32 lines (30 loc) · 970 Bytes
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
public class BubbleSortAlgorithmEx {
public static void main(String[] args) {
int[] array ={6,10,1,8,7,5,2};
System.out.println("Bubble Sort Algorithm");
System.out.println("----------------------");
System.out.println("All elements in the array : ");
for (int j : array) {
System.out.print(j + " ");
}
System.out.println();
bubbleSort(array);
System.out.println("Sorted Array : ");
for (int j : array) {
System.out.print(j + " ");
}
}
static void bubbleSort(int[] array) {
int n = array.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(array[j-1] > array[j]){
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
}
}
}
}