Question
I have a question for JAVA ?! 1.Develop your own sorting algorithm 2.Analyze your algorithm and compare with merge sort and selection sort. [Pseudo Code
I have a question for JAVA ?!
1.Develop your own sorting algorithm
2.Analyze your algorithm and compare with merge sort and selection sort.
[Pseudo Code Example]
Times = 0;
While (times<30)
Create an array for random integer number (array size=50000)
check start time
call your algorithm
check execution time
check start time
call merge sort
check execution time
check start time
call selection sort
check execution time
times++;
}
print each execution time;
* If your output is produced by graphical method for each times. Your score += 10%
Skeleton Code is :
import java.util.*;
public class mySortingAlgorithm { public static void main(String[] args) { 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(); } 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) { } // 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started