public class IntervalDemo {
public static long intervalSums(int[] A, int[][] B) {
long count = 0;
//Overall two loops are
required , time complexity will be O(n^2)
for (int i = 0; i < A.length;
i++) {
for (int j = 0;
j < A.length; j++) {
if (i > j)
B[i][j] = -1; // fill the
lower "triangle" of B with -1s
else {
//i==j assign i+1
if (i == j) {
B[i][j] =
i + 1;
} else {
//else
case previous element and j and 1
B[i][j] =
B[i][j - 1] + j + 1;
}
}
count++;
}
}
return count;
}
public static void main(String[] args) {
int A1[] = { 1, 2, 3, 4, 5, 6,
7, 8, 9 };
int B1[][] = new int[9][9];
long resultCount =
intervalSums(A1, B1);
System.out.println("Count:
"+resultCount);
for (int i = 0; i < 9; i++)
{
for (int j = 0;
j < 9; j++) {
System.out.print(B1[i][j] + " ");
}
System.out.println();
}
}
}
===============================================
---------------------
Observations:
---------------------
1. Initial code has 3 for loops , This has only 2 for
loops
2. Count value decreased from 165 to 81
3. This is drastic change in increse in
performace.
===============================================
Java code efficiency: Look at the implementation of the method called intervalSums below. Design and implement...
java
The following code is an implementation of a HeapPriorityQueue. You are to implement the methods commented with: // TODO: TIP: Do not just go from memory of your assignment implementation, be sure to consider carefully the constructor and method implementation provided to you. NOTE: You do not have to provide an implementation for any methods intentionally omitted public class Heap PriorityQueue implements PriorityQueue private final static int DEFAULT SIZE 10000 private Comparable [ ] storage private int currentSize: public...
Consider the following attempt at a selection sort algorithm implementation in Java: 1 public static void selectionSort(int[] a) { 2 int n = a.length; 3 for (int i = 0; i < n - 1; i++) { 4 int smallest = i; 5 for (int j = 1; j < n; j++) { 6 if (j < smallest) { 7 smallest = j; 8 } 9 } 10 if (i != smallest) { 11 int temp = a[smallest]; 12 a[smallest]...
need help editing or rewriting java code, I have this program
running that creates random numbers and finds min, max, median ect.
from a group of numbers,array. I need to use a data class and a
constructor to run the code instead of how I have it written right
now. this is an example of what i'm being asked
for.
This is my code:
import java.util.Random;
import java.util.Scanner;
public class RandomArray {
// method to find the minimum number in...
JAVA: Excerpt B.A: Complete the implementation of the following method. The purpose of this method is to return true if and only if all the elements in the array are x. In other words, all elements in the array are the value x (replace -a- with right answer). public static -a- allSame(int[] nums, int x){ for (int i = 0; i < -a-; i++){ if (nums[i] -a- x){ return -a-; } } return -a- } Excerpt B.B: What is printed...
java : here is a code that I have to implement. counting the occuence in an array /** * _Part 3: Implement this method._ * * Counts the items in the ordered array list that are equal to the item at * the specified index. Be sure to take advantage of the fact that the list * is sorted here. You should not have to run through the entire list to * make this count. * * @param index an...
I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need to create a static method that takes an argument "n" and returns the n'th line of pascal's triangle. the main method, which will call the class, is already done and the class is set up. Please build this without modifying the main method and without modifying exactly what the static method returns. public class Main { public static void main(String[] args) { int n...
Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...
1(5 pts): For each code fragment below, give the complexity of the algorithm (O or Θ). Give the tightest possible upper bound as the input size variable increases. The input size variable in these questions is exclusively n. Complexity Code public static int recursiveFunction (int n)f f( n <= 0 ) return 0; return recursiveFunction (n - 1) 1; for(int i 0i <n; i+) j=0; for ( int j k=0; i; k < < j++) for (int j; m <...
the code needs to be modifed. require a output for the
code
Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...
Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of bitmap (in bits) to be used */ int k, /* chunk length to be matched */ const char *qs, /* query docoument (X)*/ int m, /* query document length */ const char *ts, /* to-be-matched document (Y) */ int n /* to-be-matched document length*/) { /*if the to-be-matched document is less than k length, return false*/ if (n < k) return 0; /*start our...