Question

Write a java function to process an array as follows: 1) reverse the contents of an...

Write a java function to process an array as follows:

1) reverse the contents of an array

2) merge together two arrays

3) merge together two sorted arrays to yield a sorted array

4) append one array onto the end of another

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


public class Main
{
//method : 1)reverse the contents of an array
   static void reverse(int a[])
   {
   int n =a.length;
   int l=n-1;
   //reversing array
   for(int i=0;i<(n/2);i++)
   {
   int k =a[i];
   a[i]=a[l-i];
   a[l-i]=k;
   }
   }
   //method to 2)merge together two arrays and into new array and returns it
   static int[] merge(int a[],int b[])
   {
   int n = a.length+b.length;
   int c[] = new int[n];
   int i=0;
   for(int j=0;j<a.length;j++)
   c[i++]=a[j];
   for(int j=0;j<b.length;j++)
   c[i++]=b[j];
   return c;
   }
   //method to 3) merge together two sorted arrays to yield a sorted array and returns the sorted arrray
   static int[] mergeSortedArrays(int a[], int b[])
{
int i = 0, j = 0, k = 0;
int n1 = a.length;
int n2=b.length;
int c[] = new int[n1+n2];
// Traverse both array
while (i<n1 && j <n2)
{
if (a[i] <b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}
  
while (i < n1)
c[k++] = a[i++];
  
// Store remaining elements of second array
while (j < n2)
c[k++] = b[j++];
return c;
}
//method to 4) append one array onto the end of another
   static void append(int a[],int b[])
   {
   int n = a.length+b.length;
   int c[] = new int[n];
   int i=0;
   for(int j=0;j<a.length;j++)
   c[i++]=a[j];
   for(int j=0;j<b.length;j++)
   c[i++]=b[j];
   a=c;
   }
   public static void main(String[] args) {
      
   }
}

Add a comment
Know the answer?
Add Answer to:
Write a java function to process an array as follows: 1) reverse the contents of an...
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
  • C++. Write a program that copies the contents of one array into another array but in...

    C++. Write a program that copies the contents of one array into another array but in reverse order using pointers.           - in main()                    - define 2 arrays of type int, 10 elements each                              - initialize one array with numbers 1 through 10                              - initialize all elements of the second array to 0                    - call function reverseCopy with both arrays as arguments                    - display the values of array number 2 (reversed order)           - reverseCopy...

  • Write a mips program that defines two integer array that are pre-sorted and the same size...

    Write a mips program that defines two integer array that are pre-sorted and the same size (e.g., [3, 7, 9, 11, 15, 21] and [1, 4, 6, 14, 18, 19]) and a function merge that takes the two arrays (and their size) as inputs and populates a single array of twice the size of either input array that contains the elements of both arrays in ascending order. In the example arrays given, then output would be [1, 3, 4, 6,...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • Write a program to merge two sorted arrays into a third array. Ask the user to...

    Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note:...

  • 10 L 1 22 Given the function below, that divides the original array in two arrays...

    10 L 1 22 Given the function below, that divides the original array in two arrays of half size, develop the Java code that combines two sorted arrays or clearly explain how to implement the merge function. (3 points) nt middle values.Length/2; // divide array into two arrays of half size nt left- new int[middle]; or Cint i-e; imiddle; i+) t Leftri] - values[iJ: ntD right- new int[values. length-middle]: or Cint i-a; i<values.length-middle; i++) t rightli] -values [middle+i]; ort(left); //recursively...

  • *c language* Write a program that takes two sorted array A,B of size m,n respectively where...

    *c language* Write a program that takes two sorted array A,B of size m,n respectively where A is sorted in the ascending order and B is sorted in the descending order, and merge these two arrays into a sorted array C.   For example if A=[1, 4, 5, 7, 8] and B=[10,9,8,6,4,2]   then C=[1,2,4,4,5,6,7,8,8,9,10].

  • Please help :) Problem 1: Complete separately Dr. Rocklino a) Write a program in which the...

    Please help :) Problem 1: Complete separately Dr. Rocklino a) Write a program in which the main method creates two arrays, each with 10 int slots. main initializes the first array to randomly generated integers. It then calls a method named copy, passing it the two arrays. Your copy method copies the contents of the first array to the second array. It then returns to main. main then displays the contents of both arrays. b) Write a program in which...

  • Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to...

    Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to enter an item’s name for each position in the array. The program then prints uses a loop to output the array. Example 1: Banana 2: Orange 3: Milk 4: Carrot 5: Chips Banana, Orange, Milk, Carrot, Chips Problem 2: Print Characters in String Array    Declare a string array of size 5. Prompt the user enters five strings that are stored in the array....

  • 4. (7 pts) Let A be an array of 5 integers, whose contents are as follows:...

    4. (7 pts) Let A be an array of 5 integers, whose contents are as follows: 3, 2, 1, 5, 4. We will apply insertion sort to sort this array. Show all of the element-wise comparisons made by the algorithm in the correct order. Here an element-wise comparison means the comparison of one element of the array with another element of the array or the key set in a particular step of the algorithm. Since the algorithm may move the...

  • Write a Java program that implements Insertion Sort and sorts the following Array reverse alphabetically, printing...

    Write a Java program that implements Insertion Sort and sorts the following Array reverse alphabetically, printing the results to the console. {"five", "three", "eight","one","two","four","nine","seven","six","ten"}

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