Question

I need to program 3 and add to program 2 bellows: Add the merge sort and...

I need to program 3 and add to program 2 bellows:

Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array.

____________________>>>>>>>>>>>>>>>>>>>>___________________________

(This is program 2 code that I did : ) ---->>>>>> code bellow from program 2

java program - Algorithms

Write a program that randomly generates 100,000 integers into an array.

Write a method that sorts this large array using shell sort.

Write a method that sorts this large array using insertion sort.

Write a method that sorts this large array using selection.

Place code in the main method to evaluate the timing of the execution of each sort algorithm and report on each of them

Code program 2

SortDemo.java:
import java.util.Random;

public class SortDemo {

public static int[] createRandomArray(int size) {

int array[] = new int[size];

Random r = new Random();

for (int i = 0; i < size; i++) {

array[i] = r.nextInt() % (int) (size * 1.8);

}

return array;

}

public static int[] getArrayCopy(int[] arr) {

int newArr[] = new int[arr.length];

for (int i = 0; i < arr.length; i++) {

newArr[i] = arr[i];

}

return newArr;

}

public static void shellSort(int array[]) {

int gap = array.length / 2;

boolean swapflag;

while (gap > 0) {

swapflag = true;

while (swapflag) {

swapflag = false;

for (int s = 0; s < (array.length - gap); s++) {

if (array[s] > array[s + gap]) {

int temp = array[s];

array[s] = array[s + gap];

array[s + gap] = temp;

swapflag = true;

}

}

}

gap = gap / 2;

}

}

public static void insertionSort(int arr[]) {

int n = arr.length;

for (int i = 1; i < n; ++i) {

int key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

}

arr[j + 1] = key;

}

}

public static void selectionSort(int arr[]) {

int n = arr.length;

for (int i = 0; i < n - 1; i++) {

int min_idx = i;

for (int j = i + 1; j < n; j++)

if (arr[j] < arr[min_idx])

min_idx = j;

int temp = arr[min_idx];

arr[min_idx] = arr[i];

arr[i] = temp;

}

}

public static void main(String[] args) {

final int SIZE = 100000;

int arr1[] = createRandomArray(SIZE);

int arr2[] = getArrayCopy(arr1);

int arr3[] = getArrayCopy(arr1);

// Now all the 3 arrays have exactly same numbers

long start, end;

start = System.currentTimeMillis();

shellSort(arr1);

end = System.currentTimeMillis();

System.out.println("Shell sort time: " + (end - start) + " milliseconds.");

start = System.currentTimeMillis();

insertionSort(arr2);

end = System.currentTimeMillis();

System.out.println("Insertion sort time: " + (end - start) + " milliseconds.");

start = System.currentTimeMillis();

selectionSort(arr3);

end = System.currentTimeMillis();

System.out.println("Selection sort time: " + (end - start) + " milliseconds.");

}

}

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

//SortDemo.java

import java.util.Random;
public class SortDemo {
public static int[] createRandomArray(int size) {
int array[] = new int[size];
Random r = new Random();
for (int i = 0; i < size; i++) {
array[i] = r.nextInt() % (int) (size * 1.8);
}
return array;
}
public static int[] getArrayCopy(int[] arr) {
int newArr[] = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
return newArr;
}
public static void shellSort(int array[]) {
int gap = array.length / 2;
boolean swapflag;
while (gap > 0) {
swapflag = true;
while (swapflag) {
swapflag = false;
for (int s = 0; s < (array.length - gap); s++) {
if (array[s] > array[s + gap]) {
int temp = array[s];
array[s] = array[s + gap];
array[s + gap] = temp;
swapflag = true;
}
}
}
gap = gap / 2;
}
}
public static void insertionSort(int arr[]) {
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
public static void selectionSort(int arr[]) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}


public static int partitionArray(int arr[], int l, int h)
{
int pivot = arr[h];
int i = (l-1);
for (int j=l; j<h; j++)
{
  
  
if (arr[j] <= pivot)
{
i++;

  
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}

  
int t = arr[i+1];
arr[i+1] = arr[h];
arr[h] = t;

return i+1;
}


public static void quickSort(int arr[], int l, int h)
{
if (l < h)
{
int t = partitionArray(arr, l, h);

quickSort(arr, l, t-1);
quickSort(arr, t+1, h);
}
}


public static void merge(int arr[], int l, int m, int r)
{
  
int len1 = m - l + 1;
int len2 = r - m;

int left[] = new int [len1];
int right[] = new int [len2];

for (int i=0; i<len1; ++i)
left[i] = arr[l + i];
for (int j=0; j<len2; ++j)
right[j] = arr[m + 1+ j];

int i = 0, j = 0;
int k = l;
while (i < len1 && j < len2)
{
if (left[i] <= right[j])
{
arr[k] = left[i];
i++;
}
else
{
arr[k] = right[j];
j++;
}
k++;
}

while (i < len1)
{
arr[k] = left[i];
i++;
k++;
}

while (j < len2)
{
arr[k] = right[j];
j++;
k++;
}
}

public static void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
  
int m = (l+r)/2;
mergeSort(arr, l, m);
mergeSort(arr , m+1, r);
merge(arr, l, m, r);
}
}

public static void main(String[] args) {
final int SIZE = 100000;
int arr1[] = createRandomArray(SIZE);
int arr2[] = getArrayCopy(arr1);
int arr3[] = getArrayCopy(arr1);
int arr4[] = getArrayCopy(arr1);
int arr5[] = getArrayCopy(arr1);

long start, end;
start = System.currentTimeMillis();
shellSort(arr1);
end = System.currentTimeMillis();
System.out.println("Shell sort time: " + (end - start) + " milliseconds.");
start = System.currentTimeMillis();
insertionSort(arr2);
end = System.currentTimeMillis();
System.out.println("Insertion sort time: " + (end - start) + " milliseconds.");
start = System.currentTimeMillis();
selectionSort(arr3);
end = System.currentTimeMillis();
System.out.println("Selection sort time: " + (end - start) + " milliseconds.");

start = System.currentTimeMillis();
quickSort(arr4, 0, arr4.length-1);
end = System.currentTimeMillis();
System.out.println("Quick sort time: " + (end - start) + " milliseconds.");

start = System.currentTimeMillis();
mergeSort(arr5, 0, arr5.length-1);
end = System.currentTimeMillis();
System.out.println("Merge sort time: " + (end - start) + " milliseconds.");

}
}

Add a comment
Know the answer?
Add Answer to:
I need to program 3 and add to program 2 bellows: Add the merge sort and...
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
  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  •   Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first...

      Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first iteration? Given an insertion sort and the following array: [50,5,35,70,6] What does the array look like after the first insertion:    Fill in the missing code for InsertionSort method // Insertion Sort method //sorts an array of Auto objects public static void insertionSort(Auto [] arr) { Auto temp; int j; for (int i=1; i<arr.length; i++) {     j=i;     temp=arr[i];                while (j!=0 &&(temp.getModel().compareTo(arr[[j-1].getModel())<0)...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • I need this program converted to c++ Please help if you can import java.util.*; // Add...

    I need this program converted to c++ Please help if you can import java.util.*; // Add two arrays of the same size (size) // Each array is a representation of a natural number // The returned array will have the size of (size + 1) elements public class Fibonacci { private static String arrToString(int[] arr) {           String s = "";           for (int i = 0; i < arr.length; i++) {               s = s + arr[i];           }...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • Consider the following mergeSortHelper method, which is part of an algorithm to recursively sort an array...

    Consider the following mergeSortHelper method, which is part of an algorithm to recursively sort an array of integers. /**  Precondition: (arr.length == 0 or 0 <= from <= to <= arr.length) * arr.length == temp.length */ public static void mergeSortHelper(int[] arr, int from, int to, int[] temp) { if (from < to) { int middle = (from + to) / 2; mergeSortHelper(arr, from, middle, temp); mergeSortHelper(arr, middle + 1, to, temp); merge(arr, from, middle, to, temp); } } The merge method...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This...

    Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This method should return the median value in the array. (Recall that in a group of numbers half are larger than the median and half are smaller.) Do it the easy way. LISTING 3.3 The insertSort.java Program // insertSort.java // demonstrates insertion sort // to run this program: C>java InsertSortApp //-------------------------------------------------------------- class ArrayIns { private long[] a; // ref to array a private int nElems;...

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