Implemented TestInterpolationSearch#RandomDistinct

This commit is contained in:
Jonathan Turner 2024-03-21 12:12:35 -04:00
parent aec729b236
commit d31dbbb4d8

View 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;
}
}