Question

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 binary search method to find a number in the list. Call this after each of your sort methods. Include a comment explaining how Binary Search works, when to use it, when you cannot.

BUBBLE SORT

Insert 10,000 items. Display the data before and after the sort. You’ll see that scrolling the display takes a long time. Take Screenshot Number 1 of the your changed code.

Next comment out the calls to display(). Add timing if necessary to see how long the sort itself takes. The time will vary on different machines. Take Screenshot Number 2 of your results.

Sorting 100,000 numbers will probably take less than 30 seconds. Now, select an array size that takes about this long and time it. Take Screenshot Number 3 of these results.

SELECTION SORT

Now, perform the same experiment on the Selection Sort using the same timing method you used. This will be  Screenshot Number 4, Screenshot Number 5, Screenshot Number 6.

INSERTION SORT

Now, perform the same experiment on the Selection Sort using the same timing method you used. This will be  Screenshot Number 7, Screenshot Number 8, Screenshot Number 9.

Homework Answers

Answer #1

1) Linear Search:

In linear search, we iterate through each element and compare it with the element we want to find.
Time Complexity: O( n )
Space Complexity: O( 1 )
Linear Search is used when there are only a few elements, and we want to perform single search in unsorted list.
Linear Search is not used when the data is huge, it would take a long time to search.

import java.util.*;
import java.lang.*;
import java.io.*;

class Sample
{
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println("linear search");
        int[] a = {3,5,1,4,9,2,8,6,7};
        int number_to_be_found = 9;
        //Implementing linear search
        for(int i=0;i<a.length;i++)
            if(a[i]==number_to_be_found){
                System.out.println("Found at "+i+" index");
            }
            else{
                System.out.println("Not Found");
            }
    }
}

2) Binary Search:

Binary Search is performed on a sorted set of elements, it compares the number to be found with the middle element, if it is not the same, then it checks which (left or right) subarray will contain the element, if mid is less than element, then it would be found in right subarray, similarly for left subarray.
Time Complexity: O( log n )
Space Complexity: O( 1 )
Binary Search considerably reduces the search time if the data is sorted.
Binary search is not used when the array changes often, or if the elements are unsorted.

class BinarySearch { 
    int binarySearch(int a[], int l, int r, int number_to_be_found) 
    { 
        if (r >= l) { 
            int mid=l+(r-l)/2; 
            // checking middle element
            if (a[mid]==number_to_be_found) 
                return mid; 
            // checking left subarray 
            if (a[mid]>number_to_be_found) 
                return binarySearch(a, l, mid - 1, number_to_be_found); 
            // checking right subarray 
            return binarySearch(a, mid + 1, r, number_to_be_found); 
        } 
        // if not found
        return -1; 
    } 
    public static void main(String args[]) 
    { 
        BinarySearch ob = new BinarySearch(); 
        int a[] = {1,2,3,4,5,6,7,8,9}; 
        int number_to_be_found = 9; 
        int result = ob.binarySearch(a, 0, a.length - 1, number_to_be_found); 
        if (result == -1) 
            System.out.println("Not found"); 
        else
            System.out.println("Found at "+result+" index"); 
    } 
} 

For the following sorts , the number of elements taken is 10.

3) Bubble Sort:

class Sample {  
  static void bubbleSort(int[] a) {  
        int n = a.length;  
        int temp = 0;  
         for(int i=0;i<n;i++)
                 for(int j=1;j<(n-i);j++) 
                          if(a[j-1]>a[j]){  
                                 //swap elements  
                                 temp = a[j-1];  
                                 a[j-1] = a[j];  
                                 a[j] = temp;  
                         }         
    }  
    public static void main(String[] args) {  
                int a[] ={2,5,7,8,4,1,3,6,9};  
                 
                System.out.println("Before Bubble Sort");  
                for(int i=0; i < a.length; i++){  
                        System.out.print(a[i] + " ");  
                }  
                System.out.println();  
                // Calling bubble sort
                bubbleSort(a);  
                 
                System.out.println("After Bubble Sort");  
                for(int i=0; i < a.length; i++){  
                        System.out.print(a[i] + " ");  
                }  
   
        }  
} 

4) Selection Sort:

class Sample {  
    public static void selectionSort(int[] a){  
        for(int i=0;i<a.length-1;i++){  
            int min = i;  
            for (int j=i + 1;j<a.length;j++)
                if (a[j]<a[min]){  
                    min = j; 
                }  
            int temp=a[min];   
            a[min]=a[i];  
            a[i]=temp;  
        }  
    }  
    public static void main(String[] args){  
        int[] a = {2,4,8,6,1,5,7,9,3};  
        System.out.println("Before Selection Sort");  
        for(int i=0;i<a.length;i++)
            System.out.print(a[i]+" ");  
  
        System.out.println();  
          
        selectionSort(a);  
         
        System.out.println("After Selection Sort");  
        for(int i=0;i<a.length;i++) 
            System.out.print(a[i]+" ");  
    }  
} 

5) Insertion Sort:

class Sample {  
    public static void insertionSort(int a[]) {    
        for(int j=1;j<a.length;j++) {  
            int ele = a[j];  
            int i = j-1;  
            while((i >=0)&&(a[i]>ele)){  
                a[i+1] = a[i];  
                i--;  
            }  
            a[i+1] = ele;  
        }  
    }  
    public static void main(String[] args){    
        int[] a = {2,4,8,6,1,5,7,9,3};    
        System.out.println("Before Insertion Sort");    
        for(int i=0;i<a.length;i++){    
            System.out.print(a[i]+" ");    
        }    
        System.out.println();    
            
        insertionSort(a);   
           
        System.out.println("After Insertion Sort");    
        for(int i=0;i<a.length;i++){    
            System.out.print(a[i]+" ");    
        }    
    }    
} 

Please comment in case of doubts or queries.
Kindly upvote if you found it useful :)

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 python Generates 50,000 random numbers and puts them in an array. Sorts the numbers using...
in python Generates 50,000 random numbers and puts them in an array. Sorts the numbers using any sorting technique (Selection sort is fine, but you can try another one). This should take a few minutes to run. Ask the user for a number between 0 and 20,000,000 and search for it in your sorted array using a simple linear search. Is this a good idea? Ask the user for a number between 0 and 20,000,000 and search for it in...
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...
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()...
For this lab assignment you will need to write some code and create some graphs. You...
For this lab assignment you will need to write some code and create some graphs. You may use excel to create your graphs, or other language of your choice. Your data needs to demonstrate the experimental running time for Selection Sort (code in book), Merge Sort (code in book), and the Arrays.sort() method. Here is a basic outline of how you need to code this assignment. 1) Create several arrays of size 100, 1000, 10000, 100000, 1000000, … (you need...
Write a code in c++ using linear insertion following the steps below. Comment your work. 1....
Write a code in c++ using linear insertion following the steps below. Comment your work. 1.    Ask the user for the name of a file containing data. If it does not exist, the program should display an error, then ask for a new file name. Entering an asterisk (*) as the first and only character on a line should terminate the program. 2.     You can use a statically-allocated one-dimensional array of doubles for this with length 100. You...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this assignment must be submitted by the deadline on Blackboard. Submitting your assignment early is recommended, in case problems arise with the submission process. Late submissions will be accepted (but penalized 10pts for each day late) up to one week after the submission deadline. After that, assignments will not be accepted. Assignment The object of this assignment is to construct a mini-banking system that helps...
Programming Language is Java In class you learned how to work with collections and streams. This...
Programming Language is Java In class you learned how to work with collections and streams. This lab allows you to apply the skills you learned to a list of riders. With your first two assignment complete, the coordinator at the Car Safety Academy decided to begin collecting names for a monthly mailing list. You've been asked to collect the names and perform some processing. First you will create a Riders class. Then you will create an array of riders with...
Instructions Write a Java code Your goal is to take N integer inputs from the user...
Instructions Write a Java code Your goal is to take N integer inputs from the user -- N's value will be given by the user as well. You can assume the user provides a valid value for N, i.e., >0. Store the input integers in an array of size N in the order they are provided. These tasks should be done in the main() method. Create a new method called checkArray() that will take the previously created array as input...
This programming task will be a bit different. It will be more like what you would...
This programming task will be a bit different. It will be more like what you would receive if you were a maintenance engineer working in a corporate information systems or technology department. In other words, you will have some prebuilt source code provided for you that you must alter so it will meet the given programming specification.. Build a program from what you have been given as a starting-point. Rename the “starter code”. Do not add any modules; just, use...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT