The code runs correnctly. I am supposed to find the median of a list BUT I cannot
Fantastic news! We've Found the answer you've been seeking!
Question:
The code runs correnctly. I am supposed to find the median of a list BUT I cannot use sorting. So i have to loop through and make lists. The code runs fine, returning the median element of the list (in this instance 2) but the code is supposed to work on large lists too, so I wrote a while loop. But it gets stuck in an infinite while loop, and it is not supposed to.
package lecture4.repitisjon; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Collections; public class mcopy{ public static void main(String[] args) { // ListintList = Arrays.asList(4, 6, 9, 1, 43, 2, 7); //1 2 4 6 7 9 43 MORE List intList = Arrays.asList(3, 2, 1); //1 2 4 6 7 9 43 LESS // List intList = Arrays.asList(2, 1, 3); //1 2 4 6 7 9 43 FIRST int pivot = 0; System.out.println(intList.get(pivot)); //sjekk for MINDRE enn pivot List less = new ArrayList<>(); for(int i = 1; i < intList.size(); i++){ if(intList.get(i) more = new ArrayList<>(); for(int i = 1; i < intList.size(); i++){ if(intList.get(i)>intList.get(pivot)){ more.add(intList.get(i)); } } System.out.println(more); //Hvis første element er median if(less.size() == more.size()){ System.out.println(intList.get(pivot)); } while(less.size() != more.size()){ //skriv != for å få den til å funke if(less.size() < more.size()){ List more_explore = new ArrayList<>(); for(int i = 1; i < more.size(); i++){ // more_explore.add(more.get(i)); if(more.get(i) more.size()){ List less_explore = new ArrayList<>(); for(int i = 1; i < less.size(); i++){ // less_explore.add(less.get(i)); if(less.get(i)>less.get(pivot)){ less_explore.add(less.get(i)); } } if(less_explore.size()==0);{ System.out.println(less.get(pivot)); } } } } }
Please help me getting rid of the infinite while loop it is obly supposed to return 2 one time
CoursHeroTranscribedText
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date: