Hey I dont know what is wrong with my code. It can run but doesnt do what I want it to do, which is sorting values of array from small to large number. It just output random order which is weird.
#include <iostream>
usingnamespace std;
int main() {
constint N = 6 ; // number of elements in the array
double Feld[N];
std::cout << "Enter " << N << " numbers: ";
for( int i = 0; i < N; ++i ) cin >> Feld[i];
/*Sorting*/
// algorithm: http://www.cs.armstrong.edu/liang/animation/web/SelectionSort.html
// in each pass through this loop, we bring the smallest element in
// the sub-array [i,N) to position i
for( int i = 0; i < N; ++i ) {
// invariant: items before position i are already sorted
// for each unsorted sub-array starting at position i
// locate the position of the smallest number in the sub-array starting at position i
int pos_smallest = i ;
// we look at each number after the number at position i
for( int j = i+1; j < N ; ++j )
if( Feld[j] < Feld[pos_smallest] ) pos_smallest = j ;
// and bring the smallest number to position i (swap)
constdouble smallest_number = Feld[pos_smallest] ;
Feld[pos_smallest] = Feld[i] ;
Feld[i] = smallest_number ;
}
// print out the sorted array
for( int i = 0; i < N; ++i ) cout << Feld[i] << ' ' ;
cout << '\n' ;
}