In: Computer Science
Create a database to hold a collection of numbers to be searched and sorted:
1) Create a main class and a database class
2) Collect 5 random numbers from the user in the main class and store in an array. (not in ascending or descending order)
3) Create an instance of the database class in your main class and store the numbers array in the database using its constructor.
4) In the Database class, create a method that performs a bubble sort and returns a sorted array of the data (ascending and descending). Call this method from the main class and print the result to the screen.
5) In the Database class, create methods that each return the min, average, and max respectively. Call each of them from the main class and print the result to the screen.
6) In the Database class, create a method that searches for a value in the array using a binary search and returns the index of that value. Call this method from the main class and print the result to the screen.
BubbleSort and Binary Search Code:
{
public static void main(String args[])
{
SortExample ob = new SortExample();
int[] arr = {64, 34, 25, 12, 22, 11, 90};
ob.bubbleSort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
int key = 22;
int index = ob.BinarySearch(arr, key);
System.out.println("\n" + key + " is at index: " + index);
}
void bubbleSort(int[] arr)
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
/* Prints the array */
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
int BinarySearch(int[] arr, int key)
{
int mid = 0;
int low = 0;
int high = arr.length - 1;
while (high >= low)
{
mid = (high + low) / 2;
if (arr[mid] < key) {
low = mid + 1;
}
else if (arr[mid] > key) {
high = mid - 1;
}
else {
return mid;
}
}
return -1;
}
}
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
import java.util.*;
class Database
{
int[] arr;
Database(int[] a)
{
//this.arr=new int[arr.length];
this.arr = a;
}
void bubbleSort()
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
for (int j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int min()
{
int min=arr[0];
for (int i = 1; i < arr.length; i++)
{
if(arr[i]<min)
min=arr[i];
}
return min;
}
int max()
{
int max=arr[0];
for (int i = 1; i < arr.length; i++)
{
if(arr[i]>max)
max=arr[i];
}
return max;
}
double average()
{
double sum=0;
for (int i = 0; i < arr.length; i++)
{
sum+=arr[i];
}
return sum/arr.length;
}
int binarySearch(int key)
{
int mid = 0;
int low = 0;
int high = arr.length - 1;
while (high >= low)
{
mid = (high + low) / 2;
if (arr[mid] < key)
{
low = mid + 1;
}
else if (arr[mid] > key)
{
high = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
void printArray()
{
for (int i=0; i<arr.length; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
}
class Main
{
public static void main (String[] args)
{
System.out.print("Enter 5 Numbers:
");
int[] a=new int[5];
Scanner sc = new
Scanner(System.in);
//read 5 numbers
for(int i=0;i<5;i++)
a[i]=sc.nextInt();
//Pass data in the
constructor
Database db = new
Database(a);
System.out.print("The Array is:
");
db.printArray();
System.out.println("Minimum:
"+db.min());
System.out.println("Maximum:
"+db.max());
System.out.println("Average:
"+db.average());
System.out.println("Search for 10,
INDEX = : "+db.binarySearch(10));
System.out.println("Search for 20,
INDEX = : "+db.binarySearch(20));
System.out.print("Sorted Array:
");
db.bubbleSort();
db.printArray();
}
}
===============================
SCREENSHOT: