java - Selection Sort Swap Count -
i have array including user's inputs. programme bubble sort, selection sort , insertion sort. couldn't manage solve problem in selection part. swap counter gives me array's length - 1. know it's because of first loop couldn't find solutions though.
int scomparison = 0; int sswaps = 0; (int = 0; < sorbag.length - 1; i++) { min = i; (int j = + 1; j < sorbag.length; j++) { scomparison++; if (sorbag[j] < sorbag[min]) { min = j; } } sswaps++; temp2 = sorbag[i]; sorbag[i] = sorbag[min]; sorbag[min] = temp2; }
i tried putting sswaps "if", gives me 0. tried making "if" find how many array elements smaller first ones. still same. should set sswaps++ ? give thanks !
selection sort updates min index comparing (your if ...
), , element swapping if needed.
you set swapps++
@ right position. should check , if min == i
. want swap if min !=i
. instead, didn't check that, swap anyway. that's why got fixed length -1
p.s. please seek follow java naming convention.
java arrays sorting
No comments:
Post a Comment