In: Computer Science
Given the root C++ code:
void sort()
{
const int N = 10;
int x[N];
for(int i = 0; i < N; i++)
{
x[i] = 1 + gRandom-> Rndm() *
10;
cout<<x[i]<<" ";
}
cout<<endl;
int t;
for(int i = 0; i < N; i++)
{
for(int j = i+1; j < N; j++)
{
if(x[j] < x[i])
{
int t =
x[i];
x[i] =
x[j];
x[j] = t;
}
}
}
cout << endl;
for(int i = 0; i < N; i++)
{
cout << x[i] <<"
";
}
cout << endl;
}
Given the order of numbers (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
Why does the program sort into (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
and not into (9, 8, 7, 6, 5, 4, 3, 2, 1, 10)?
Please explain in detail.
first given input
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
x[i],x[j]= 10 9
after if comparison list [9, 10, 8, 7, 6, 5, 4, 3, 2, 1]
x[i],x[j]= 9 8
after if [8, 10, 9, 7, 6, 5, 4, 3, 2, 1]
x[i],x[j]= 8 7
after if [7, 10, 9, 8, 6, 5, 4, 3, 2, 1]
x[i],x[j]= 7 6
after if [6, 10, 9, 8, 7, 5, 4, 3, 2, 1]
x[i],x[j]= 6 5
after if [5, 10, 9, 8, 7, 6, 4, 3, 2, 1]
x[i],x[j]= 5 4
after if [4, 10, 9, 8, 7, 6, 5, 3, 2, 1]
x[i],x[j]= 4 3
after if [3, 10, 9, 8, 7, 6, 5, 4, 2, 1]
x[i],x[j]= 3 2
after if [2, 10, 9, 8, 7, 6, 5, 4, 3, 1]
x[i],x[j]= 2 1
after if [1, 10, 9, 8, 7, 6, 5, 4, 3, 2]
after all the inner loop iteration
[1, 10, 9, 8, 7, 6, 5, 4, 3, 2]
x[i],x[j]= 10 9
after if [1, 9, 10, 8, 7, 6, 5, 4, 3, 2]
x[i],x[j]= 9 8
after if [1, 8, 10, 9, 7, 6, 5, 4, 3, 2]
x[i],x[j]= 8 7
after if [1, 7, 10, 9, 8, 6, 5, 4, 3, 2]
x[i],x[j]= 7 6
after if [1, 6, 10, 9, 8, 7, 5, 4, 3, 2]
x[i],x[j]= 6 5
after if [1, 5, 10, 9, 8, 7, 6, 4, 3, 2]
x[i],x[j]= 5 4
after if [1, 4, 10, 9, 8, 7, 6, 5, 3, 2]
x[i],x[j]= 4 3
after if [1, 3, 10, 9, 8, 7, 6, 5, 4, 2]
x[i],x[j]= 3 2
after if [1, 2, 10, 9, 8, 7, 6, 5, 4, 3]
after inner for loop
[1, 2, 10, 9, 8, 7, 6, 5, 4, 3]
x[i],x[j]= 10 9
after if [1, 2, 9, 10, 8, 7, 6, 5, 4, 3]
x[i],x[j]= 9 8
after if [1, 2, 8, 10, 9, 7, 6, 5, 4, 3]
x[i],x[j]= 8 7
after if [1, 2, 7, 10, 9, 8, 6, 5, 4, 3]
x[i],x[j]= 7 6
after if [1, 2, 6, 10, 9, 8, 7, 5, 4, 3]
x[i],x[j]= 6 5
after if [1, 2, 5, 10, 9, 8, 7, 6, 4, 3]
x[i],x[j]= 5 4
after if [1, 2, 4, 10, 9, 8, 7, 6, 5, 3]
x[i],x[j]= 4 3
after if [1, 2, 3, 10, 9, 8, 7, 6, 5, 4]
after inner for loop
[1, 2, 3, 10, 9, 8, 7, 6, 5, 4]
x[i],x[j]= 10 9
after if [1, 2, 3, 9, 10, 8, 7, 6, 5, 4]
x[i],x[j]= 9 8
after if [1, 2, 3, 8, 10, 9, 7, 6, 5, 4]
x[i],x[j]= 8 7
after if [1, 2, 3, 7, 10, 9, 8, 6, 5, 4]
x[i],x[j]= 7 6
after if [1, 2, 3, 6, 10, 9, 8, 7, 5, 4]
x[i],x[j]= 6 5
after if [1, 2, 3, 5, 10, 9, 8, 7, 6, 4]
x[i],x[j]= 5 4
after if [1, 2, 3, 4, 10, 9, 8, 7, 6, 5]
after inner for loop
[1, 2, 3, 4, 10, 9, 8, 7, 6, 5]
x[i],x[j]= 10 9
after if [1, 2, 3, 4, 9, 10, 8, 7, 6, 5]
x[i],x[j]= 9 8
after if [1, 2, 3, 4, 8, 10, 9, 7, 6, 5]
x[i],x[j]= 8 7
after if [1, 2, 3, 4, 7, 10, 9, 8, 6, 5]
x[i],x[j]= 7 6
after if [1, 2, 3, 4, 6, 10, 9, 8, 7, 5]
x[i],x[j]= 6 5
after if [1, 2, 3, 4, 5, 10, 9, 8, 7, 6]
after inner for loop
[1, 2, 3, 4, 5, 10, 9, 8, 7, 6]
x[i],x[j]= 10 9
after if [1, 2, 3, 4, 5, 9, 10, 8, 7, 6]
x[i],x[j]= 9 8
after if [1, 2, 3, 4, 5, 8, 10, 9, 7, 6]
x[i],x[j]= 8 7
after if [1, 2, 3, 4, 5, 7, 10, 9, 8, 6]
x[i],x[j]= 7 6
after if [1, 2, 3, 4, 5, 6, 10, 9, 8, 7]
after inner loop
[1, 2, 3, 4, 5, 6, 10, 9, 8, 7]
x[i],x[j]= 10 9
after if [1, 2, 3, 4, 5, 6, 9, 10, 8, 7]
x[i],x[j]= 9 8
after if [1, 2, 3, 4, 5, 6, 8, 10, 9, 7]
x[i],x[j]= 8 7
after if [1, 2, 3, 4, 5, 6, 7, 10, 9, 8]
after inner loop
[1, 2, 3, 4, 5, 6, 7, 10, 9, 8]
x[i],x[j]= 10 9
after if [1, 2, 3, 4, 5, 6, 7, 9, 10, 8]
x[i],x[j]= 9 8
after if [1, 2, 3, 4, 5, 6, 7, 8, 10, 9]
after inner loop
[1, 2, 3, 4, 5, 6, 7, 8, 10, 9]
x[i],x[j]= 10 9
after if [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
after loop
final output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
so,it sorts like the above ......but not (9, 8, 7, 6, 5, 4, 3, 2, 1, 10)