Vector function not working with selectionSort

I learned a little with starting c++ this semester, so I thought is it possible to combine vector array and the selectionSort function,so I built the program with vector arrays first through user input, asking for size and then asking user to input values. It worked fine. So I thought try and use the function selectionSort bypassing the for loops manually. It won't sort the data input so maybe I should just build or write the selection sort from scratch.

I would like to know why it won't sort.

Any brutal comments are welcome as well.. thanks .. when I added the sortedData and selectionSort it did not sort but compiled though Dev c++comp used


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
#include<iostream>
#include<vector>

using namespace std;
void showData(vector<int>);
void selectionSort(vector<int>, int);
void sortedShowData(vector<int>, int);
int main ()
{
vector<int> values;
int Size;


cout<<"input the size of the array:";
cin>>Size;
for (int counter=0;counter<Size;counter++)
  {
  int no;
  cout<<"Value ["<<(counter+1)<<"]:";
  cin>>no;
  values.push_back(no);
 
  }
  //showData(values); 
  selectionSort(values,Size);
  cout<<endl;
  cout<<"sorted"<<endl;
  sortedShowData(values,Size);
  system("pause");

return 0;
}

void showData(vector<int> vect)
{
     for(int count=0; count<vect.size();count++)
     cout<<vect[count]<<endl;
}
void selectionSort(vector<int> a,int)
{
     int startScan, minIndex, minValue;
    int s=a.size();
     for (int startScan=0; startScan<s-1;startScan++)
    {
     minIndex =startScan;
     minValue = a[startScan];
     for (int index =startScan+1; index<s;index++)
     {
         if (a[index]<minValue)
            {
             minValue =a[index];
             minIndex=index;
             }
      }
             a[minIndex]=a[startScan];
             a[startScan]=minValue;
    }        

}

void sortedShowData(vector<int>mect, int size)
{    size=mect.size();
     for (int c=0;c<size; c++)
     cout<<mect[c]<<endl;
     
}
Last edited on
The vector is passed by value to the function. That means that inside the function a is a copy of the vector passed in so any changes to a will only affect the copy.

If you want the function to change the vector you give to it you can pass the vector by reference.
void selectionSort(vector<int>& a,int)

It is generally a good idea to always pass vectors by reference to avoid expensive copying. If the function doesn't change the vector you can pass a const vector.
void showData(const vector<int>& vect)
Last edited on
Thank Peter87, that explains, the address and pointer differences when used.
I overlooked the syntax for sortSelection( func) that it had this in the syntax..
hm.. what would you suggest in learning how to be more proficient in loops as used
in sorting and swapping just on a general basis or wide applications..

The code below was what I initially started before I did the vector version still I think
I need to build more codes from scratch I only learned basic, pascal when I was 19..

C++, I encountered only now that I'm 32.. lol thanks though..

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
#include <iostream>
using namespace std;
void selectionSort(int[],int);
void showArray( const int[], int);

int main()
{ const int SIZE= 5;
int numero[SIZE]={5,2,4,1,3};

cout<<"Unsorted..\n";
showArray(numero, SIZE);
selectionSort(numero,SIZE);
cout<<"Sorted..\n";
showArray(numero,SIZE);
system("pause");
return 0;
}
void selectionSort(int *array, int size)
{
int startScan, minIndex,minValue;
for(startScan=0;startScan<(size-1);startScan++)
{ 
	minIndex=startScan;
	minValue=*(array+startScan);
	for(int index =startScan+1;index<size;index++)
	{
            if (*(array+index)<minValue)
            {
               minValue=*(array+index);
               minIndex=index;
               }
     }
                *(array+minIndex)=*(array+startScan);
             	*(array+startScan)=minValue;
     }
}

void showArray(const int *array, int size)
	{
		for(int count=0; count<size; count++)
			cout<<*(array+count);
			cout<<endl;
	}
I meant to say thank you.. have to sleep 4 in the morn' already.. thanks again
Peter87
Topic archived. No new replies allowed.