Skip to content

Commit bab74f4

Browse files
committed
add CoutingInversion & Karatsuba
1 parent f13526e commit bab74f4

File tree

3 files changed

+227
-0
lines changed

3 files changed

+227
-0
lines changed

04/CountingInverison.java

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package hw04;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.util.StringTokenizer;
8+
9+
public class CountingInverison {
10+
private int[] arr;
11+
12+
public CountingInverison(String filePath){
13+
String data = this.readFile(filePath);
14+
StringTokenizer str = new StringTokenizer(data, " ");
15+
arr = new int[str.countTokens()];
16+
for(int i = 0; str.hasMoreTokens(); i++){
17+
arr[i] = Integer.parseInt(str.nextToken());
18+
}
19+
}
20+
21+
private int[] getArr(){
22+
return this.arr;
23+
}
24+
25+
public int doSortAndCount(){
26+
return sortAndCount(getArr(), 0, getArr().length - 1);
27+
}
28+
29+
private int sortAndCount(int[] arr, int start, int end){
30+
if(start == end){
31+
return 0;
32+
}
33+
34+
int halfIndex = (start + end) / 2;
35+
int leftInversionCounting = sortAndCount(arr, start, halfIndex);
36+
int rightInversionCounting = sortAndCount(arr, halfIndex+1, end);
37+
int mergeInversionCounting = mergeAndCount(arr, start, halfIndex, end);
38+
return leftInversionCounting + rightInversionCounting + mergeInversionCounting;
39+
}
40+
41+
private int mergeAndCount(int[] arr, int start, int mid, int end){
42+
int inversionCount = 0;
43+
int[] tempArr = new int[end - start + 1];
44+
int count = 0;
45+
int leftPointerIndex = start;
46+
int rightPointerIndex = mid + 1;
47+
int leftArrCount = mid - start + 1;
48+
int rightArrCount = end - (mid + 1) + 1;
49+
while(leftArrCount != 0 && rightArrCount != 0){
50+
if(arr[leftPointerIndex] > arr[rightPointerIndex]){
51+
tempArr[count] = arr[rightPointerIndex];
52+
inversionCount += leftArrCount;
53+
rightArrCount--;
54+
rightPointerIndex++;
55+
}else{
56+
tempArr[count] = arr[leftPointerIndex];
57+
leftArrCount--;
58+
leftPointerIndex++;
59+
}
60+
count++;
61+
}
62+
if(leftArrCount == 0){
63+
System.arraycopy(arr, rightPointerIndex, tempArr, count, rightArrCount);
64+
}else if(rightArrCount == 0){
65+
System.arraycopy(arr, leftPointerIndex, tempArr, count, leftArrCount);
66+
}
67+
System.arraycopy(tempArr, 0, getArr(), start, end - start + 1);
68+
69+
return inversionCount;
70+
}
71+
72+
private String readFile(String filePath) {
73+
BufferedReader br = null;
74+
StringTokenizer str = null;
75+
StringBuilder sb = null;
76+
String line;
77+
78+
try {
79+
br = new BufferedReader(new FileReader(filePath));
80+
sb = new StringBuilder();
81+
while ((line = br.readLine()) != null) {
82+
sb.append(line);
83+
}
84+
} catch (FileNotFoundException e) {
85+
e.printStackTrace();
86+
} catch (IOException e) {
87+
e.printStackTrace();
88+
} finally {
89+
if (br != null) {
90+
try {
91+
br.close();
92+
} catch (IOException e) {
93+
e.printStackTrace();
94+
}
95+
}
96+
return sb.toString();
97+
}
98+
}
99+
}

04/Karatsuba.java

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package hw04;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.math.BigInteger;
8+
import java.util.StringTokenizer;
9+
10+
public class Karatsuba {
11+
12+
private int[] arr;
13+
14+
public Karatsuba(String filePath){
15+
String data = this.readFile(filePath);
16+
StringTokenizer str = new StringTokenizer(data, " |\n");
17+
arr = new int[str.countTokens()];
18+
for(int i = 0; str.hasMoreTokens(); i++){
19+
arr[i] = Integer.parseInt(str.nextToken());
20+
}
21+
22+
}
23+
24+
private int[] getArr(){
25+
return this.arr;
26+
}
27+
28+
public BigInteger doKaratsuba(){
29+
return karatsuba(getArr()[0], getArr()[1]);
30+
}
31+
32+
// private int karatsuba(int a, int b){
33+
// int aCount = calculateIntNumber(a);
34+
// int bCount = calculateIntNumber(b);
35+
// int maxValueCount = max(aCount, bCount);
36+
// if(maxValueCount == 1){
37+
// return a * b;
38+
// }
39+
// int exponent = maxValueCount / 2;
40+
// int divisor = getDivisor(exponent);
41+
// int x1 = a / divisor;
42+
// int x2 = a - (x1 * divisor);
43+
// int y1 = b / divisor;
44+
// int y2 = b - (y1 * divisor);
45+
// int z0 = karatsuba(x2, y2);
46+
// int z2 = karatsuba(x1, y1);
47+
// int z1 = karatsuba((x2 + x1), (y2 + y1)) - z2 - z0;
48+
//
49+
// return (z2 * divisor * divisor) + z1 * divisor + z0;
50+
// }
51+
52+
private BigInteger karatsuba(int a, int b){
53+
int aCount = calculateIntNumber(a);
54+
int bCount = calculateIntNumber(b);
55+
int maxValueCount = max(aCount, bCount);
56+
if(maxValueCount == 1){
57+
return BigInteger.valueOf(a * b);
58+
}
59+
int exponent = maxValueCount / 2;
60+
int divisor = getDivisor(exponent);
61+
int x1 = a / divisor;
62+
int x2 = a - (x1 * divisor);
63+
int y1 = b / divisor;
64+
int y2 = b - (y1 * divisor);
65+
BigInteger z0 = karatsuba(x2, y2);
66+
BigInteger z2 = karatsuba(x1, y1);
67+
BigInteger z1 = karatsuba((x2 + x1), (y2 + y1)).subtract(z2).subtract(z0);
68+
69+
BigInteger bigDivisor = BigInteger.valueOf(divisor);
70+
return z2.multiply(bigDivisor.pow(2)).add(z1.multiply(bigDivisor)).add(z0);
71+
}
72+
73+
private int getDivisor(int exponent){
74+
int number = 1;
75+
while(exponent > 0){
76+
number *= 10;
77+
exponent--;
78+
}
79+
return number;
80+
}
81+
82+
private int max(int a, int b){
83+
return (a >= b) ? a : b;
84+
}
85+
86+
87+
public int calculateIntNumber(int number){
88+
int count = 0;
89+
while(number > 0){
90+
number /= 10;
91+
count++;
92+
}
93+
return count;
94+
}
95+
96+
private String readFile(String filePath) {
97+
BufferedReader br = null;
98+
StringTokenizer str = null;
99+
StringBuilder sb = null;
100+
String line;
101+
102+
try {
103+
br = new BufferedReader(new FileReader(filePath));
104+
sb = new StringBuilder();
105+
while ((line = br.readLine()) != null) {
106+
sb.append(line+"\n");
107+
}
108+
} catch (FileNotFoundException e) {
109+
e.printStackTrace();
110+
} catch (IOException e) {
111+
e.printStackTrace();
112+
} finally {
113+
if (br != null) {
114+
try {
115+
br.close();
116+
} catch (IOException e) {
117+
e.printStackTrace();
118+
}
119+
}
120+
return sb.toString();
121+
}
122+
}
123+
}

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
# Algorithm
2+
*****
3+
1. BinaryInsertionSort / InsertionSort / MergeSort
4+
2. HeapSort / QuickSort
5+
3. Closest Pair of Points / Loop Invariant
6+
4. Counting Inversion / Karatsuba

0 commit comments

Comments
 (0)