From d31dbbb4d85775f483ec5d81be033824955bb850 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 21 Mar 2024 12:12:35 -0400 Subject: [PATCH] Implemented TestInterpolationSearch#RandomDistinct --- .../A3/TestInterpolationSearch.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/Assignments/A3/TestInterpolationSearch.java diff --git a/src/Assignments/A3/TestInterpolationSearch.java b/src/Assignments/A3/TestInterpolationSearch.java new file mode 100644 index 0000000..8def1f5 --- /dev/null +++ b/src/Assignments/A3/TestInterpolationSearch.java @@ -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 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; + } +}