Bubble sort help

here is output of random number:
83-42-46-80-69-75-22-83-63-70
41-65-49-68-63-46-55-61-95-5
89-27-57-35-11-25-11-52-22-6


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int sorting_array[size2];
    int swap = 0;
    for(int counter = size2-1; counter >= 0 ; counter --)
    {
        for (int index =0 ; index < 30; index ++)
        {
            if (sorting_array[index] < sorting_array[index + 1])
            {
                swap = sorting_array [index + 1];
                sorting_array [index + 1] = sorting_array [index] ;
                sorting_array[index] = swap;
            
            }
        }
    }
    
    for (int index = 0 ; index < size2; index ++)
    {
        cout << sorting_array[index] << ", ";
        
    }



2037409383, 1986684523, 1952918832, 1952671082, 1885501034, 
1869762607, 1718314087, 1718051190, 1701474162, 1684497017, 
1652126823, 1635151970, 892482093, 841822559, 828339299, 758134066, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 1373919184, 32767, 1110405476, 
1684826485, 0, 0, 0, 0, 1373919168, 32767, 1111244643, -201287232, 
1373919168, 32767, 232499934, 1, 0, 0, 1711890102, 1711890102, 100, 100, 
1373919184, 32767, 1373919600, 32767, 232499750, 1, 83, 42, 46, 80, 69, 75, 
22, 83, 63, 70, 41, 65, 49, 68, 63, 46, 55, 61, 95, 5, 89, 27, 57, 35, 11, 25, 11, 
52, 22, 6, 38, 86, 62, 96, 64, 69, 53, 93, 83, 63, 
Last edited on
what did i did wrong?
What is sorting_array? Is there more code? From this it just looks like nothing is in sorting_array, which obviously isn't the case if you are outputting something.
I am a bit confused on this line:
for(int counter = size2-1; counter >= 0 ; counter --) shouldn't you be looping while the previous one was sorted ( not finished sorting yet ).

Also for (int index =0 ; index < 30; index ++) whats with the magic number 30? Is that the size of the index? If so you want to loop till index - 1 otherwise you are going out of bounds.

I would suggest something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool sorting = true;
int temp;
while( sorting )
{
    sorting = false;
    for( int offset = 0; offset < size - 1; ++offset )
    {
         if( array[offset] < array[offset+1] )
        {
            temp = array[offset];
            array[offset] = array[offset+1];
            array[offset+1] = temp
            sorting = true;
         }
    }
}


*typo
Last edited on
i got it !!
Topic archived. No new replies allowed.