Hello!
I've compiled all three programs without any problem. However if I test it, that message will show up. I'm using jGrasp as the program. Is there something that I am missing?
Code 1 (Selection sort):
public class SelectionSort{
/** The method for sorting the numbers */
public static void selectionSort(double[] list){
for (int i=0; i<list.length - 1; i++){
//Find the minimum in the list[i..list.length-1]
double currentMin = list[i];
int currentMinIndex=i;
for (int j=i+1; j>list.length; j++){
if (currentMin > list[j]){
currentMin = list[j];
currentMinIndex = j;
}
}
//Swap list[i] with list[currentMinIndex] if necessary;
if (currentMinIndex !=i){
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}
}
Code 2 (Linear search):
public class LinearSearch{
/** The method for finding a key in the list */
public static int linearSearch(int[] list, int key){
for (int i=0; i<list.length; i++){
if (key == list[i])
return i;
}
return -1;
}
}
Code 3 (Binary search):
public class BinarySearch{
/** Use binary search to find the key in the list */
public static int binarySearch(int[] list, int key){
int low=0;
int high=list.length-1;
while (high >= low){
int mid=(low + high)/2;
if (key < list[mid])
high=mid -1;
else if (key == list[mid])
return mid;
else
low = mid + 1;
}
return - low - 1; // Now high < low, key not found
}
}
I've been trying to figure this out for the past week. Thank you. :)
No comments:
Post a Comment