vector and a sorting loop

Hi, I'm having a bit of confusion as to how vector's list work when a sorting loop aplies to it.
I could be using turms wrong since I'm learning this in another language.
The question is in the code comments. Thanks!

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
int main ()
{
   int list[10];
   int i;
   for( i=0;i<10;i++)       
  {  
// so this loop seems to create the vektor list                     
// where i is the position of input number in the vektor list 
	  
           cout<<"Put in a number nr."<<i+1<<"  ";
	   cin>>list[i];
   }
       Sorting(list,10);   // call to funktion
       cout<<endl;	   
       cout<<"Here are the numbers in growing order: "<<endl;
       for( i=0;i<10;i++)
             cout<<list[i]<<" ";
      cout<<endl;  
      getch();
 return 0;
}
void Sorting(int vekt[], int n)
{
  //  the sorting loops.
// and here is the thing i don't understand:
// Let's say 30 is given place list[1], and 20 is given place list[2]. When the loop is aplied , does it change place of 30 and 20 or of [1] and [2]?

 int  sort;
   for (int i=0;i<n;i++)  
         for (int j=(n-1);j>i;j--) 
             if (vekt[j-1]>vekt[j])
              {
                 sort=vekt[j-1];
                 vekt[j-1]=vekt[j];
                 vekt[j]=sort;
               }
          
} 

Hi, I'm having a bit of confusion as to how vector's list work when a sorting loop aplies to it.
int list[10];
This is an array (named list) of 10 unnamed int variables.

std::vector is a different beast.

Let's say 30 is given place list[1], and 20 is given place list[2]. When the loop is aplied , does it change place of 30 and 20 or of [1] and [2]?


sort is assigned the value held by the variable @ list[1]. list[1] is assigned the value of the variable at list[2]. list[2] is assigned the value of the variable sort.

In essence, the values of the variables are exchanged or swapped (which is why the exchange is often done via a function named swap.)
oh, I see, that's what got mixed up in languages, that it's an array in the end. And there is a great detailed explanation under Tutorials - Arrays about how integer values inside arrays can be manipulated.
Topic archived. No new replies allowed.