Sorting wont work?

so i wrote this code and it SHOULD work but for some reason when it runs it doesn't actually sort the numbers, it just moves them around, but iv looked at it and it looks like it should work.
any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <vector>
using namespace std;

void fill_array(vector<int> &numbers);
void sort1(vector<int> &numbers);
void swap_values(int& v1,int& v2);
int smallest1(vector<int> &numbers);
int const MAX=20;
using namespace std;

int main( )
    {
    cout << "This program sorts numbers from lowest to highest. \n";
	vector<int> numbers;
        fill_array(numbers);
        sort1(numbers);

       cout << "In sorted order the numbers are: \n";

	for (int i = 0;i<numbers.size();i++)
        {
            cout << numbers[i] << " ";
        }
    }
 void fill_array(vector<int> &numbers)
{
    int number,i;
   for (int i = 0;i<MAX;i++)
        {
         cin >> number;
         if (number > 0)
         {numbers.push_back(number);}
         else
            break;
        }
}

void sort1(vector<int> &numbers)
    {
    int i;
    int smallest;
    cout << numbers.size();
    for (i = 0;i<numbers.size();i++)
    {      smallest = smallest1(numbers);
           swap_values(numbers[i],numbers[smallest]);
	}
    }


void swap_values(int& v1,int& v2)
{
    int temp;
    temp = v1;
    v1 = v2;
    v2 = temp;
}

int smallest1(vector<int> &numbers)
    {
        int i,smallest;
        int minI = 1;
	for (i=0;  i < numbers.size();i++)
{
    if (numbers[i] < numbers[minI])
            {
               minI = i;
               smallest = minI;
            }
        else
            {continue;}
}
	return smallest;
    }
At present your function smallest1() returns the smallest number in the entire array. (with a minor bug, I guess you meant the initial value of minI to be 0).

The function smallest should return the smallest number in the array a particular index onwards.

That is because once you find the smallest number and place it in its proper position, now you want the next smallest number. You do not want the function to return the same smallest number again and again.

so just take the smallest1 function out of the loop?
oh wait i see, ok let me try something
Topic archived. No new replies allowed.