07.01.2015 Views

Opgaver

Opgaver

Opgaver

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

* the specified object.<br />

*/<br />

public int compareTo(Object o);<br />

Her er en sorteringsalgoritme selectionSort, der kan sortere et array af<br />

type C[] for enhver klasse C, der implementerer Comparable (og det gør<br />

f.eks. String og Integer):<br />

public class SortModel<br />

{<br />

public void selectionSort(Comparable[] l)<br />

{<br />

int q = 0;<br />

while(q < l.length) {<br />

int m = findLeast(l,q,l.length);<br />

Comparable temp = l[q]; l[q] = l[m]; l[m] = temp;<br />

q++;<br />

}<br />

}<br />

}<br />

private int findLeast(Comparable[] r, int i1, int i2)<br />

{<br />

int min = i1;<br />

int q = i1 + 1;<br />

while(q < i2) {<br />

if(r[q].compareTo(r[min]) < 0) { min = q; }<br />

q++;<br />

}<br />

return min;<br />

}<br />

56

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!