Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e737dc5

Browse files
committed
FEAT Implemented brute force search
1 parent ec24ad9 commit e737dc5

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

‎src/PracticeAlgorithms.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,19 @@ private static int rank(int key, int[] array, int minIndex, int maxIndex) {
164164
return rank(key, array, minIndex, maxIndex - 1);
165165
}
166166

167+
/**
168+
* static method to brute force search for a "key" in an int array
169+
* */
170+
public static int bruteForceSearch(int key, int[] theArray) {
171+
for (int i = 0; i < theArray.length; i++) {
172+
if (theArray[i] == key) {
173+
return i;
174+
}
175+
}
176+
177+
return -1;
178+
}
179+
167180

168181
public static void main(String[] args) {
169182
Random generator = new Random();
@@ -247,5 +260,8 @@ public static void main(String[] args) {
247260
System.out.println("index: " + binarySearch(5, testIntArray));
248261
System.out.println();
249262

263+
System.out.println("- Brute Force Search: key = 3");
264+
System.out.println("index: " + bruteForceSearch(3, testIntArray));
265+
System.out.println();
250266
}
251267
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /