Question

Write a Java program using the OOP paradigm to do the following: 1. Main Function: create...

Write a Java program using the OOP paradigm to do the following:


1. Main Function: create a list with a million items, and you can use the generate function to add a million random values to the list.

2. Create a method to sort the list created in the main function using bubble sort, then use the sorted list to search for the (k) value using binary search.

3. Create a method to create a new list of 1000 items and search for a value (V) using linear search.

4. Create a method to create a new list of 77 items and then sort it using the selection sort.

Homework Answers

Answer #1
import java.util.Random;

public class Sorting {

    public static int sortBinarySearch(int array[], int k){

        // Sort array using bubble sort

        int n = array.length;
        for (int i = 0; i < n-1; i++){
            for (int j = 0; j < n-i-1; j++){
                if (array[j] > array[j+1]){
                // swap temp and arr[i]
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }


        // Search for the element using binary search.
        // Iterative binary search is used.

        int l = 0;
        int r = n-1;

        while(l <= r){
            int mid = (l + r)/2;
            if(array[mid] == k){
                return mid;
            }
            else if(array[mid] > k){
                r = mid - 1;
            }
            else{
                l = mid + 1;
            }
        }

        return -1;

    }

    public static int linearSearch(int v){

        // Create a random array of 1000 elements
        Random rand = new Random();

        int n = 1000;
        int array[] = new int[n];

        for(int i=0;i<n;i++){
            array[i] = rand.nextInt();
        }

        // Linear search for v
        for(int i=0;i<n;i++){
            if(array[i] == v){
                return i;
            }
        }

        return -1;
    }

    public static void selectionSort(){

        // Create a random array of 77 elements.
        Random rand = new Random();

        int n = 77;
        int array[] = new int[n];

        for(int i=0;i<n;i++){
            array[i] = rand.nextInt();
        }

        // Selection sort -  iteratively find the next minimum element and place it in the front of array,
        for (int i = 0; i < n-1; i++){
        
            // Find the minimum element in unsorted array
            int min_idx = i;
            for (int j = i+1; j < n; j++)
                if (array[j] < array[min_idx])
                    min_idx = j;
 
            // Swap the found minimum element with the first
            // element
            int temp = array[min_idx];
            array[min_idx] = array[i];
            array[i] = temp;
        }

        

    } 

    public static void main(String[] args){

        // Make a random array of 1000 elements.
        Random rand = new Random();

        int n = 1000000;
        int array[] = new int[n];

        // Random element is filled.
        // Use rand.nextInt(bound) to restrict the random numbers to a bound.
        for(int i=0;i<n;i++){
            array[i] = rand.nextInt();
        }

       // Calling methods.
       int index = sortBinarySearch(array, 100);
       
       System.out.println(index);

       int index2 = linearSearch(500);

       selectionSort();


    }
    
}

I cannot show the output as the first method where we do bubble sort does not complete in a reasonable amount of time. It can take a couple of hours to sort an array of million elements using bubble sort.

I have added comments for your understanding

I would love to resolve any queries in the comments. Please consider dropping an upvote to help out a struggling college kid :)

Happy Coding !!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In JAVA Find the code for sorts in your language some where online. You will copy...
In JAVA Find the code for sorts in your language some where online. You will copy code from the internet (with citation!) and modify it to test. Make a random array of integers, then perform each search on them. LINEAR SEARCH Write a linear search method to find a number in the list. Call this before each of your sort methods. Include a comment explaining how Linear Search works, when to use it, when you cannot. BINARY SEARCH Write a...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following...
USING JAVA LANGUAGE : Using Doubly Linked List, create a java code that does the following Without using LinkedList from the JAVA LIBRARY. and please include methods for each function. Create a menu that contains the following operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL....
Write a Java program that randomly generates an array of 500,000 integers between 0 and 499,999,...
Write a Java program that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime = System.currentTimeMillis(); long...
JAVA: Design and implement a recursive version of a binary search.  Instead of using a loop to...
JAVA: Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are passed to...
Write a Java part code to do the following   : ** Suppose a file called infile stored...
Write a Java part code to do the following   : ** Suppose a file called infile stored in drive D: ,filled with five integers(1000,200,3030,40 and 500) Read  the integers from infile then store them in array called ar[] and print it elements. Method called search( ) to print the location of   value 500. Method called sort() to sort array elements. Build another file called outfile in drive D: to save the sorted array elements . Note : Use ArrayIndexOutOfBoundsException when you use array...
Java Create an array list of your own choosing; the data type is up to you....
Java Create an array list of your own choosing; the data type is up to you. Add at least four values to it initially. Then, insert at least two values. Display the value of the first and last items using the get method. Replace at least one value with a new value. Display the final count of items in the array list.
Write a program in Java that: 1. will prompt user with a menu that contains options...
Write a program in Java that: 1. will prompt user with a menu that contains options to: a. Add a To-Do Item to a todo list. A To-Do Item contains: i. An arbitrary reference number (4; 44,004; etc.) ii. A text description of the item ("Pick up dry cleaning", etc.) iii. A priority level (1, 2, 3, 4, or 5) b. View an item in the list (based on its reference number) c. Delete an item from the list (based...
Write a program that sorts the content of a map by Values. Before we start, we...
Write a program that sorts the content of a map by Values. Before we start, we need to check the explanation of Java Map Interface and Java LinkedHashMap. This program can be implemented as follows: 1. Create a LinkedHashMap class <String (Country), String(Capital)> that stores Capitals of FIVE countries. 2. Use sortMap() for receiving content of the created class of LinkedHashMap and reorder it in a sorted Map. (sort in natural order). 3. You need to transfer the map into...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...