Write a Java program to randomly generate and display a list
Write a Java program to randomly generate and display a list of integers; then use the built in Java sort routine to sort the list. Display the sorted list, then using the binary search algorithm, search the list for a user given element. If the element is found print its index, otherwise print -1
Solution
//code has been tested on eclipse
import java.util.*;
import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;
public class HelloWorld{
public static ArrayList<Integer> randomArrayList(int n) //method for generating random list of //integres,parameter n=count of random numbers
{
ArrayList<Integer> list = new ArrayList<Integer>(); //creating a arraylist
Random random = new Random(); //creating object of random class for //generating random numbers
for (int i = 0; i < n; i++) //generating random numbers till the count provided by //the user,i.e n
{
list.add(random.nextInt(255)); //adding random integers in the arraylist list
}
return list; //return list of random integers
}
//end of generating random integers method
public static void binarySearch(ArrayList<Integer> list,int search_key){ //binarySearch method for //finding search_key provided by the user
int size=list.size(); //size of the array list passed as a parameter in binarysearch method
int first=0; //variable storing first index
int last=size-1; //variable storing last index
int middle=(first+last)/2; //variable storing middle index
while(first<=last) //execute the loop till first index <= last index
{
if(list.get(middle)<search_key) //if the element at the middle index of list < //search_key,it means
//search_key should be in second half of the sorted //list.
first=middle+1; //therefore:first=middle+1
else if(list.get(middle)==search_key) //if the element at the middle index of list //= search_key
{
System.out.println(search_key + \" found at index \" + (middle) + \".\"); //print the index and break from //the loop
break;
}
else //else
last=middle-1; //search_key should be in first half of the sorted //list,hence=last=middle-1
middle=(first+last)/2;
}
if(first>last) //it means search_key is not available in the sorted //list
System.out.println(\"-1\"); //print -1
}
//end of binarySearch method
public static void main(String []args){ // main method
Scanner sc=new Scanner(System.in);
System.out.println(\"Enter the size of list of integrs\"); //asking the user for the count of random //integers
int n=sc.nextInt(); //storing it in the variable n
ArrayList<Integer> list = new ArrayList<>();
list=randomArrayList(n); //calling random number method and storing the //returned arraylist in list
for(int i=0;i<list.size();i++)
{
System.out.println(\"List of integers:\"+list.get(i)); //print unsorted list
}
Collections.sort(list); //calling inbuilt sort routine
for(int i=0;i<list.size();i++)
System.out.println(\"List of integers after sorting:\"+list.get(i)); //print sorted list
System.out.println(\"Enter the integer you want to search in the list\"); //asking the user for the element //which needs to be search
int search_key=sc.nextInt(); //storing the user entered element in the //variable search_key
binarySearch(list,search_key); //calling binarysearch method
}
}
********OUTPUT**********
Enter the size of list of integrs
5
List of integers:228
List of integers:4
List of integers:82
List of integers:218
List of integers:85
List of integers after sorting:4
List of integers after sorting:82
List of integers after sorting:85
List of integers after sorting:218
List of integers after sorting:228
Enter the integer you want to search in the list
85
85 found at index 2.
********OUTPUT**********
Please let me know in case of any doubt,thanks.

