sorting - Methods aren't returning anything in Java -


i wrote class in java 5 methods. linear search, returns true if value found , false if it's not found. linear search 2, returns location of value, if found. binary search. searches value within array well, print int array, prints 10 numbers time, , selection sort, sorts array can binary search. compiling fine, reason, none of methods returning (except void printintarray method).

edit:


thanks, guys, didn't realize needed that. reason thought return value on own... question, though. binarysearch method doesn't appear doing anything. after print statement "searching 11 in random array using binary search"...., nothing printed.


edit 2: binarysearch method wasn't working because accidentally had mid + 1 both else statements (else if (key < array[mid]) should have been mid - 1). everyone! added fixes.

public class sortingsearching {    public static boolean linearsearch (int [] array, int key) {     (int = 0; < array.length; i++) {        if (array [i] == key)            return true;     }// end     return false; }  public static int linearsearch2 (int [] array, int key) {    (int = 0; < array.length; i++) {        if (array [i] == key)         return i;     }//end     return -1; }//end linearsearch2  public static boolean binarysearch (int [] array, int key) {   int left = 0;     int right = array.length - 1;     int mid = (left + right) /2;        while (left <= right) {            if (array[mid] == key)                return true;             else if ( key < array[mid])                right = mid - 1;             else                 left = mid + 1;             mid = (left + right) /2;         } //end while     return false; }//end binarysearch  public static void printintarray (int [] array) {    (int = 0; < array.length; i++) {        if (i%10 == 0)            system.out.println();         system.out.print(array[i] + " ");     } // end }  public static void selectionsort (int [] array) {    (int start = 0; start < array.length - 1; start ++) {        int mini = start;         (int = start + 1; < array.length; i++)            if (array[i] < array[start])                mini = i;        int temp = array[start];     array[start] = array[mini];        array[mini] = temp;      }//end    } //end selectionsort  public static void main (string args []) {    int [] array = new int [20];     (int =0; < array.length; i++)        array[i] = (int)((math.random() * 100) + 1);      //print array using printarray       printintarray(array);     system.out.println();   //use linearsearch search 30, 86, , 87     system.out.println("searching 30 in random array. if true returned, " +     "the value found. if false returned, value not found.");       system.out.println(linearsearch(array, 30));     system.out.println("searching 86 in random array. if true returned, " +     "the value found. if false returned, value not found.");        system.out.println(linearsearch(array, 86));     system.out.println("searching 87 in random array. if true returned, " +     "the value found. if false returned, value not found.");        system.out.println(linearsearch(array, 87));     //use linearsearch locate first occurrences of 25, 80, , 91     system.out.println("searching location of 25 in random array. if -1 " +      "returned, number not found in array.");        system.out.println(linearsearch2(array, 25));     system.out.println("searching location of 80 in random array. if -1 " +      "returned, number not found in array.");        system.out.println(linearsearch2(array, 80));     system.out.println("searching location of 91 in random array. if -1 " +      "returned, number not found in array.");        system.out.println(linearsearch2(array, 91));     //use selectionsort sort array   selectionsort(array);     //use binarysearch search 11, 28, 74, , 99   system.out.println("searching 11 in random array using binary search. if true returned, " +     "the value found. if false returned, value not found.");        system.out.println(binarysearch (array, 11));   system.out.println("searching 28 in random array using binary search. if true returned, " +     "the value found. if false returned, value not found.");        system.out.println(binarysearch (array, 28));   system.out.println("searching 74 in random array using binary search. if true returned, " +     "the value found. if false returned, value not found.");        system.out.println(binarysearch (array, 74));   system.out.println("searching 99 in random array using binary search. if true returned, " +     "the value found. if false returned, value not found.");        system.out.println(binarysearch (array, 99)); } //end main   } //end sortingsearching 

also, i'm sorry print statements in main method distracting. thought taking them out ease of reading, wanted i've been running it.

linearsearch(array, 30); 

they return something. return value!

boolean value = linearsearch(array, 30); system.out.println(value); 

or simpler:

system.out.println(linearsearch(array, 30)); 

in response edit

you need initiate left 1. you're performing integer division, can never reach zero. hence right gets stuck on 1 , left less it.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -