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:

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 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, 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 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 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. 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 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, 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 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...
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] +...