Question: How to take this java program output is produced by graphical method for each times.? import java.util.*; public class mySortingAlgorithm { public static void main(String[]
How to take this java program output is produced by graphical method for each times.?
import java.util.*;
public class mySortingAlgorithm {
public static void main(String[] args)
{
int times=0;
while(times<30)
{
System.out.println("Times value="+times);
int n = 50000;
int[] a = createRandomIntArray(n);
int[] a4mySort = Arrays.copyOf(a, a.length);
int[] a4mergeSort = Arrays.copyOf(a, a.length);
int[] a4selectionSort = Arrays.copyOf(a, a.length);
// call my algorithm
// System.out.println("before my Sort : "+ Arrays.toString(a4mySort));
long start = System.currentTimeMillis();
mySort(a4mySort);
double elapsed0 = (System.currentTimeMillis() - start) / 1000.0;
System.out.println("my sort took " + elapsed0 + " seconds");
// System.out.println( "after my Sort : "+Arrays.toString(a4mySort));
System.out.println();
// call merge sort
// System.out.println("before merge Sort : "+ Arrays.toString(a4mergeSort));
start = System.currentTimeMillis();
mergeSort(a4mergeSort);
double elapsed1 = (System.currentTimeMillis() - start) /1000.0;
System.out.println("merge sort took " + elapsed1 + " seconds");
// System.out.println( "After merge Sort : " +Arrays.toString(a4mergeSort));
System.out.println();
// call selction sort
//System.out.println("before selection Sort : "+ Arrays.toString(a4selectionSort));
start = System.currentTimeMillis();
Arrays.sort(a4selectionSort);
double elapsed2 = (System.currentTimeMillis() - start) / 1000.0;
System.out.println("selection sort took " + elapsed2 + " seconds");
// System.out.println("after selection sort : " + Arrays.toString(a4selectionSort));
System.out.println();
times++;
}
}
public static int[] createRandomIntArray(int size) {
int[] numbers = new int[size];
Random rand = new Random();
int min = rand.nextInt(size);
int max = rand.nextInt(size - min) + min;
for (int i = 0; i< size; i++) {
// numbers[i] = rand.nextInt(max - min + 1) + min;
numbers[i] = rand.nextInt(size * 2);
}
return numbers;
}
// Returns true if array a's elements are in sorted order.
public static boolean isSorted(int[] a) {
for (int i = 0; i< a.length - 1; i++) {
if (a[i] > a[i+1]) {
return false;
}
}
return true;
}
// Swaps a[i] with a[j].
public static void swap(int[] a, int i, int j) {
if (i != j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
// my sorting Algorithm
public static void mySort(int[] array)
{
int tot = array.length-1;
sort(array);
for (int i = tot; i> 0; i--)
{
swap(array,0, i);
tot = tot-1;
max(array, 0);
}
}
public static void sort(int array[])
{
int tot = array.length-1;
tot = array.length-1;
for (int i = tot/2; i> = 0; i--)
max(array, i);
}
public static void max(int array[], int i)
{
int tot = array.length-1;
int val1 = 2*i ;
int val2 = 2*i + 1;
int max = i;
if (val1 <= tot&& array[val1] > array[i])
max = val1;
if (val2 <= tot&& array[val2] > array[max])
max = val2;
if (max != i)
{
swap(array, i, max);
max(array, max);
}
}
// selection sort
public static void selectionSort(int[] a) {
for (int i = 0; i< a.length - 1; i++) {
// find index of smallest element
int smallest = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[smallest]) {
smallest = j;
}
}
swap(a, i, smallest); // swap smallest to front
}
}
//merge sort
public static void mergeSort(int[] array) {
if (array.length > 1) {
// split array into two smaller arrays
int size1 = array.length / 2;
int size2 = array.length - size1;
int[] half1 = new int[size1];
int[] half2 = new int[size2];
for (int i = 0; i < size1; i++) {
half1[i] = array[i];
}
for (int i = 0; i < size2; i++) {
half2[i] = array[i + size1];
}
// recursively sort the two smaller arrays
mergeSort(half1);
mergeSort(half2);
// merge the sorted halves into a sorted whole
merge(array, half1, half2);
}
}
// 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++) {
if (i2 >= right.length ||
(i1 < left.length &&
left[i1]<=right[i2])) {
result[i] = left[i1];
i1++;
} else {
result[i] = right[i2];
i2++;
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
