algorithm - java - largest to smallest sort - largest is out of order -
i'm trying sorting algorithm sort largest smallest in array. here's have:
private void sort(int[] data) { int min; (int index = 0; index < data.length - 1; index++) { min = index; (int scan = index + 1; scan < data.length; scan++) { if (data[scan] > data[min]) min = scan; swap (data, min, index); } } } private void swap(int[] data, int pos0, int pos1) { int temp = data[pos0]; data[pos0] = data[pos1]; data[pos1] = temp; }
the output is:
3 3 4 2 2 2 2 1 1 1 1 1 1
why second largest number out of order?
i keep going through , i'm missing something.
you closing if statement earlier
if (data[scan] > data[min]) min = scan; swap (data, min, index);
swap()
invoked regard less of if
condition
Comments
Post a Comment