-
Notifications
You must be signed in to change notification settings - Fork 4
/
CS263_Lab_1.java
36 lines (32 loc) · 1.19 KB
/
CS263_Lab_1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Question: Given an unsorted array arr[] of length N and an integer K,
// count the occurrences (or frequency) of K in the given array using the Divide and Conquer method.
// Note that, minimum length of the array is 100
import java.util.*;
public class CS263_Lab_1 {
public static int FREQUENCY(int[] array, int low, int high, int key) {
int size = array.length;
if (size == 0) return 0;
if (low >= high)
{
if (key == array[low]) return 1; else return 0;
}
int mid = low + (high - low) / 2;
int left = FREQUENCY(array, low, mid, key);
int right = FREQUENCY(array, mid + 1, high, key);
return left + right;
}
public static void main(String[] args) {
Random rand = new Random();
int[] A = new int[100];
for (int i = 0; i < 100; i++) {
A[i] = rand.nextInt(1, 20);
}
int a = rand.nextInt(1, 20);
for (int i = 0; i < 100; i++) {
System.out.print(A[i] + " ");
}
System.out.println();
int count = FREQUENCY(A, 0, 99, a);
System.out.println("The number " + a + " has frequency " + count + " in the array");
}
}