Please help me find what's wrong with my code. For academic honesty I am just sharing the output right now but can provide if needed and the algorithm for binary search and selection sort which are as follows : BINARY SEARCH
1 Answer
Not sure if I'm interpreting your pseudo-code correctly.
In selection sort, make sure to keep track of the index of the smallest element, you'll need it for swapping. Swap two array elements, not an array element and a variable or maybe even two variables.
[Edit]
Also: Do not min = values[i];
within the loop, as this might reset min
even if it had been less.
-
I have just put my code for selection sort in the question above and hopefully you can give suggestions . Please check the code and let me know what can I improve.Plse help!– sk.76Jun 16, 2017 at 12:16
-
Keep track of the index of the smallest element, and swap two array elements, not variable
min
and array elementvalues[i]
.– BlauelfJun 16, 2017 at 13:30 -
I am not able to figure out how to keep the track of index of smallest element else than initialize a variable min and use it to swap because k will never have a value outside the loop. Plse help I am stuck in for long!– sk.76Jun 16, 2017 at 15:49
-
For example, when you assign to
min
, also assign the corresponding index (i
ork
) to another variableminIndex
. Then swapvalues[i]
andvalues[minIndex]
.– BlauelfJun 16, 2017 at 15:52 -
I did change that as in edited in the code above (plse check) but then I got error messages that I have also edited in the output section of the question . Any suggestion will be appreciated.– sk.76Jun 16, 2017 at 16:33