Implemented TestInterpolationSearch#RandomDistinct
This commit is contained in:
parent
aec729b236
commit
d31dbbb4d8
37
src/Assignments/A3/TestInterpolationSearch.java
Normal file
37
src/Assignments/A3/TestInterpolationSearch.java
Normal file
@ -0,0 +1,37 @@
|
||||
package Assignments.A3;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class TestInterpolationSearch {
|
||||
|
||||
/** Holds the list of 1024 unique values used to search through */
|
||||
public int[] values;
|
||||
|
||||
|
||||
/**
|
||||
* Generates a list of 1024 random, non-repeating, integers and assigns them to the values field.
|
||||
*/
|
||||
public void RandomDistinct() {
|
||||
/* Used to track what ints have been used */
|
||||
HashSet<Integer> isUsed = new HashSet<>();
|
||||
int[] newList = new int[1024];
|
||||
Random random = new Random();
|
||||
|
||||
/* Loops 1024 times and assigns a new unique value each time. */
|
||||
for (int i = 0; i < 1024; i++) {
|
||||
int newValue = -1;
|
||||
|
||||
/* Ensures that there is no duplicate values */
|
||||
do {
|
||||
newValue = random.nextInt(9999) + 1;
|
||||
} while (isUsed.contains(newValue));
|
||||
|
||||
/* Adds the value to the known list and also to the generated list. */
|
||||
newList[i] = (newValue);
|
||||
isUsed.add(newValue);
|
||||
}
|
||||
/* Sorts and assigns the values generated to the field. */
|
||||
Arrays.sort(newList);
|
||||
this.values = newList;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user