[ amel @ 09.05.2006. 13:31 ] @
Pozdrav, postoji li neki drugi i efikasniji nacin pronalazenja drugog najveceg elementa u nizu osim sljedecih mogucnosti Mogucnost 1: Sortirati array a u opadajjucem nizu i izvaditi a[1]; (Mislim da je to sporo) Mogucnost 2: Napisati funkciju "max" za pronalazenje najveceg elementa od array "a" i u drugoj funkciji secondMax ponovo napraviti pretrazivanje najveceg elementa bez koji nije jednak max. Naime Mogucnost 2 bi u Java izgledala IntArray: Code: public class IntArray { private int[] array; public IntArray(int length){ this.array = new int[length]; } public int get(int i){ return this.array[i]; } public void set(int i,int k){ this.array[i] = k; } public int length(){ return this.array.length; } } SecMaxInt: Code: public class secMaxInt extends IntArray{ public secMaxInt(int length) { super(length); } public int max() { int c = this.get(0); for (int i = 1; i < this.length(); i++) { if (this.get(i) > c) { c = this.get(i) } } return c; } public int secmax() { int max = max(); int secmax = 0; for (int i = 0; i < this.length(); i++) { if (this.get(i) > secmax && this.get(i) != max) { secmax = this.get(i); } } return secmax; } } Postoji li kakav brzi algoritam osim drugog? |