I am trying to figure out the selection sort algorithm and understand it by printing out each line, but when I run it, the console won't even pop up. I know my error has to do something with the main class exception not found and the main string args method but I don't know how to fix it to see the output.
package sort;
public class Sorting {
private int[] a = new int[]{ 11, 9, 17, 5, 12 };
public Sorting(int[]a) {
this.a = a;
}
public static void main(String[] args) {
}
public void sortInSpace(int[]a) {
for(int startOfUnsorted = 0; startOfUnsorted<a.length; startOfUnsorted++) {
int smallestInUnsorted = Integer.MAX_VALUE;
System.out.println(smallestInUnsorted);
}
}
}
Your main function in Sorting is empty so it won't print anything if you try to run that class.
public static void main(String[] args) {
}
Instead I would do the following -
public class Sorting {
private int[] a;
public Sorting(int[]a) {
this.a = a;
}
public void sortInSpace(int[]a) {
for(int startOfUnsorted = 0; startOfUnsorted<a.length; startOfUnsorted++)
{
int smallestInUnsorted = Integer.MAX_VALUE;
System.out.println(smallestInUnsorted);
}
}
}
Create another class which will initiate an object type of Sorting and use the object to call the method.
public static void main(String [] args)
{
int[] a = new int[]{ 11, 9, 17, 5, 12 };
Sorting sortObj = new Sorting(a);
sortObj.sortInSpace(a);
}
Output -
2147483647
2147483647
2147483647
2147483647
2147483647
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.