C Bubble Sort Help | Get Programming Assistance

  • Thread starter Thread starter James889
  • Start date Start date
  • Tags Tags
    Bubble Sort
AI Thread Summary
The discussion centers around troubleshooting a C program that implements the bubble sort algorithm to sort an array of random numbers. Key issues identified include the incorrect initialization of the array, where only the first 1000 elements are filled, and the improper call to the bubble sort function using an out-of-bounds index. Additionally, the bubble sort function lacks knowledge of the array's size, leading to flawed indexing and logic in the sorting process. The participants discuss the mechanics of the bubble sort algorithm, emphasizing the need to compare and swap adjacent elements correctly. Overall, the conversation highlights common pitfalls in array handling and sorting algorithms in C programming.
James889
Messages
190
Reaction score
1
Hi,

I am trying to write a program that sorts an array of random numbers.
Unfortunately, it does not work out as i had hoped, I am getting some strange output.


Here's my code:
Code:
#include <stdio.h>
#include <stdlib.h>


/* Function Prototypes */
int rand(void);
void srand(unsigned int seed);
void bubblesort(int array[]);

static unsigned long int next = 1;


int main() {

    int array[10000] = { 0 };
    int *ap,*fp;
    srand(23);


    
    for(ap = array; ap<&array[1000]; ap++) {      
        *ap = rand();
    }
        
        /*Sort it*/
        bubblesort(&array[10000]);

      /*Print it*/
      for (fp = array; fp<&array[10000]; fp++)
          printf("%d ",*fp);
              
              return 0;
}

void bubblesort(int array[]) {
    

    int ARRAY_LENGTH,temp;
    ARRAY_LENGTH = sizeof(array)/sizeof(int);
    
    for(int i=0; i<ARRAY_LENGTH; i++) {

            if(array[ARRAY_LENGTH] < array[ARRAY_LENGTH - i]) {
                    temp = array[ARRAY_LENGTH];
                    array[ARRAY_LENGTH] = array[ARRAY_LENGTH -i];
                    array[ARRAY_LENGTH -i] = temp;
            }

    }

}
    
int rand(void) {

next = next * 1103515245 + 12345;
return (unsigned int)(next/65536)%32768;
}

void srand(unsigned int seed) {
        
        next = seed;
}
 
Physics news on Phys.org
Hi James! :smile:

I'll start from the top.

Your for-loop to fill the array fills only the first 1000 elements, so 9000 elements will be left uninitialized. In this case they will be zero though.

In your call to bubblesort you index the array with 10000 and take the address of that element.
But that is out of range.
So you'd not be sorting the actual array.
You should specify simply "array" as parameter instead of "&array[10000]".

In your bubblesort function you have specified int array[] as parameter.
Here you have a common pitfall.
In itself this is correct, but it gives no clue to how big the array is.
You should pass the size of the array as a separate parameter.

In your bubblesort function you try to calculate the ARRAY_LENGTH with sizeof(array)/sizeof(int).
In itself this is a proper way to find the number of elements in the array, but not here.
As I mentioned before, when you pass an array to a function, you lose the information about how big it is.
Typically you should do this calculation before calling the function, and pass this size as a parameter.

In your bubblesort function you index the array with ARRAY_LENGTH, but this element is not part of the array (if it would have been properly calculated). It is one beyond the last.
So your algorithm for bubblesort is flawed.
Where did you get it?
The if-statement as you have it won't work.

Cheers! :smile:
 
Hello Serena!

Thanks for the input.

I've made the said above corrections.
Why won't the if statement work?
 
Before I explain further, can you show your corrections please?
 
Code:
#include <stdio.h>
#include <stdlib.h>/* Function Prototypes */
int rand(void);
void srand(unsigned int seed);
void bubblesort(int array[],int arraysize[/color]);

static unsigned long int next = 1;int main() {

    int array[10000] = { 0 };
    int *ap,*fp;
    srand(23);    
    for(ap = array; ap<&array[10000[/color]]; ap++) {      
        *ap = rand();
    }
        
        /*Sort it*/
        bubblesort(array,sizeof(array)/sizeof(int));[/color]

      /*Print it*/
      for (fp = array; fp<&array[10000]; fp++)
          printf("%d ",*fp);
              
              return 0;
}

void bubblesort(int array[],int arraysize[/color]) {
    
                      .
                      .
                      .   

    }
 
Last edited:
What coding scheme is this?
 
James889 said:
Hello Serena!

Thanks for the input.

I've made the said above corrections.
Why won't the if statement work?

Your corrections look good! :smile:

What do you know about the bubblesort algorithm and how it works?
Why is it called "bubble" sort for that matter?
 
It steps through the whole list and 'pushes' the smaller elements towards the beginning. Ie swaps a and b, if a > b.
 
Yes, it steps through the list, and (usually) compares 2 consecutive elements, and swaps them if applicable.
I think it also works if you compare each element in the list with the last one and swap them if applicable.
As yet, you're not indexing the last element, but the one beyond it.

If you run through the list like that once, will it be sorted when you've done?
 

Similar threads

Replies
7
Views
2K
Replies
4
Views
1K
Replies
4
Views
2K
Replies
7
Views
3K
Replies
1
Views
9K
Replies
1
Views
10K
Back
Top