Question

Do a theta analysis and count the number of computations it performed in each function/method of...

Do a theta analysis and count the number of computations it performed in each function/method of the following code:

import java.io.*;
import java.util.Scanner;

class sort
{
int a[];
int n;
long endTime ;
long totalTime;
long startTime;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public sort(int nn) // Constructor
{
a = new int[nn];
n = nn;
endTime= 0;
totalTime =0;
startTime =0;
}
public static void main(String args[]) throws IOException
{
System.out.print("\nEnter number of students: ");
int nn = Integer.parseInt(br.readLine());
sort call = new sort(nn);

// Read array from user
System.out.println("\nEnter " +nn +" student's ID numbers:");
call.readArray();

// Ask for choosing the sorting technique
System.out.println("Choose Sorting Technique :\n");
System.out.println("1 : Bubble Sort");
System.out.println("2 : Selection Sort");
System.out.println("3 : Insertion Sort");
System.out.println("4 : Quick Sort");
System.out.println("5 : Merge Sort");
System.out.print("\nYour Choice : ");
int choice = Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
call.bubbleSort();
break;
case 2:
call.selectionSort();
break;
case 3:
call.insertionSort();
break;
case 4:
call.quickSort();
break;
case 5:
call.mergeSort();
break;
default :
System.out.println("\nInvalid Choice !");
System.exit(1);
break;
}
call.display(); // Print the sorted array
call.firstLastName();
}
public void readArray() throws IOException
{
for(int i=0;i<n;i++)
a[i] = Integer.parseInt(br.readLine());
}
public void bubbleSort()
{
int t;
startTime = System.currentTimeMillis();
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
endTime = System.currentTimeMillis();
totalTime = endTime - startTime;
System.out.println(totalTime);

}
public void firstLastName() {
int n;
String temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of names you want to enter:");
n = s.nextInt();
String names[] = new String[n];
Scanner s1 = new Scanner(System.in);
System.out.println("Enter all the names names:");
for(int i = 0; i < n; i++)
{
names[i] = s1.nextLine();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (names[i].compareTo(names[j])>0)
{
temp = names[i];
names[i] = names[j];
names[j] = temp;
}
}
}
System.out.print("Names in Sorted Order: ");
for (int i = 0; i < n - 1; i++)
{
System.out.print(names[i] + ", ");
}
System.out.print(names[n - 1]);
System.out.println();

}
public void selectionSort()
{
startTime = System.currentTimeMillis();
int t, min;
for(int i=0;i<n-1;i++)
{
min = i;
for(int j=i+1;j<n;j++)
{
if(a[min]>a[j])
min = j;
}
if(min!=i)
{
t = a[min];
a[min] = a[i];
a[i] = t;
}
}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println(totalTime);
}
public void insertionSort()
{
startTime = System.currentTimeMillis();
int t,j;
for(int i=1;i<n;i++)
{
j = i-1;
t = a[i];
while(t<a[j] && j>=0)
{
a[j+1] = a[j];
j--;
}
a[j+1] = t;
}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println(totalTime);
}
public void quickSort()
{
startTime = System.currentTimeMillis();
int t;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
endTime = System.currentTimeMillis();
totalTime = endTime - startTime;
System.out.println(totalTime);
}
static void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;

/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];

/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];


/* Merge the temp arrays */

// Initial indexes of first and second subarrays
int i = 0, j = 0;

// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}

/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}

public void mergeSort(int arr[], int l, int r) {
startTime = System.currentTimeMillis();
if (l < r)
{
// Find the middle point
int m = (l+r)/2;

// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr , m+1, r);

// Merge the sorted halves
merge(arr, l, m, r);
}
}
public void display()
{
System.out.println("\nSorted Array :");
for(int i=0;i<n;i++)
System.out.println(a[i]);
}


}

Homework Answers

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
I need to get the Min and Max value of an array when a user inputs...
I need to get the Min and Max value of an array when a user inputs values into the array. problem is, my Max value is right but my min value is ALWAYS 0. how do i fix this? any help please!!! _________________________________________________________________________________________________ import java.util.Scanner; public class ArrayMenu{ static int count; static Scanner kb = new Scanner(System.in);             public static void main(){ int item=0; int[] numArray=new int[100]; count=0;       while (item !=8){ menu(); item = kb.nextInt();...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems that need to be corrected. Your task is to complete it to course style and documentation standards CS 200 Style Guide. This project will be human graded. This class contains a set of methods. The main method contains some examples of using the methods. Figure out what each method does and style and document it appropriately. The display method is done for you and...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static void readFromFile() throws IOException { BufferedReader bufReader = new BufferedReader(new FileReader("processes.txt")); ArrayList<String> listOfLines = new ArrayList<>(); String line = bufReader.readLine(); while (line != null) { listOfLines.add(line); line = bufReader.readLine(); } bufReader.close(); System.out.println("Content of ArrayLiList:"); // split by new line int num = 0; for (String line1 : listOfLines) { String line2[] = line1.split(","); // int burstTime = Integer.parseInt(line2[3].trim()); // String retrival = listOfLines.get(0); System.out.println("...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Modify the CountByFives application so that the user enters the value to count by. Start each...
Modify the CountByFives application so that the user enters the value to count by. Start each new line after 10 values have been displayed. (Cengage Mindtap assignment). This is the java code we are given: public class CountByAnything { // Modify the code below public static void main (String args[]) { final int START = 5; final int STOP = 500; final int NUMBER_PER_LINE = 50; for(int i = START; i <= STOP; i += START) { System.out.print(i + "...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to...
ex3 Write a method public static boolean isPalindrome(String input) that uses one or more stacks to determine if a given string is a palindrome. [A palindrome is a string that reads the same forwards and backwards, for example ‘racecar’, ‘civic’]. Make sure that your method works correctly for special cases, if any. What is the big-O complexity of your method in terms of the list size n. Supplementary Exercise for Programming (Coding) [Stacks] Download and unpack (unzip) the file Stacks.rar....
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
<<<<<<<< I need only the UML diagram for ALL classes.Java???????????? public class House {    private...
<<<<<<<< I need only the UML diagram for ALL classes.Java???????????? public class House {    private int houseNumber;    private int bedrooms;    private int sqFeet;    private int year;    private int cost;    public House(int houseNumber,int bedrooms,int sqFeet, int year, int cost)    {        this.houseNumber = houseNumber;        this.bedrooms = bedrooms;        this.sqFeet = sqFeet;        this.year = year;        this.cost = cost;    }    public int getHouseNumber()    {        return houseNumber;    }   ...