-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
191 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package main.java.ua.com.florin.algorithm; | ||
|
||
/** | ||
* Contains static methods with custom realizations of different sorting algorithms for sorting an array | ||
* <p/> | ||
* Created by florin on 06.08.2014. | ||
*/ | ||
public class Sorter { | ||
|
||
/** | ||
* | ||
* @param a target array | ||
*/ | ||
public static void sortByInsertion(int[] a) { | ||
for (int i = 1; i < a.length; i++) { | ||
for (int j = i; j > 0 && a[j] < a[j - 1]; j--) { | ||
swap(a, j, j - 1); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param a target array | ||
*/ | ||
public static void sortBySelection(int[] a) { | ||
for (int i = 1; i < a.length - 1; i++) { | ||
for (int j = i + 1; j < a.length; j++) { | ||
if (a[j] < a[i]) { | ||
swap(a, i, j); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param a target array | ||
*/ | ||
public static void sortByBubble(int[] a) { | ||
for (int i = 0; i < a.length; i++) { | ||
for (int j = 0; j < a.length - i - 1; j++) { | ||
if (a[j] > a[j + 1]) { | ||
swap(a, j, j + 1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param a target array | ||
* @param low | ||
* @param high | ||
*/ | ||
public static void sortByQuick(int[] a, int low, int high) { | ||
int i = low; | ||
int j = high; | ||
int pivot = a[a.length / 2]; | ||
while (i <= j) { | ||
while (a[i] < pivot) { | ||
i++; | ||
} | ||
while (a[j] > pivot) { | ||
j--; | ||
} | ||
if (i <= j) { | ||
swap(a, i, j); | ||
i++; | ||
j--; | ||
} | ||
} | ||
if (low < j) { | ||
sortByQuick(a, low, j); | ||
} | ||
if (i < high) { | ||
sortByQuick(a, i, high); | ||
} | ||
} | ||
|
||
/** | ||
* Swaps the elements in the given array | ||
* | ||
* @param a target array | ||
* @param i first element to be swapped | ||
* @param j second element to be swapped | ||
*/ | ||
private static void swap(int[] a, int i, int j) { | ||
int tmp = a[i]; | ||
a[i] = a[j]; | ||
a[j] = tmp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main.java.ua.com.florin.findmissing; | ||
|
||
/** | ||
* Created by florin on 06.08.2014. | ||
*/ | ||
public class FindMissing { | ||
|
||
/** | ||
* Finds the missing number in the array of consecutive numbers | ||
* | ||
* @param a an array with consecutive numbers with one missing | ||
* @return the missing number | ||
*/ | ||
public static int findMissing(int[] a) { | ||
|
||
// sum of all elements in the array | ||
int fullSum = 0; | ||
// number of elements in the array, supposing that one is missing | ||
int fullLen = a.length + 1; | ||
|
||
// calculating the sum of the array's elements | ||
for (int i = 1; i <= fullLen; i++) { | ||
fullSum += i; | ||
} | ||
|
||
// sum of the actual elements in the array | ||
int partSum = 0; | ||
// actual length of the array | ||
int partLen = a.length; | ||
|
||
// calculating the sum of actual array's elements | ||
for (int elem : a) { | ||
partSum += elem; | ||
} | ||
|
||
// the missing element will be equal to the difference between two calculated sums | ||
return fullSum - partSum; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package test.java; | ||
|
||
import main.java.ua.com.florin.findmissing.FindMissing; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotEquals; | ||
|
||
public class FindMissingTest { | ||
|
||
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 10}; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
array = null; | ||
} | ||
|
||
@Test | ||
public void testFindMissing() throws Exception { | ||
assertEquals(9, FindMissing.findMissing(array)); | ||
assertNotEquals(10, FindMissing.findMissing(array)); | ||
} | ||
} |