Hello all. First post on this forum. I only have experience in C++ and a little bash. I found a Java bubble sort online and I tried to use it as a guide to convert my C++ code to Java. I'm not using an IDE. I downloaded a Java compiler for my Linux terminal. I'm using a bubble sort algorithm here and reading from an array. You can see on line 2 and 3 how I'm compiling and running it. Line 5, "package test;" not sure if that is what I need there. Here's the code:
Java Code:
// BubbleSort Java
// compile with: javac BubbleSort.java
// run with: java BubbleSort
package test;
import java.util.Arrays;
public class BubbleSort {
private static int swapHolder = 0;
private static int sizeOfArray = 10;
private static int pass = 0;
public static void main(String args[]) {
int[] myArray = {44,9,237,1,5,34,534,17,21,819};
bubbleSort(myArray);
}
public static void bubbleSort(int[] myArray){
while(pass < sizeOfArray) {
for(int j=0; j < sizeOfArray - pass; j++) {
if(myArray[j] > myArray[j+1]) { // needs to be swapped
swapHolder = myArray[j+1];
myArray[j+1] = myArray[j]; // this makes both the same
myArray[j] = swapHolder;
}
} // end of pass
pass++;
}
//**first pass puts greatest number in last index**
//cout << "The array sorted from lowest to highest:" << endl;
System.out.printf("The array sorted from lowest to highest:\n");
for(int j=0; j < sizeOfArray; j++){
//cout << myArray[i] << " " ;
System.out.printf(myArray[j] + " ");
}
}
}
I compiles but I get these errors when I run it:
Exception in thread "main" java.lang.NoClassDefFoundError: BubbleSort (wrong name: test/BubbleSort)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java :800)
at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader .java:449)
at java.net.URLClassLoader.access$100(URLClassLoader. java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 361)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:4 25)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 58)
at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.java:482)
No comments:
Post a Comment