Question

Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int j) that exchanges the values stored at ind

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below, I have attached the codes for the given problems.

(a)

public void exchange(int[] a, int i, int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}

The worst case time complexity will O(1).

(b)

Using binary search we can determine whether an present or not using (log n) comparisons. Binary search will make two equal halves repeatedly until the middle element every time is equal to the required element.

(c)

public int sum(int[] a, int lo, int hi){
int sum=0;
for(int i=lo; i<hi; i++){
sum = sum + a[i];
}
return sum;
}

(d)

Here is the other approach where time complexity is O(n). The main logic here is, we compute sum till present ith index and just copy that sum at every iteration.

public int createB(int[] a,int[] b, int lo, int hi){
int sum=0;
for(int i=lo; i<hi; i++){
sum = sum + a[i];
b[i] = sum;
}
return sum;
}

(e)

If we had an array b which was derived from array a, and we need to compute sum(a, lo, hi) in O(1) time is as follows:

sum = b[hi-1] - b[lo - 1]

Hoping that the solutions helps. Please comment for further assistance.

Add a comment
Know the answer?
Add Answer to:
Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Complete these 2 Java functions (the instructions are attached below): private static int minIndexBinarySearch(int array[], int...

    Complete these 2 Java functions (the instructions are attached below): private static int minIndexBinarySearch(int array[], int arrayLength, int key) { // Complete this function. } private static int maxIndexBinarySearch(int array[], int arrayLength, int key) { // Complete this function. } In certain applications, we are interested in counting the number of times key appears in a sorted array. The technique to solve such problems is to determine: - minIndex: the minimum index where key appears - maxIndex: the maximum index...

  • Java question Given an array of integer numbers, write a linear running time complexity program in...

    Java question Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...

  • In class we wrote a method closestPairFast that on an input array of numbers, finds the...

    In class we wrote a method closestPairFast that on an input array of numbers, finds the distance of the closest pair by first sorting the input array and then finding the closest adjacent pair. (See the file ClosestPair1D.java in the Code folder on D2L.) In this problem, you are asked to modify the method so that it returns an integer array consisting of the indices of the closest pair in the original array. If there is a tie, just return...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Please write where its written write your code here!! please use java for the code! please...

    Please write where its written write your code here!! please use java for the code! please use the given code and I will rate thanks Magic index in an array a[1..n] is defined to be an index such that a[ i ] = i. Given an array of integers, write a recursive method to find the first magic index from left to right. If one exists in the given array, return the index number i, otherwise return -1. Here are...

  • Need help to answer this method in java Write a method int indexFirstOne(int[ ] input) The...

    Need help to answer this method in java Write a method int indexFirstOne(int[ ] input) The input array is sorted, and every element is 0 or 1. Return the index of the first 1. If there are no 1s in the array, return -1. The worst-case runtime must be O(logn)where n is the number of elements (no credits for slower runtimes) Example: a = [0,0,1,1,1]      return 2 a = [ 0,0,0,1]          return 3 a = [0,0,0]              return -1 int indexFirstOne...

  • Given an array of integer numbers, write a linear running time complexity program in Java to...

    Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A if A[0] + A[1] + ... + A[i-1] = A[i+1] + A[i+2] + ... + A[n-1]; where 0 < i < n-1. Similarly, 0 is an stability index if (A[1] + A[2] + ... + A[n-1]) = 0 and...

  • java : here is a code that I have to implement. counting the occuence in an...

    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...

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support...

    Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support the Iterable interface such that the following code works: JstyArray<Integer> data; data = new JstyArray<Integer>(10); for (int i = 0; i < 10; ++i) { data.set(i, new Integer(i) ); } int sum = 0; for ( int v : data ) { if (v == null) continue; // empty cell sum += v; } The iterator provided by this class follows the behaviour of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT