Question

In the most basic/easiest JAVA please change the following code into RECURISON make sure the prog...

In the most basic/easiest JAVA please change the following code into RECURISON make sure the program complies correctly. (It is currently in iterative.) please show comments how they are different after.


//public class
public class MergeSort
{
public static void mergeSort(int[] array)
{
int start;
int i;
int minScan;
int minValue;
int counter = 0;
/**
The outer loop iterates once for each element in the
array. The start variable marks the position where the scan should begin.
**/
for(start = 0; start < array.length-1; start++)
{
//assume the first element in the scannable area is the smallest value.
minScan = start;
minValue = array[start];
/**
Scan the array, starting at the 2nd element in
the scannable area. We are looking for the smallest value in
the scannable area.
**/
for(i = start+1;i < array.length;i++)
{
if(array[i] < minValue)
{
minValue = array[i];
minScan = i;
counter++;
}
}
//swap the element with the smallest value with the first element in the scannable area.
array[minScan] = array [start];
array[start] = minValue;
}
System.out.print("\nSwaps:" + counter); //displays swaps.
}
}


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

CODE:

public class MergeSort
{
//initialise counter variable to 0
static int counter=0;
  
public static void main(String args[] ) throws Exception {
  
//initialise new array
int[] intArray = new int[]{ 10,2,5,4,5,6,7,8,9,11 };
  
//call mergesort function
mergeSort(intArray, intArray.length, 0);
  
//displays swaps.
System.out.print("\nSwaps:" + counter);

}
  
  
static int minIndex(int a[], int i, int j)
{
//if starting ndex and size of the array are same
if (i == j)
return i;

// find minimum of remaining elements using recursion
int k = minIndex(a, i + 1, j);

// Return minimum of current and remaining.
return (a[i] < a[k])? i : k;
}

  
public static void mergeSort(int array[], int n, int position)
{

// Return when starting and size are same
if (position == n)
return;

// calling minimum index function for minimum index
int min = minIndex(array, position, n-1);

// Swapping when index nd minimum index are not same
if (min != position){
// swap
int temp = array[min];
array[min] = array[position];
array[position] = temp;
counter++;
}
// Recursively calling mergeSort function
mergeSort(array, n, position + 1);
}

}

OUTPUT:

media%2Fede%2Fedec4278-780a-40a5-acfb-34


Add a comment
Know the answer?
Add Answer to:
In the most basic/easiest JAVA please change the following code into RECURISON make sure the prog...
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
  • please illistrate a UML diagram for the following code bellow, it should have 3 rows, 1...

    please illistrate a UML diagram for the following code bellow, it should have 3 rows, 1 colum like the one bellow, first box should have the class name, second box is for class attributes, and third box should have the class operations/methods and please put a (+) for public and a (-) for private example: class +-attributes +-Operations Also make a JAVADOCs, but not generated by a compiler, to explain what is going on in the code import java.util.Random; public...

  • Can someone please explain this piece of java code line by line as to what they...

    Can someone please explain this piece of java code line by line as to what they are doing and the purpose of each line (you can put it in the code comments). Code: import java.util.*; public class Array { private Integer[] array; // NOTE: Integer is an Object. Array() {     super();     array = new Integer[0]; } Array(Array other) {     super();     array = other.array.clone(); // NOTE: All arrays can be cloned. } void add(int value) {    ...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Java question Q1) Use the following code snippet which generates a random sized array with random...

    Java question Q1) Use the following code snippet which generates a random sized array with random contents to complete the following problems: public int [] createRandomArray() {         int size = (int) (Math.random() * 10) + 1;         int[] array = new int [size];         for (int i = 0; i < array.length; i++) {             array[i] = (int) (Math.random() * 10 ) + 1;         }         return array;     } Assignment...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • in java please fix code between 14 and 20 to make the code compile Write a...

    in java please fix code between 14 and 20 to make the code compile Write a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10). The array's size may differ from 4. Display ari ay Values 1 pas [ pa: 6 public void displayValues(int [] arrayVals) { 7 int i; 8 9 for (i = 0; i < arrayVals.length; ++i) { 19 System.out.print(arrayVals[i] +...

  • Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the a...

    Another simple sort is the odd-even sort. The idea is to repeatedly make two passes through the array. On the first pass you look at all the pairs of items, a[j] and a[j+1], where j is odd (j = 1, 3, 5, …). If their key values are out of order, you swap them. On the second pass you do the same for all the even values (j = 2, 4, 6, …). You do these two passes repeatedly until...

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