Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ff3a90d

Browse files
add SelectionProblem
1 parent 0eb17a2 commit ff3a90d

File tree

4 files changed

+91
-76
lines changed

4 files changed

+91
-76
lines changed

‎src/main/java/ir/sk/algorithm/mathematic/Mathematical.java‎

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -500,67 +500,6 @@ public static double meanUsingCountingSort(int array[]) {
500500
return mode;
501501
}
502502

503-
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
504-
505-
/**
506-
* median = "the middle" value
507-
*/
508-
@TimeComplexity("O(n Log n) as we need to sort the array first")
509-
public static double median(int a[]) {
510-
// First we sort the array
511-
Arrays.sort(a);
512-
513-
// check for even case
514-
if (a.length % 2 != 0)
515-
return a[a.length / 2];
516-
517-
return (double) (a[(a.length - 1) / 2] + a[a.length / 2]) / 2.0;
518-
}
519-
520-
@TimeComplexity("O(n + p) = O(n)")
521-
@SpaceComplexity("O(p) where P is the size of auxiliary array")
522-
public static double medianUsingCountingSort(int[] array) {
523-
int n = array.length;
524-
525-
int max = Arrays.stream(array).max().getAsInt();
526-
527-
// Frequency Array
528-
int[] counting = new int[max + 1];
529-
530-
// store count of each character
531-
for (int i = 0; i < n; i++) {
532-
counting[array[i]]++;
533-
}
534-
535-
double median = 0;
536-
if (n % 2 == 0) {
537-
Integer m1 = null;
538-
Integer m2 = null;
539-
int count = 0;
540-
for (int j = 0; j < counting.length; j++) {
541-
count += counting[j];
542-
if (m1 == null && count >= n / 2) {
543-
m1 = j;
544-
}
545-
if (m2 == null && count >= n / 2 + 1) {
546-
m2 = j;
547-
break;
548-
}
549-
}
550-
median = (m1 + m2) / 2.0;
551-
} else {
552-
int count = 0;
553-
for (int j = 0; j < counting.length; j++) {
554-
count += counting[j];
555-
if (count > n / 2) {
556-
median = j;
557-
break;
558-
}
559-
}
560-
}
561-
return median;
562-
}
563-
564503
/**
565504
* Mode = frequently occurring element in an array
566505
*/
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package ir.sk.algorithm.mathematic;
2+
3+
import java.util.*;
4+
5+
import ir.sk.helper.complexity.SpaceComplexity;
6+
import ir.sk.helper.complexity.TimeComplexity;
7+
8+
public class SelectionProblem {
9+
10+
/**
11+
* median = "the middle" value
12+
*/
13+
@TimeComplexity("O(n Log n) as we need to sort the array first")
14+
public static double median(int a[]) {
15+
// First we sort the array
16+
Arrays.sort(a);
17+
18+
// check for even case
19+
if (a.length % 2 != 0)
20+
return a[a.length / 2];
21+
22+
return (double) (a[(a.length - 1) / 2] + a[a.length / 2]) / 2.0;
23+
}
24+
25+
@TimeComplexity("O(n + p) = O(n)")
26+
@SpaceComplexity("O(p) where P is the size of auxiliary array")
27+
public static double medianUsingCountingSort(int[] array) {
28+
int n = array.length;
29+
30+
int max = Arrays.stream(array).max().getAsInt();
31+
32+
// Frequency Array
33+
int[] counting = new int[max + 1];
34+
35+
// store count of each character
36+
for (int i = 0; i < n; i++) {
37+
counting[array[i]]++;
38+
}
39+
40+
double median = 0;
41+
if (n % 2 == 0) {
42+
Integer m1 = null;
43+
Integer m2 = null;
44+
int count = 0;
45+
for (int j = 0; j < counting.length; j++) {
46+
count += counting[j];
47+
if (m1 == null && count >= n / 2) {
48+
m1 = j;
49+
}
50+
if (m2 == null && count >= n / 2 + 1) {
51+
m2 = j;
52+
break;
53+
}
54+
}
55+
median = (m1 + m2) / 2.0;
56+
} else {
57+
int count = 0;
58+
for (int j = 0; j < counting.length; j++) {
59+
count += counting[j];
60+
if (count > n / 2) {
61+
median = j;
62+
break;
63+
}
64+
}
65+
}
66+
return median;
67+
}
68+
69+
}

‎src/test/java/ir/sk/algorithm/array/MathematicalTest.java‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,6 @@ public void meanUsingCountingSort() {
124124
Assert.assertEquals(expectedValue, Mathematical.meanUsingCountingSort(a) + "");
125125
}
126126

127-
128-
@Test
129-
public void median() {
130-
int a[] = {1, 3, 4, 2, 7, 5, 8, 6};
131-
String expectedValue = "4.5";
132-
Assert.assertEquals(expectedValue, Mathematical.median(a) + "");
133-
}
134-
135-
@Test
136-
public void medianUsingCountingSort() {
137-
int a[] = {1, 3, 4, 2, 7, 5, 8, 6};
138-
String expectedValue = "4.5";
139-
Assert.assertEquals(expectedValue, Mathematical.medianUsingCountingSort(a) + "");
140-
}
141-
142127
@Test
143128
public void isPrime() {
144129
boolean expectedValue = true;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package ir.sk.algorithm.array;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import ir.sk.algorithm.mathematic.SelectionProblem;
7+
8+
public class SelectionProblemTest {
9+
@Test
10+
public void median() {
11+
int a[] = {1, 3, 4, 2, 7, 5, 8, 6};
12+
String expectedValue = "4.5";
13+
Assert.assertEquals(expectedValue, SelectionProblem.median(a) + "");
14+
}
15+
16+
@Test
17+
public void medianUsingCountingSort() {
18+
int a[] = {1, 3, 4, 2, 7, 5, 8, 6};
19+
String expectedValue = "4.5";
20+
Assert.assertEquals(expectedValue, SelectionProblem.medianUsingCountingSort(a) + "");
21+
}
22+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /