arrays - How to find smallest number without using java sort() -


how find smallest number without using java sort() method , sorting techniques integer array list? used collections.min(), snippet is:

public class smallestnum {     @suppresswarnings("resource")     public static void main(string[] args) {         scanner sc=new scanner(system.in);         system.out.println("enter size of array::");         int sizee=sc.nextint();         list<integer> numbers=new arraylist<integer>();         for(int i=0;i<sizee;i++) {             numbers.add(sc.nextint());         }         //collections.sort(numbers);         system.out.println(" search value:");         int num=sc.nextint();         integer mini=collections.min(numbers);         system.out.println("minimum value is:"+mini);         for(int i=0;i<sizee;i++) {             system.out.println("minimum:"+(mini));             *if(mini.equals(numbers)) {                 int smallsec=integer.max_value;             }             /*system.out.println(" search value:");             int num=sc.nextint();         }         system.out.println(numbers.get(num-1));             */         }     } } 

this program not working. please suggest alternative logic. don't understand if user want search next smallest number how ?

collections.min directly give minimum value. need additional comparisons. if want find minimum number without using out of box method use logic

integer minnum = integer.max_value; for(integer :  numbers){     if(i < minnum) {         minnum = i;     } } system.out.println("minimum value is: " + minnum); 

considering reading "search value" assume looking else. please give more details trying achieve.

update based on comment.

for use quick select algorithm. program be

import java.util.arraylist; import java.util.list; import java.util.scanner;  public class smallestnum {     @suppresswarnings("resource")     public static void main(string[] args) {         scanner sc = new scanner(system.in);         system.out.println("enter size of array::");         int size = sc.nextint();         list<integer> numbers = new arraylist<integer>();         (int = 0; < size; i++) {             numbers.add(sc.nextint());         }         system.out.println(" search value:");         int n = sc.nextint();         if (n > 0 && n <= size) {             int nthsmall = select(numbers, 0, size - 1, n);             system.out.println("minimum value is: " + nthsmall);         } else {             system.out.println("invalid entry");         }     }      private static int select(list<integer> list, int left, int right, int n) {         int pivotindex = getpivotindex(list, left, right);         if (pivotindex == n - 1) {             return list.get(pivotindex);         }         if ((n - 1) < pivotindex) {             return select(list, left, pivotindex - 1, n);         } else {             return select(list, pivotindex + 1, right, n);         }      }      private static int getpivotindex(list<integer> list, int left, int right) {         int pivot = list.get((left + right) / 2);         while (left < right) {             while (list.get(left) < pivot) {                 left++;             }             while (list.get(right) > pivot) {                 right--;             }              if (left < right) {                 int temp = list.get(left);                 list.set(left, list.get(right));                 list.set(right, temp);                 left++;                 right--;             }          }         return left;     } } 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -