In: Computer Science
Can anyone merge these two java programs I keep getting an error can't find main classes.
MySorts.java
public class MySorts {
public static void insertSort(int[] arr) {
int i, temp, j;
for (i = 1; i < arr.length; i++) {
temp = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}
}
public static void selectSort(int[] arr) {
int temp;
for(int i=0; i<arr.length; i++){
for(int j=i+1; j<arr.length; j++){
if(arr[i]>arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
private static int pivot(int[] arr, int begin, int end) {
int p = arr[end];
int i = (begin-1);
for (int j=begin; j<end; j++)
{
if (arr[j] < p)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i+1];
arr[i+1] = arr[end];
arr[end] = temp;
return i+1;
}
private static void quickSortRecursive(int[] arr, int begin, int end) {
if (begin < end)
{
int pi = pivot(arr, begin, end);
quickSortRecursive(arr, begin, pi-1);
quickSortRecursive(arr, pi+1, end);
}
}
public static void quickSort(int[] arr) {
quickSortRecursive(arr, 0, arr.length-1);
}
public static void mergeSort(int[] arr) {
mergeSortRecursive(arr, 0, arr.length-1);
}
private static void mergeSortRecursive(int[] arr, int begin, int end) {
if (begin < end)
{
int m = (begin+end)/2;
mergeSortRecursive(arr, begin, m);
mergeSortRecursive(arr , m+1, end);
merge(arr, begin, m, end);
}
}
private static void merge(int[] arr, int start, int middle, int end) {
int n1 = middle - start + 1;
int n2 = end - middle;
int L[] = new int [n1];
int R[] = new int [n2];
for (int i=0; i<n1; ++i)
L[i] = arr[start + i];
for (int j=0; j<n2; ++j)
R[j] = arr[middle + 1+ j];
int i = 0, j = 0;
int k = start;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
}
Proj1.java
import java.util.*;
public class Proj1 {
public static final int RANGE = 100000;
/**
* Randomly fill up the given array with integers from 0 to 100000
*/
public static void fillArray(int[] arr) {
Random randGen = new Random();
for (int i = 0; i < arr.length; i++)
arr[i] = randGen.nextInt(RANGE);
}
/**
* copy the content in second array to first array. We assume two array has same
* size
*
* @param firstArray the destination of copy action
* @param secondArray the source of copy action
*/
public static void copy(int[] firstArray, int[] secondArray) {
for (int i = 0; i < secondArray.length; i++)
firstArray[i] = secondArray[i];
}
/**
* isSorted
*
* @return true if the array is sorted from least to largest or is sorted from
* largest to least
*/
public static boolean isSorted(int[] arr) {
if (arr.length <= 1)
return true;
int index = 0;
while (index < arr.length - 1 && arr[index] == arr[index + 1]) {
index++;
}
// bypass first equal elements
if (index == arr.length - 1)
return true; // all elements are the same
else if (index < arr.length - 1 && arr[index] > arr[index + 1]) // possible descend
{
for (int i = index + 2; i < arr.length; i++)
if (arr[i] > arr[i - 1])
return false;
} else // sort for ascend
{
for (int i = index + 2; i < arr.length; i++)
if (arr[i] < arr[i - 1])
return false;
}
return true;
}
/**
* test the sort with given array. The test array will not be modified
*
* @param arr The array that is used to test the sort. The array will not be
* modified
* @name The name of sort method that is to be tested
*/
public static void testSort(int[] arr, String name) {
long totalTime = 0;
int[] a = new int[arr.length];
copy(a, arr); // copy arr to a
// get start time
long start = Calendar.getInstance().getTimeInMillis();
if (name.equals("Insert Sort"))
MySorts.insertSort(a);
else if (name.equals("Select Sort"))
MySorts.selectSort(a);
else if (name.equals("Quick Sort"))
MySorts.quickSort(a);
else if (name.equals("Merge Sort"))
MySorts.mergeSort(a);
long end = Calendar.getInstance().getTimeInMillis();
if (isSorted(a)) {
totalTime = end - start;
System.out.println("Execution time for " + name + " is: " + totalTime + " milliseconds");
} else
System.out.println("The " + name + " did not work properly");
}
/**
* display the test menu
*/
public static void displayMenu() {
System.out.println("***************************");
System.out.println("* MENU *");
System.out.println("* 1. Fill Array *");
System.out.println("* 2. Select Sort *");
System.out.println("* 3. Insert Sort *");
System.out.println("* 4. Quick Sort *");
System.out.println("* 5. Merge Sort *");
System.out.println("* 6. Quit *");
System.out.println("***************************");
}
public static void main(String[] args) {
int choice;
int[] arr = new int[RANGE];
Scanner input = new Scanner(System.in);
do {
displayMenu();
System.out.println("Enter you choice: ");
choice = input.nextInt();
switch (choice) {
case 1: // generate a new random filled array
fillArray(arr);
break;
case 2: // select sort
testSort(arr, "Select Sort");
break;
case 3: // insert sort
testSort(arr, "Insert Sort");
break;
case 4: // quick sort
testSort(arr, "Quick Sort");
break;
case 5: // merge sort
testSort(arr, "Merge Sort");
break;
case 6: // quit
System.out.println("Make sure that you have good documentation!");
break;
default: // wrong choice
}
} while (choice != 6);
}
}
import java.util.*;
class MySorts {
public static void insertSort(int[] arr) {
int i, temp, j;
for (i = 1; i < arr.length; i++) {
temp = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}
}
public static void selectSort(int[] arr) {
int temp;
for(int i=0; i<arr.length; i++){
for(int j=i+1; j<arr.length; j++){
if(arr[i]>arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
private static int pivot(int[] arr, int begin, int end) {
int p = arr[end];
int i = (begin-1);
for (int j=begin; j<end; j++)
{
if (arr[j] < p)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i+1];
arr[i+1] = arr[end];
arr[end] = temp;
return i+1;
}
private static void quickSortRecursive(int[] arr, int begin, int end) {
if (begin < end)
{
int pi = pivot(arr, begin, end);
quickSortRecursive(arr, begin, pi-1);
quickSortRecursive(arr, pi+1, end);
}
}
public static void quickSort(int[] arr) {
quickSortRecursive(arr, 0, arr.length-1);
}
public static void mergeSort(int[] arr) {
mergeSortRecursive(arr, 0, arr.length-1);
}
private static void mergeSortRecursive(int[] arr, int begin, int end) {
if (begin < end)
{
int m = (begin+end)/2;
mergeSortRecursive(arr, begin, m);
mergeSortRecursive(arr , m+1, end);
merge(arr, begin, m, end);
}
}
private static void merge(int[] arr, int start, int middle, int end) {
int n1 = middle - start + 1;
int n2 = end - middle;
int L[] = new int [n1];
int R[] = new int [n2];
for (int i=0; i<n1; ++i)
L[i] = arr[start + i];
for (int j=0; j<n2; ++j)
R[j] = arr[middle + 1+ j];
int i = 0, j = 0;
int k = start;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
}
public class Proj1 {
public static final int RANGE = 100000;
/**
* Randomly fill up the given array with integers from 0 to 100000
*/
public static void fillArray(int[] arr) {
Random randGen = new Random();
for (int i = 0; i < arr.length; i++)
arr[i] = randGen.nextInt(RANGE);
}
/**
* copy the content in second array to first array. We assume two array has same
* size
*
* @param firstArray the destination of copy action
* @param secondArray the source of copy action
*/
public static void copy(int[] firstArray, int[] secondArray) {
for (int i = 0; i < secondArray.length; i++)
firstArray[i] = secondArray[i];
}
/**
* isSorted
*
* @return true if the array is sorted from least to largest or is sorted from
* largest to least
*/
public static boolean isSorted(int[] arr) {
if (arr.length <= 1)
return true;
int index = 0;
while (index < arr.length - 1 && arr[index] == arr[index + 1]) {
index++;
}
// bypass first equal elements
if (index == arr.length - 1)
return true; // all elements are the same
else if (index < arr.length - 1 && arr[index] > arr[index + 1]) // possible descend
{
for (int i = index + 2; i < arr.length; i++)
if (arr[i] > arr[i - 1])
return false;
} else // sort for ascend
{
for (int i = index + 2; i < arr.length; i++)
if (arr[i] < arr[i - 1])
return false;
}
return true;
}
/**
* test the sort with given array. The test array will not be modified
*
* @param arr The array that is used to test the sort. The array will not be
* modified
* @name The name of sort method that is to be tested
*/
public static void testSort(int[] arr, String name) {
long totalTime = 0;
int[] a = new int[arr.length];
copy(a, arr); // copy arr to a
// get start time
long start = Calendar.getInstance().getTimeInMillis();
if (name.equals("Insert Sort"))
MySorts.insertSort(a);
else if (name.equals("Select Sort"))
MySorts.selectSort(a);
else if (name.equals("Quick Sort"))
MySorts.quickSort(a);
else if (name.equals("Merge Sort"))
MySorts.mergeSort(a);
long end = Calendar.getInstance().getTimeInMillis();
if (isSorted(a)) {
totalTime = end - start;
System.out.println("Execution time for " + name + " is: " + totalTime + " milliseconds");
} else
System.out.println("The " + name + " did not work properly");
}
/**
* display the test menu
*/
public static void displayMenu() {
System.out.println("***************************");
System.out.println("* MENU *");
System.out.println("* 1. Fill Array *");
System.out.println("* 2. Select Sort *");
System.out.println("* 3. Insert Sort *");
System.out.println("* 4. Quick Sort *");
System.out.println("* 5. Merge Sort *");
System.out.println("* 6. Quit *");
System.out.println("***************************");
}
public static void main(String[] args) {
int choice;
int[] arr = new int[RANGE];
Scanner input = new Scanner(System.in);
do {
displayMenu();
System.out.println("Enter you choice: ");
choice = input.nextInt();
switch (choice) {
case 1: // generate a new random filled array
fillArray(arr);
break;
case 2: // select sort
testSort(arr, "Select Sort");
break;
case 3: // insert sort
testSort(arr, "Insert Sort");
break;
case 4: // quick sort
testSort(arr, "Quick Sort");
break;
case 5: // merge sort
testSort(arr, "Merge Sort");
break;
case 6: // quit
System.out.println("Make sure that you have good documentation!");
break;
default: // wrong choice
}
} while (choice != 6);
}
}