Minor sort problem

I'm trying to make my program sort this array of numbers from least to greatest but the output is coming out otherwise.

Output:
9 12 3 14

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main() {
    int a[4] = {12, 9, 14, 3};
    int temp;
    
    for(int i = 0; i < 4; i++) {
        if(a[i] > a[i + 1]) {
            temp = a[i];
            a[i] = a[i + 1];
            a[i + 1] = temp;
        }
        cout << a[i] << " ";
    }
    return 0;
}
You are going through the array once only. Let's watch what happens:

Array at start: 12 9 14 3

i = 0
Compare a[0] and a[1], swap them round.
Array is now: 9 12 14 3

i = 1
Compare a[1] and a[2], they're in the right order, no swap.
Array is now: 9 12 14 3

i = 2
Compare a[2] and a[3], swap them round.
Array is now: 9 12 3 14

i = 3
Compare a[3] and a[4], except a[4] doesn't exist so you're comparing with some random value off the end of your array. This is a very bad idea. Don't do this.
Array is now: 9 12 3 14


So that's what your code does. Your sort algorithm simply doesn't work. You've tried to implement a bubble sort, but you've missed part of it. You need to keep doing this, over and over (except DON'T try to use a[4]) until no swaps happen. You need to keep repeating the for loop until no changes happen.
Last edited on
I tried i < 4 - 1 (line 8) but that doesn't work either. Kind of stuck as to what to write. Your explanation is very clear, though. Thanks a lot for that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  bool at_least_one_swap_happened = true;
  while (at_least_one_swap_happened)
    {
      at_least_one_swap_happened = false;
      for(int i = 0; i < 3; i++)
       {
	if(a[i] > a[i + 1]) 
        {
	  at_least_one_swap_happened = true;
	  temp = a[i];
	  a[i] = a[i + 1];
	  a[i + 1] = temp;
	}
      }
    }
Last edited on
I understand what you said initially. I just want to know what I need to change in my for loop in order for this thing to work once and for all because I know I have the right idea I'm just missing something really minuscule .
I just want to know what I need to change in my for loop


Nothing. Your for loop is fine (apart from the a[4] issue). You need to execute your for loop many times until it stops making a difference.
Forgive me please but I'm just really lost. I can't wrap my head around how fix the a[4] issue.
Don't ever try to use a[4]. That's it. That's all there is to it.

a[0] - OK
a[1] - OK
a[2] - OK
a[3] - OK
a[4] - Not OK

Let's look at that again, but instead of using numbers directly, we'll use "i".

Let's imagine i=0;
So a[i] = a[0] - OK.

Let's imagine i=1;
So a[i] = a[1] - OK.

Let's imagine i=2;
So a[i] = a[2] - OK.

Let's imagine i=3;
So a[i] = a[3] - OK.

Let's imagine i=4;
So a[i] = a[4] - Not OK.


Now let's look at it again, but this time we'll be accessing a[i+1]

Let's imagine i=0;
So a[i+1] = a[1] - OK.

Let's imagine i=1;
So a[i+1] = a[2] - OK.

Let's imagine i=2;
So a[i+1] = a[3] - OK.

Let's imagine i=3;
So a[i+1] = a[4] - Not OK.

Let's imagine i=4;
So a[i+1] = a[5] - Really Not OK.


So there we have it. To fix the a[4] issue, you need to never ever try to use a[4].

So if you're using a[i], then you need to never ever let i=4
If you're using a[i+1], then you need to never ever let i=3
Topic archived. No new replies allowed.