Question

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

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


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

MergeSort.java

import java.util.Scanner;

public class MergeSort
{
   public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);

       System.out.print("Enter the size of unsorted array: ");
       int n = sc.nextInt();

       int[] list = new int[n];

       for (int i = 0; i < n; i++)
       {
           System.out.printf("Enter %d element: ", i+1);
           list[i] = sc.nextInt();
       }

       System.out.println("\n-----------------------------------------\n");
       System.out.println("List before sorting: ");
      
       for (int i = 0; i < list.length; i++)
           System.out.print(list[i] + " ");

       mergeSort(list, 0, list.length - 1);

       System.out.println("\n-----------------------------------------\n");
      
       System.out.print("List after sorting: ");
       for (int i = 0; i < list.length; i++)
           System.out.print(list[i] + " ");

       sc.close();           // closing scanner is important
   }

   public static void mergeSort(int list[], int low, int high)
   {
       if (low >= high)
           return;

       int middle = (low + high) / 2;

       mergeSort(list, low, middle);
       mergeSort(list, middle + 1, high);
       merge(list, low, middle, high);
   }

   private static void merge(int list[], int low, int middle, int high)
   {

       int endIst = middle;

       int endIInd = middle + 1;

       int l = low;

       while ((l <= endIst) && (endIInd <= high))
       {
           if (list[low] < list[endIInd])
               low++;
           else
           {
               int temp = list[endIInd];
               for (int j = endIInd - 1; j >= low; j--)
                   list[j + 1] = list[j];

               list[low] = temp;
               low++;

               endIst++;
               endIInd++;
           }
       }
   }

}

Output:

Add a comment
Answer #2

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:


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

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

  • 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(); }    }...

  • Hello, I am getting two errors with this code and I don't understand why? can you...

    Hello, I am getting two errors with this code and I don't understand why? can you help me please? This is JAVA public class Main { private static int partition(int a[],int start,int end) //this function takes first element as pivot. { int pivotValue; int endOfLeftList; pivotValue = a[start]; endOfLeftList = start; // At this point A[endOfLeftList] == pivotValue for (int scan = start + 1; scan <= end; scan ++) { if (a[scan] < pivotValue) { endOfLeftList ++; swap(a, endOfLeftList,...

  • 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) {    ...

  • 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 help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

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

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