Skip to content

Commit e6aa4f1

Browse files
authored
Merge pull request #68 from TORRYNN/Divide
Completed Divide and Conqueror
2 parents e8be31c + 4493ae0 commit e6aa4f1

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

25_Divide and Conquer/PracticeQs2.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class PracticeQs2 {
44
// largest count.
55
// Time Complexity:O(n2);
66
public static int MajorityElement(int[] nums) {
7-
// We know that majorit count we be more than half of the element.
7+
// We know that majority count we be more than half of the element.
88
int majoritycount = nums.length / 2;
99

1010
for (int i = 0; i < nums.length; i++) {
@@ -55,7 +55,7 @@ public static int majoritElementCount(int[] numbers, int low, int high) {
5555
}
5656

5757
public static void main(String[] args) {
58-
int nums[] = { 2, 2, 1, 1, 1, 1, 1, 2, 2 };
58+
int nums[] = { 2, 2, 1, 1, 1, 1, 1, 3, 7 };
5959
System.out.println(MajorityElement(nums));
6060
System.out.println(majoritElementCount(nums, 0, nums.length - 1));
6161
}
1.15 KB
Binary file not shown.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// InversionCount
2+
public class PracticeQs3 {
3+
// Brute Force Approach
4+
// Time Complexity: O(n^2)
5+
public static int InversionCount(int arr[], int low, int high) {
6+
int count = 0;
7+
for (int i = 0; i < high; i++) {
8+
for (int j = i + 1; j < high; j++) {
9+
if (arr[i] > arr[j] && i < j) {
10+
count++;
11+
}
12+
}
13+
}
14+
return count;
15+
16+
}
17+
// Using Divide and Conqueror Technique
18+
// Time Complexity:O(nlogn)
19+
// Modified MergeSort
20+
public static int MergeSort(int arr[], int low, int high) {
21+
22+
int InversionCount = 0;
23+
if (low >= high) {
24+
return 0;
25+
}
26+
27+
int mid = low + (high - low) / 2;
28+
InversionCount = MergeSort(arr, low, mid);
29+
InversionCount += MergeSort(arr, mid + 1, high);
30+
InversionCount += Merge(arr, low, mid, high);
31+
32+
return InversionCount;
33+
}
34+
35+
private static int Merge(int arr[], int low, int mid, int high) {
36+
int temp[] = new int[high - low + 1];
37+
int InversionCount = 0;
38+
int i = low;
39+
int j = mid + 1;
40+
int k = 0;
41+
42+
while (i <= mid && j <= high) {
43+
if (arr[i] <= arr[j]) {
44+
temp[k] = arr[i];
45+
i++;
46+
} else {
47+
temp[k] = arr[j];
48+
InversionCount += (mid - i+1);
49+
j++;
50+
}
51+
k++;
52+
53+
}
54+
// if there is some remaining element in left part or high part
55+
while (i <= mid) {
56+
temp[k] = arr[i];
57+
k++;
58+
i++;
59+
}
60+
while (j <= high) {
61+
temp[k] = arr[j];
62+
k++;
63+
j++;
64+
}
65+
for (i = low, k = 0; i <= high; i++, k++) {
66+
arr[i] = temp[k];
67+
}
68+
69+
return InversionCount;
70+
71+
}
72+
73+
public static void main(String[] args) {
74+
int nums[] = { 2, 4, 1, 3, 5 };
75+
System.out.println(InversionCount(nums, 0, nums.length - 1));
76+
System.out.println(MergeSort(nums, 0, nums.length - 1));
77+
}
78+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8

0 commit comments

Comments
 (0)