So to strengthen my learning in c++, I'm trying my hands on a few algorithms I found in a book to sort arrays. However, that's in pascal and I don't quite understand it, as they don't show the entire code.
This is what I've come up with, it works with some number, but not with all. Can somebody help me and say what is the problem with it?
Note: I don't want to use any other sorting algorithm, I would like to implement this specific type.
The principle of sort by choice is that, you select the smallest element in an array, put it in position 0, go on to the next smallest element, put it in position 1, etc.
You select position j1, which is the position of the smallest value a[1],...,a[n], an swap a[j1] with a[1]. Then we select the next smallest value j2 from a[2],...,a[n], and swap a[j2] with a[2] and so forth until all elements are in the correct position.
If you can read german, here is the book I found on the internet. The chapter is called "Sortieren durch Auswahl":
You are not resetting it, that is the problem.
Suppose that this is the input array: [ 7 2 5 4 3 1 5 ]
First iteration: min = 5
array = [1 2 5 4 3 7 5]
Second iteration: min = 1
array = [1 2 5 4 3 7 5]
Third: min = 1 // <-----
array = [1 2 5 4 3 7 5]
Glad you solved the problem. You have a memory leak though! This is important so I want to point it out even though you have closed the thread.
You need to delete the dynamically allocated memory. kbw pointed this out but I guess you didn't catch it.
Add after line 35: delete [] arr;