-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem no. 44.java
43 lines (34 loc) · 1.42 KB
/
Problem no. 44.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
// Ques: Sort Colors aka bubble sort
// Ques Link: https://leetcode.com/problems/sort-colors/
// NOTE : While submitting on leetcode just remove the sawp part(comment below which one you have to remove) over there then it will work ...
package com.nitin;
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] arr = {5, 4, 3, 2, 1};
bubble(arr);
System.out.println(Arrays.toString(arr));
}
static void bubble(int[] arr) {
boolean swapped;
// run the steps n-1 times
for (int i = 0; i < arr.length; i++) {
swapped = false; // remove this line to submit in leetCode
// for each step, max index will come at the last respective index
for (int j = 1; j < arr.length - i; j++) {
// swap if the item is smaller than the previous item
if (arr[j] < arr[j-1]) {
// swap
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
swapped =true; // remove this line to submit in leetCode
}
}
// if you did not swap for a particular value of i, it means array is sorted hence stop the program
if (!swapped){ // !false = true // remove this if part also to submit in leetCode
break;
}
}
}
}