Skip to content
This repository was archived by the owner on Aug 13, 2024. It is now read-only.

Commit 3c5f32c

Browse files
committed
BubbleSort & pair=sum of given number algorithms
1 parent 052fc91 commit 3c5f32c

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

src/main/java/BubbleSort.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
/**
3+
* @author medany
4+
*/
5+
6+
/*
7+
* (Best case) O(n) if the array is already sorted, that means no swap occurred.
8+
*
9+
* (Worst case) O(n^2) if the array is already sorted but in descending order.
10+
*/
11+
12+
public class BubbleSort {
13+
14+
public int[] solve(int[] a) {
15+
16+
int[] result = new int[3];
17+
int temp;
18+
for (int i = 0; i < a.length; i++) {
19+
for (int j = 0; j < a.length - 1; j++) {
20+
if (a[j] > a[j + 1]) {
21+
temp = a[j];
22+
a[j] = a[j + 1];
23+
a[j + 1] = temp;
24+
result[0] += 1;
25+
}
26+
}
27+
}
28+
result[1] = a[0];
29+
result[2] = a[a.length - 1];
30+
31+
return result;
32+
}
33+
34+
}

src/main/java/IceCreamParlor.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
/*
9+
* Each time Sunny and Johnny take a trip to the Ice Cream Parlor, they pool
10+
* together money dollars for ice cream. On any given day, the parlor offers a
11+
* line of n flavors. Each flavor, i, is numbered sequentially with a unique ID
12+
* number from 1 to n and has a cost, cost i, associated with it.
13+
*
14+
* Given the value of money and the cost of each flavor for t trips to the Ice
15+
* Cream Parlor, help Sunny and Johnny choose two distinct flavors such that
16+
* they spend their entire pool of money during each visit. For each trip to the
17+
* parlor, print the ID numbers for the two types of ice cream that Sunny and
18+
* Johnny purchase as two space-separated integers on a new line. You must print
19+
* the smaller ID first and the larger ID second.
20+
*/
21+
22+
public class IceCreamParlor {
23+
24+
public int[] solve(int money, int[] flavors) {
25+
Map<Integer, Integer> hash = new HashMap<>();
26+
for (int i = 0; i < flavors.length; i++) {
27+
int complement = money - flavors[i];
28+
if (hash.containsKey(complement)) {
29+
return new int[] { hash.get(complement) + 1, i + 1 };
30+
}
31+
hash.put(flavors[i], i);
32+
}
33+
return new int[] {};
34+
}
35+
}

test/main/java/BubbleSortTest.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
public class BubbleSortTest {
9+
10+
private BubbleSort alg = new BubbleSort();
11+
private int[] actual, expected;
12+
13+
@Test
14+
public void Test_1() {
15+
16+
actual = alg.solve(new int[] { 1, 2, 3 });
17+
expected = new int[] { 0, 1, 3 };
18+
19+
Assert.assertArrayEquals(expected, actual);
20+
}
21+
22+
@Test
23+
public void Test_2() {
24+
25+
actual = alg.solve(new int[] { 3, 2, 1 });
26+
expected = new int[] { 3, 1, 3 };
27+
28+
Assert.assertArrayEquals(expected, actual);
29+
}
30+
31+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
public class IceCreamParlorTest {
9+
10+
private static IceCreamParlor alg = new IceCreamParlor();
11+
private int[] actual, expected;
12+
13+
@Test
14+
public void Test_1() {
15+
16+
actual = alg.solve(4, new int[] { 1, 4, 5, 3, 2 });
17+
expected = new int[] { 1, 4 };
18+
19+
Assert.assertArrayEquals(expected, actual);
20+
}
21+
22+
@Test
23+
public void Test_2() {
24+
25+
actual = alg.solve(4, new int[] { 2, 2, 4, 3 });
26+
expected = new int[] { 1, 2 };
27+
28+
Assert.assertArrayEquals(expected, actual);
29+
}
30+
31+
@Test
32+
public void Test_3() {
33+
34+
actual = alg.solve(16, new int[] { 2, 7, 8, 5, 8, 3, 8 });
35+
expected = new int[] { 3, 5 };
36+
37+
Assert.assertArrayEquals(expected, actual);
38+
}
39+
}

0 commit comments

Comments
 (0)