1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
astraxan [27]
2 years ago
9

Write a Java class to perform the following: 1. Write a method to search the following array using a linear search, ( target ele

ments: 11, 55, 17.). (count the number of comparisons needed). {06, 02, 04, 07, 11, 09, 50, 62, 43, 32, 13, 75, 01, 46, 88, 17} 2. Write a method to sort the array using Selection Sort. (count the number of comparisons needed) 3, Write a method to sort the array using Bubble Sort. (count the number of comparisons needed) 4, Search he sorted array using a binary search (recursive) for the same set of target elements. (count the number of comparisons needed)

Computers and Technology
1 answer:
Alina [70]2 years ago
7 0

Answer:

Check the explanation

Explanation:

Linear search in JAVA:-

import java.util.Scanner;

class linearsearch

{

  public static void main(String args[])

  {

     int count, number, item, arr[];

     

     Scanner console = new Scanner(System.in);

     System.out.println("Enter numbers:");

     number = console.nextInt();

   

     arr = new int[number];

     System.out.println("Enter " + number + " ");

     

     for (count = 0; count < number; count++)

       arr[count] = console.nextInt();

     System.out.println("Enter search value:");

     item = console.nextInt();

     for (count = 0; count < number; count++)

     {

        if (arr[count] == item)

        {

          System.out.println(item+" present at "+(count+1));

         

          break;

        }

     }

     if (count == number)

       System.out.println(item + " doesn't found in array.");

  }

}

Kindly check the first attached image below for the code output.

Selection Sort in JAVA:-

public class selectionsort {

   public static void selectionsort(int[] array){

       for (int i = 0; i < array.length - 1; i++)

       {

           int ind = i;

           for (int j = i + 1; j < array.length; j++){

               if (array[j] < array[ind]){

                   ind = j;

               }

           }

           int smaller_number = array[ind];  

           array[ind] = array[i];

           array[i] = smaller_number;

       }

   }

     

   public static void main(String a[]){

       int[] arr = {9,94,4,2,43,18,32,12};

       System.out.println("Before Selection Sort");

       for(int i:arr){

           System.out.print(i+" ");

       }

       System.out.println();

         

       selectionsort(arr);

       

       System.out.println("After Selection Sort");

       for(int i:arr){

           System.out.print(i+" ");

       }

   }

}  

Kindly check the second attached image below for the code output.

Bubble Sort in JAVA:-

public class bubblesort {

   static void bubblesort(int[] array) {

       int num = array.length;

       int temp = 0;

        for(int i=0; i < num; i++){

                for(int j=1; j < (num-i); j++){

                         if(array[j-1] > array[j]){

                           

                                temp = array[j-1];

                                array[j-1] = array[j];

                                array[j] = temp;

                        }

                         

                }

        }

   }

   public static void main(String[] args) {

               int arr1[] ={3333,60,25,32,55,620,85};

               

               System.out.println("Before Bubble Sort");

               for(int i=0; i < arr1.length; i++){

                       System.out.print(arr1[i] + " ");

               }

               System.out.println();

                 

               bubblesort(arr1);

               

               System.out.println("After Bubble Sort");

               for(int i=0; i < arr1.length; i++){

                       System.out.print(arr1[i] + " ");

               }

 

       }

}  

Kindly check the third attached image below for the code output.

Binary search in JAVA:-

public class binarysearch {

  public int binarySearch(int[] array, int x) {

     return binarySearch(array, x, 0, array.length - 1);

  }

  private int binarySearch(int[ ] arr, int x,

        int lw, int hg) {

     if (lw > hg) return -1;

     int middle = (lw + hg)/2;

     if (arr[middle] == x) return middle;

     else if (arr[middle] < x)

        return binarySearch(arr, x, middle+1, hg);

     else

        return binarySearch(arr, x, lw, middle-1);

  }

  public static void main(String[] args) {

     binarysearch obj = new binarysearch();

     int[] ar =

       { 22, 18,12,14,36,59,74,98,41,23,

        34,50,45,49,31,53,74,56,57,80,

        61,68,37,12,58,79,904,56,99};

     for (int i = 0; i < ar.length; i++)

        System.out.print(obj.binarySearch(ar,

           ar[i]) + " ");

     System.out.println();

     System.out.print(obj.binarySearch(ar,19) +" ");

     System.out.print(obj.binarySearch(ar,25)+" ");

     System.out.print(obj.binarySearch(ar,82)+" ");

     System.out.print(obj.binarySearch(ar,19)+" ");

     System.out.println();

  }

}

Kindly check the fourth attached image below for the code output

You might be interested in
The Save command saves your changes silently without additional prompts, using the same save settings; the Save As command reope
kirza4 [7]

The "Save command" saves your changes <u>silently</u> without additional prompts and it uses the same save settings while the "Save As command" <u>reopens</u> the Save screen: True.

What is the Save command?

A Save command can be defined as a type of command associated with the file menu of a software application and it causes a copy of the current file to be created and stored to a specific location on a computer system.

<h3>What is the Save as command?</h3>

A Save command can be defined as a type of command associated with the file menu of a software application and it causes a copy of the current file to be created and stored to a different location, file name, and/or file type.

In conclusion, the "Save command" saves your changes <u>silently</u> without additional prompts and it uses the same save settings while the "Save As command" <u>reopens</u> the Save screen, so as to enable you make different choices.

Read more on Save command here: brainly.com/question/16852455

5 0
2 years ago
Instructions:Select the correct answer.
Dafna1 [17]
It would be a audio file -B.
8 0
3 years ago
How do I write a pseudocode algorithm to read a sequence of number terminated by the number 999 and print the sum of the positiv
Veronika [31]

Answer:Start with the algorithm you are using, and phrase it using words that are easily transcribed into computer instructions.

Indent when you are enclosing instructions within a loop or a conditional clause Avoid words associated with a certain kind of computer language.

Explanation:

5 0
2 years ago
Which is a major drawback of muliuser procesing of a database
-BARSIC- [3]

The main drawback of multiuser processing of a database is:

  • Lost-update problem.

<h3>What is  lost-update problem?</h3>

In the area of lost update problem, an update that has been carried on to a data item by a transaction is said to be lost as it is known to be overwritten by the update that has been carried on by another transaction.

Therefore, The main drawback of multiuser processing of a database is:

  • Lost-update problem.

Learn more about database from

brainly.com/question/26096799

#SPJ12

3 0
1 year ago
Write a program that first gets a list of integers from input. Then, get another value from the input, and output all integers l
jonny [76]

Answer:

The c++ program for the given scenario is given below.

#include <iostream>

using namespace std;

int main() {

   int len=20, arr[len], data;    

   // initialize all elements of the array to 0

   for(int i=0; i<len; i++)

   {

       arr[i] = 0;

   }    

   cout<<"This program outputs all the numbers less than or equal to the given number."<<endl;

   cout<<"Enter the list of numbers. Enter 0 to stop entering the numbers. "<<endl;

   for(int i=0; i<len; i++)

   {

       cin>>arr[i];        

       // 0 indicates user wishes to stop entering values  

       if(arr[i] == 0)

           break;

       else

           continue;

   }    

   // number from which the list is to be compared

   cout<<"Enter the number to be compared."<<endl;

   cin>>data;

     

   cout<<"The values less than or equal to the number "<<data<<" are "<<endl;

   for(int i=0; i<len; i++)

   {  

       // 0 indicates the end of the list entered by the user

       if(arr[i]==0)

           break;

       if(arr[i] <= data)

           cout<<arr[i]<<endl;

   }    

   return 0;    

}  

OUTPUT

This program outputs all the numbers less than or equal to the given number.

Enter the list of numbers. Enter 0 to stop entering the numbers.  

23

45

67

89

10

0

Enter the number to be compared.

59

The values less than or equal to the number 59 are  

23

45

10

Explanation:

This program takes input only from the user and makes no assumptions.

The list of numbers entered by the user are stored in an array. If the user input is less than the size of the array, the remaining elements are set to 0.

While taking input from the user, if any element of the array is found to be 0, the loop is discontinued.

for(int i=0; i<len; i++)

   {

       cin>>arr[i];        

       if(arr[i] == 0)

           break;

       else

           continue;

   }  

Same test is applied when all the numbers less than the given number are displayed.

for(int i=0; i<len; i++)

   {  

        if(arr[i]==0)

           break;

       if(arr[i] <= data)

           cout<<arr[i]<<endl;

   }

The above program takes into account all the specifications in the given question.

5 0
3 years ago
Other questions:
  • 1. Which of the following are considered CAD powerhouses?
    6·1 answer
  • As with country citizenship, with digital citizenship comes _____.
    14·2 answers
  • Many organizations find themselves in the position of being data rich and information poor. Even in today's electronic world, ma
    13·1 answer
  • What is the output of 1101 x 10 == 11000 + 10?
    12·1 answer
  • All nuclear energy results in the rapid release of energy, such as in atomic bombs. true or false
    8·1 answer
  • Katarina is deciding whether to buy a desktop or a laptop computer. What will most likely help Katarina make her decision?
    10·2 answers
  • When you sign in to your Microsoft account with another Windows device, your settings will appear very differently than they do
    7·1 answer
  • A data analyst adds descriptive headers to columns of data in a spreadsheet. How does this improve the spreadsheet?.
    8·1 answer
  • A(n) ________ address is the address that external entities use when communicating with your network.
    11·1 answer
  • Which principle or element of layout design is highlighted in this event poster?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!