Your running times will probably be different than these. Please do a better job with the snipping tool than I did.
Java program provided:
// Student Name Today's Date
import java.util.Arrays;
import java.util.Random;
public class SortTimer {
// Please expand method main() to meet the lab
requirements.
// You have the following sorting methods
available:
// insertionSort(int[] a);
// selectionSort(int[] a);
// mergeSort(int[] a);
// quickSort(int[] a);
// The array will be in sorted order after the
routines are called!
// Be sure to re-randomize the array after each
sort.
public static void main(String[] args) {
// Create and initialize
arrays
int[] a = {1, 3, 5}, b, c, d;
// Check the time to sort array
a
long startTime =
System.nanoTime();
quickSort(a);
long endTime =
System.nanoTime();
long duration = (endTime -
startTime) / 1000l;
// Output results
System.out.println("Working on an
array of length " + a.length + ".");
System.out.println("Quick sort: " +
duration + "us.");
}
//
//
###############################################################
//
// Standard implementations of sorting algorithms
follow.
//
// Please do not include these methods in your lab
submission
//
//
###############################################################
//
// Thanks to
https://www.javatpoint.com/insertion-sort-in-java
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++)
{
int key =
array[j];
int i = j -
1;
while ((i >
-1) && (array[i] > key)) {
array[i + 1] = array[i];
i--;
}
array[i + 1] =
key;
}
}
// Thanks to
//
http://www.java2novice.com/java-sorting-algorithms/selection-sort/
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length -
1; i++) {
int index =
i;
for (int j = i +
1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int
smallerNumber = arr[index];
arr[index] =
arr[i];
arr[i] =
smallerNumber;
}
}
// Thanks to
http://stackoverflow.com/questions/13727030/mergesort-in-java
public static void mergeSort(int[] A) {
if (A.length > 1) {
int q = A.length
/ 2;
// changed
range of leftArray from 0-to-q to 0-to-(q-1)
int[] leftArray
= Arrays.copyOfRange(A, 0, q - 1);
// changed range
of rightArray from q-to-A.length to
//
q-to-(A.length-1)
int[] rightArray
= Arrays.copyOfRange(A, q, A.length - 1);
mergeSort(leftArray);
mergeSort(rightArray);
merge(A,
leftArray, rightArray);
}
}
private static void merge(int[] a, int[] l, int[]
r) {
int totElem = l.length +
r.length;
// int[] a = new
int[totElem];
int i, li, ri;
i = li = ri = 0;
while (i < totElem) {
if ((li <
l.length) && (ri < r.length)) {
if (l[li] < r[ri]) {
a[i] = l[li];
i++;
li++;
} else {
a[i] = r[ri];
i++;
ri++;
}
} else {
if (li >= l.length) {
while (ri < r.length)
{
a[i] =
r[ri];
i++;
ri++;
}
}
if (ri >= r.length) {
while (li < l.length)
{
a[i] =
l[li];
li++;
i++;
}
}
}
}
// return a;
}
// Thanks to:
http://www.algolist.net/Algorithms/Sorting/Quicksort
public static void quickSort(int arr[]) {
quickSortRecurse(arr, 0,
arr.length-1);
}
private static void quickSortRecurse(int arr[], int
left, int right) {
int index = partition(arr, left,
right);
if (left < index - 1)
quickSortRecurse(arr, left, index - 1);
if (index < right)
quickSortRecurse(arr, index, right);
}
private static int partition(int arr[], int left,
int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) /
2];
while (i <= j) {
while (arr[i]
< pivot)
i++;
while (arr[j]
> pivot)
j--;
if (i <= j)
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
;
return i;
}
}
When answered could you please provide the full program!
thank you.
-------------------------Screenshot-----------------------
-------------------------Code-----------------------
// Student Name Today's Date
import java.util.Arrays;
import java.util.Random;
public class SortTimer {
// Please expand method main() to meet the lab
requirements.
// You have the following sorting methods available:
// insertionSort(int[] a);
// selectionSort(int[] a);
// mergeSort(int[] a);
// quickSort(int[] a);
// The array will be in sorted order after the routines are
called!
// Be sure to re-randomize the array after each sort.
public static int[] copy(int[] b, int n){
int[] a = new int[n];
for(int i=0;i<n;i++){
a[i] = b[i];
}
return a;
}
public static void main(String[] args) {
Random randomGenerator = new Random();
int[] sizes = {100,1000,10000,100000,1000000};
for(int n: sizes){
int[] b;
b = new int[n];
for(int i=0;i<n;i++){
b[i] = randomGenerator.nextInt();
}
System.out.println("Working on an array of length " + n +
".");
long startTime, endTime, duration;
int[] a;
// QuickSort
a = copy(b,n);
startTime = System.nanoTime();
quickSort(a);
endTime = System.nanoTime();
duration = (endTime - startTime) / 1000l;
System.out.println("Quick sort: " + duration + "us.");
a = copy(b,n);
startTime = System.nanoTime();
mergeSort(a);
endTime = System.nanoTime();
duration = (endTime - startTime) / 1000l;
System.out.println("Merge sort: " + duration + "us.");
a = copy(b,n);
startTime = System.nanoTime();
insertionSort(a);
endTime = System.nanoTime();
duration = (endTime - startTime) / 1000l;
System.out.println("Insertion sort: " + duration + "us.");
a = copy(b,n);
startTime = System.nanoTime();
selectionSort(a);
endTime = System.nanoTime();
duration = (endTime - startTime) / 1000l;
System.out.println("Selection sort: " + duration + "us.");
System.out.println();
}
}
//
//
###############################################################
//
// Standard implementations of sorting algorithms follow.
//
// Please do not include these methods in your lab submission
//
//
###############################################################
//
// Thanks to
https://www.javatpoint.com/insertion-sort-in-java
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j - 1;
while ((i > -1) && (array[i] > key)) {
array[i + 1] = array[i];
i--;
}
array[i + 1] = key;
}
}
// Thanks to
//
http://www.java2novice.com/java-sorting-algorithms/selection-sort/
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
// Thanks to
http://stackoverflow.com/questions/13727030/mergesort-in-java
public static void mergeSort(int[] A) {
if (A.length > 1) {
int q = A.length / 2;
// changed range of leftArray from 0-to-q to 0-to-(q-1)
int[] leftArray = Arrays.copyOfRange(A, 0, q - 1);
// changed range of rightArray from q-to-A.length to
// q-to-(A.length-1)
int[] rightArray = Arrays.copyOfRange(A, q, A.length - 1);
mergeSort(leftArray);
mergeSort(rightArray);
merge(A, leftArray, rightArray);
}
}
private static void merge(int[] a, int[] l, int[] r) {
int totElem = l.length + r.length;
// int[] a = new int[totElem];
int i, li, ri;
i = li = ri = 0;
while (i < totElem) {
if ((li < l.length) && (ri < r.length)) {
if (l[li] < r[ri]) {
a[i] = l[li];
i++;
li++;
} else {
a[i] = r[ri];
i++;
ri++;
}
} else {
if (li >= l.length) {
while (ri < r.length) {
a[i] = r[ri];
i++;
ri++;
}
}
if (ri >= r.length) {
while (li < l.length) {
a[i] = l[li];
li++;
i++;
}
}
}
}
// return a;
}
// Thanks to:
http://www.algolist.net/Algorithms/Sorting/Quicksort
public static void quickSort(int arr[]) {
quickSortRecurse(arr, 0, arr.length-1);
}
private static void quickSortRecurse(int arr[], int left, int
right) {
int index = partition(arr, left, right);
if (left < index - 1)
quickSortRecurse(arr, left, index - 1);
if (index < right)
quickSortRecurse(arr, index, right);
}
private static int partition(int arr[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
;
return i;
}
}
Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...
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....
Please merge all the codes below and add comments using JAVA Program. I need a complete code which is the combination of the following codes: // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted public static void merge(int[] result, int[] left, int[] right) { int i1 = 0; // index into left array int i2 = 0; // index into right array for (int i = 0; i < result.length; i++)...
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void insertionSort(int arr[], int n); void merge(int a[], int l1, int h1, int h2); void mergeSort(int a[], int l, int h) { int i, len=(h-l+1); //Using insertion sort for small sized array if (len<=5) { insertionSort(a+l, len); return; } pid_t lpid,rpid; lpid = fork(); if(lpid<0) { //Lchild proc not created perror("Left Child Proc. not created\n"); _exit(-1); } else if (lpid==0) { mergeSort(a,l,l+len/2-1); _exit(0); } else...
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)...
Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static 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,...
COMPLETE BigMerge public class BigMerge { /* * Returns j such that a[j][index[j]] is the minimum * of the set S={a[i][index[i]] for every i such that index[i]<a[i].length} * If the set S is empty, returns -1 * Runs in time a.length. * * NOTE: normally this would be private, but leave it * public so we can test it separately from your merge. */ public static int argMin(int [][]...
Hello, I want to check if my C++ code is correct and follows the requeriments described thanks. Requeriments: Assignment Sorting Benchmark each of the sorting methods listed below. Insertion Sort Bubble Sort Selection Sort Heap Sort. Quick Sort. Merge Sort. Benchmark each of the above sorting methods for data sizes of 10000, 20000, 30000, 40000 and 50000. Display the results in a table as shown below. The table should have rows and columns. However, the rows and columns need not...
Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...