How to arrange array in ascending order?

Can you please help me to find out the problem in my code for arranging array in ascending number?

#include<iostream>
#include<stdlib.h>

using namespace std;
int main() {
int const size = 100;
int user[size];
int x;
int lan = user[0];

cout << "Enter the number you like";
cin >> x;

cout << "Enter the marks";
for (int i = 0; i < x; i++) {
cin >> user[i];
}

for (int i = 0; i < x; i++) {

for (int j =1; j < x; j++) {
if (user[i] > user[j]) {
user[i] = lan;
user[i] = user[j];
user[j] = lan;
}

}

}
for (int i = 0; i < x; i++) {
cout<< user[i];
}


return 0;
}
@Tenma: how many can you see? hopefully only one :)

you mixed i and j in your swapping code...

first you need some code to scan the array swapping 2 values if necessary.
1
2
3
4
5
6
7
8
9
for (int j =1; j < x; j++) 
{
   if (user[j-1] > user[j]) 
   { 
      lan = user[j];
      user[j] = user[j-1];
      user[j-1] = lan;
   }
}


you need to keep doing that until there's nothing left to swap. convention (and the book you read) say use another for loop, but what if the sort finishes early, you will still go round that loop scanning but not swapping. So better to just do it until nothing needs swapping.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool swapped = false;
do
{
   swapped = false; // reset before each scan

   for (int j =1; j < x; j++) // start at 1 and compare with previous
   {
      if (user[j-1] > user[j])  // move bigger values along, use "<" for descending
      { 
         lan = user[j];
         user[j] = user[j-1];
         user[j-1] = lan;
         swapped = true;
      }
   }
} while (swapped);


You can also speed the sort up further by noting that at the end of each pass the final element will be in its final position. You don't need to do so many checks next time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
   bool swapped = false;
   int xx = x;      // <=== added a variable for the end of the scan
   do
   {
      swapped = false; 

      for (int j = 1; j < xx; j++) //  <=== note xx not x 
      {
         if (user[j-1] > user[j])  
         { 
            lan = user[j];
            user[j] = user[j-1];
            user[j-1] = lan;
            swapped = true;
         }
      }
   xx--;                           // <=== end element finalised in position, so don't need to go as far next time
   } while ( swapped );
closed account (3pX8b7Xj)

try this one i just modified your code a little bit

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

#include<iostream>
#include<stdlib.h>

using namespace std;
int main() {
int const size = 100;
int user[size];
int x;
int lan; //

cout << "Enter the number you like";
cin >> x;

cout << "Enter the marks";
for (int i = 0; i < x; i++) {
cin >> user[i];
}

for (int i = 0; i < x; i++) {

for (int j =i+1; j < x; j++) {
if (user[i] > user[j]) {
lan=user[i];
user[i]=user[j];
user[j]=lan;
}

}

}
for (int i = 0; i < x; i++) {
cout<< user[i];
}


return 0;
}
Last edited on
Different strategy to bubble sort, but seems to work as far as sorting is concerned.

(Maybe you could tweak the non-sorting bits - inputting and outputting marks - so that there are spaces, line feeds and, where necessary, prompts for the user. Also, indent your code to show structure.)
When swapping the 2 elements in an array, it can be done without using a temporary variable. Only works if the objects can be subtracted/added. Like, int.

1
2
3
element1 += element2;
element2 = element1 - element2;
element1 -= element2;
Last edited on
The advantage of Jaybob66's approach is that, if the array is already fully sorted (or nearly sorted) then it will stop after a single outer pass (or only a few passes). I would go with that strategy.
Topic archived. No new replies allowed.