C Bubble Sort Help | Get Programming Assistance

In summary: No, it will not be completely sorted after just one run through the list. The algorithm needs to continue running through the list until there are no more elements that need to be swapped. This is why it is called "bubble" sort - because smaller elements "bubble" to the top of the list.
  • #1
James889
192
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
  • #2
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:
 
  • #3
Hello Serena!

Thanks for the input.

I've made the said above corrections.
Why won't the if statement work?
 
  • #4
Before I explain further, can you show your corrections please?
 
  • #5
Code:
#include <stdio.h>
#include <stdlib.h>/* Function Prototypes */
int rand(void);
void srand(unsigned int seed);
void bubblesort(int array[],[color=red]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[[color=red]10000[/color]]; ap++) {      
        *ap = rand();
    }
        
        /*Sort it*/
        [color=red]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[],[color=red]int arraysize[/color]) {
    
                      .
                      .
                      .   

    }
 
Last edited:
  • #6
What coding scheme is this?
 
  • #7
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?
 
  • #8
It steps through the whole list and 'pushes' the smaller elements towards the beginning. Ie swaps a and b, if a > b.
 
  • #9
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?
 

1. What is C Bubble Sort?

C Bubble Sort is a sorting algorithm used to arrange elements in a specific order in an array. It works by comparing adjacent elements and swapping them if they are in the wrong order, repeatedly until the entire array is sorted.

2. How does C Bubble Sort work?

C Bubble Sort works by comparing adjacent elements in an array and swapping them if they are in the wrong order. This process is repeated until the entire array is sorted in the desired order.

3. What are the advantages of using C Bubble Sort?

The main advantage of using C Bubble Sort is its simplicity and ease of implementation. It also has a space complexity of O(1) and a time complexity of O(n^2), making it efficient for small datasets.

4. What are the limitations of C Bubble Sort?

C Bubble Sort has a time complexity of O(n^2) which means it can be slow for large datasets. It also has a worst-case scenario where the array is already sorted in reverse order, making it less efficient.

5. How can I get programming assistance for C Bubble Sort?

You can get programming assistance for C Bubble Sort by reaching out to online programming communities, hiring a tutor, or seeking help from a computer science professional. You can also find tutorials and resources online to help you understand and implement C Bubble Sort.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
924
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
Back
Top