-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHomogeneousSeparation
56 lines (43 loc) · 1.19 KB
/
HomogeneousSeparation
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
44
45
46
47
48
49
50
51
52
53
54
55
56
package array;
/**
* @author bibhash.kumar
*/
public class HomogeneousSeparation {
// Time Complexity for array1 approach is 2n whereas for array2 is 3n/2.
public static void main(String[] args) {
int array[] = new int[] { -3, 1, 2, -4, -2, 5, 6, 2 };
int array2[] = new int[] { -3, 1, 2, -4, -2, 5, 6, 2 };
int temp, timeCpmplexityArray1 = 0, timeComplexityArray2 = 0;
for (int i = 0, j = 0; i < array.length; i++) {
timeCpmplexityArray1++;
if (array[i] < 0) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
j++;
}
}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
timeCpmplexityArray1++;
}
System.out.println();
int j = 0;
for (int i = 0; i < array2.length; i++) {
timeComplexityArray2++;
if (array2[i] > -1) {
temp = array2[i];
array2[i] = array2[j];
array2[j] = temp;
System.out.print(array2[j] + " ");
j++;
}
}
for (int i = j; i < array2.length; i++) {
System.out.print(array2[i] + " ");
timeComplexityArray2++;
}
System.out.println("\n timeCpmplexityArray1" + " = " + timeCpmplexityArray1 +
" and timeComplexityArray2" + " = " + timeComplexityArray2);
}
}