forked from shakti1590/bubble-sort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimize.java
More file actions
33 lines (27 loc) · 760 Bytes
/
Optimize.java
File metadata and controls
33 lines (27 loc) · 760 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
33
//Bubble sort is a basic algorithm for arranging a string of numbers or other elements in the correct order.
import java.util.Arrays;
public class BubbleSort {
static void sort(int [] arrA){
if(arrA==null || arrA.length==0)
return;
System.out.println("Original Array: " + Arrays.toString(arrA));
int size = arrA.length;
for (int i = 0; i <size–1 ; i++) {
for (int j = 0; j <size–i–1 ; j++) {
//check the adjacent elements
// if(arrA[j]<arrA[j+1]) --- hima----
if(arrA[j]>arrA[j+1]){
//swap the elements
int temp = arrA[j];
arrA[j] = arrA[j+1];
arrA[j+1] = temp;
}
}
}
System.out.println("Sorted Array : " + Arrays.toString(arrA));
}
public static void main(String[] args) {
int [] arrA = {5, 1, 7, 3, 2, 10};
sort(arrA);
}
}