-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble.java
More file actions
29 lines (26 loc) · 810 Bytes
/
bubble.java
File metadata and controls
29 lines (26 loc) · 810 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
//Bubble sort
class bubble {
public static void printArray(int arr[]) {
for(int i=0; i<arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void main(String args[]) {
int arr[] = {7, 8, 1, 3, 2};
//time complexity = O(n^2)
//outter loop to count n-1 iterations where arr.length represents n
for(int i=0; i<arr.length-1; i++) {
//inner loop for the sorted elements
for(int j=0; j<arr.length-i-1; j++) {
if(arr[j] > arr[j+1]) {
//to swap
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printArray(arr);
}
}