Skip to content

Instantly share code, notes, and snippets.

@tranvanthuc
Created October 27, 2016 03:46
Show Gist options
  • Save tranvanthuc/59691c0e23e6560f859d4d6311ed2fd3 to your computer and use it in GitHub Desktop.
Save tranvanthuc/59691c0e23e6560f859d4d6311ed2fd3 to your computer and use it in GitHub Desktop.
package src;
import java.util.Arrays;
public class MainClass {
private static double[] arr;
private static double[] tempArr;
public static String toStringArray(){
String result = "\nArray default: "+SortingAlgorithms.toStringArray(arr);
result += "\nArray sorted: "+SortingAlgorithms.toStringArray(tempArr);
return result;
}
private static String setSecond(long time){
long temp = time/1000000;
if(temp < 1){
return "\nTime : "+time +" nanoS";
}
else {
return "\nTime : "+ temp + " miliS "+(time - temp*1000000) +" nanoS";
}
}
public static void setArray(double[] myArray){
arr = new double[myArray.length];
System.arraycopy(myArray, 0, arr, 0, myArray.length);
tempArr = new double[arr.length];
System.arraycopy(arr, 0, tempArr, 0, arr.length);
Arrays.sort(tempArr);
}
public static void createRandomArray(int size, int min, int max){
arr = new double[size];
for(int i=0;i<arr.length;i++){
arr[i] = min + (Math.random()*(max - min) + 1);
}
tempArr = new double[arr.length];
System.arraycopy(arr, 0, tempArr, 0, arr.length);
Arrays.sort(tempArr);
}
public static String mainBubbleSort(){
long startTime = System.nanoTime();
double[] bubbleSortArr = SortingAlgorithms.bubbleSort(arr);
long endTime = System.nanoTime();
boolean boo = SortingAlgorithms.compareArray(tempArr, bubbleSortArr);
String result ="\n____________________";
/*result += SortingAlgorithms.toStringArray(bubbleSortArr);*/
result += "\nBubbleSort: "+ boo;
long time = Long.parseLong(Long.toString(endTime - startTime));
result += setSecond(time);
return result;
}
public static String mainSelectionSort(){
long startTime = System.nanoTime();
double[] selectionSortArr = SortingAlgorithms.selectionSort(arr);
long endTime = System.nanoTime();
boolean boo = SortingAlgorithms.compareArray(tempArr, selectionSortArr);
String result ="\n____________________";
/*result += SortingAlgorithms.toStringArray(selectionSortArr);*/
result += "\nSelectionSort: "+ boo;
long time = Long.parseLong(Long.toString(endTime - startTime));
result += setSecond(time);
return result;
}
public static String mainInsertionSort(){
long startTime = System.nanoTime();
double[] insertionSortArr = SortingAlgorithms.insertionSort(arr);
long endTime = System.nanoTime();
boolean boo = SortingAlgorithms.compareArray(tempArr, insertionSortArr);
String result ="\n____________________";
/*result += SortingAlgorithms.toStringArray(insertionSortArr);*/
result += "\nInsertionSort: "+ boo;
long time = Long.parseLong(Long.toString(endTime - startTime));
result += setSecond(time);
return result;
}
public static String mainTopDownMergeSort(){
double [] B= new double [arr.length];
long startTime = System.nanoTime();
double[] topDownMergeSortArr = SortingAlgorithms.topDownMergeSort(arr,B);
long endTime = System.nanoTime();
boolean boo = SortingAlgorithms.compareArray(tempArr, topDownMergeSortArr);
String result ="\n____________________";
/*result += SortingAlgorithms.toStringArray(topDownMergeSortArr);*/
result += "\nTopDownMergeSort: "+ boo;
long time = Long.parseLong(Long.toString(endTime - startTime));
result += setSecond(time);
return result;
}
public static String mainHeapSort(){
long startTime = System.nanoTime();
double[] heapSortArr = SortingAlgorithms.heapSort(arr);
long endTime = System.nanoTime();
boolean boo = SortingAlgorithms.compareArray(tempArr, heapSortArr);
String result ="\n____________________";
/*result += SortingAlgorithms.toStringArray(heapSortArr);*/
result += "\nHeapSortArr: "+ boo;
long time = Long.parseLong(Long.toString(endTime - startTime));
result += setSecond(time);
return result;
}
//quick sort wrong !
public static String mainQuickSort(){
long startTime = System.nanoTime();
double[] quickSortArr = SortingAlgorithms.quickSort(arr);
long endTime = System.nanoTime();
boolean boo = SortingAlgorithms.compareArray(tempArr, quickSortArr);
String result ="\n____________________";
/*result += SortingAlgorithms.toStringArray(quickSortArr);*/
result += "\nQuickSortArr: "+ boo;
long time = Long.parseLong(Long.toString(endTime - startTime));
result += setSecond(time);
return result;
}
public static String mainShellSort(){
long startTime = System.nanoTime();
double[] shellSortArr = SortingAlgorithms.shellSort(arr);
long endTime = System.nanoTime();
boolean boo = SortingAlgorithms.compareArray(tempArr, shellSortArr);
String result ="\n____________________";
/*result += SortingAlgorithms.toStringArray(shellSortArr);*/
result += "\nShellSortArr: "+ boo;
long time = Long.parseLong(Long.toString(endTime - startTime));
result += setSecond(time);
return result;
}
public static String mainCombSort(){
long startTime = System.nanoTime();
double[] combSortArr = SortingAlgorithms.combSort(arr);
long endTime = System.nanoTime();
boolean boo = SortingAlgorithms.compareArray(tempArr, combSortArr);
String result ="\n____________________";
/*result += SortingAlgorithms.toStringArray(combSortArr);*/
result += "\nCombSortArr: "+ boo;
long time = Long.parseLong(Long.toString(endTime - startTime));
result += setSecond(time);
return result;
}
public static void main(String[] args) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment