0

l changed my code but still cant figure out why it wont sort array...bubble sort only moves all elements one place to the right in my program instead of sorting array...l tired bsort and ssort and both do same thing shift elements for 1 position

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void bsort(int n,int a[])   
{
int i,j,k;
for(i=0;i<n-1;i++)  
{       
for(j=0;j<n-1;j++)
{   
        if(a[j]>a[j+1]);
            {

                k=a[j+1];
                a[j+1]=a[j];
                a[j]=k;

            }               
}   

} }

int main()
 {
int i,j,k,m,n;

srand(time(0));

printf("Unesi broj clanova niza:"); 
scanf("%d",&n);

int a[n];   

printf("Niz za sortiranje je:\n");
for(int i=0;i<n;i++) //Generisanje niza
{
    a[i]=rand()%(81);   
}

for(int i=0;i<n;i++) 
{
    printf("%3d",a[i]); 
}

bsort(n,a); 




 printf("\n\nSortirani niz je:\n");     
 for(i=0;i<n;i++)
 {
    printf("%3d",a[i]);

}

}

7
  • Can you print after sorting. Commented Mar 1, 2014 at 18:59
  • 1
    If you're using a language other than C then, please tag as such. I have tagged C as it appeared to me. Commented Mar 1, 2014 at 18:59
  • Do you see the problem with accessing a[j + 1] in a loop that runs to j=19 and an array only declared to be a[20] ? You're walking over your array bounds. You're also not correctly reducing your inner-loop end-point with each pass to avoid redundantly resorting already-sorted data. And though considered optional by some, Finally, bubbelsort should terminate sorting entirely on a no-swaps-appened inner-pass; its what give the algorithm O(N) on already sorted data, which you're also not doing. Commented Mar 1, 2014 at 19:02
  • intialising 19 elemets and sorting 20 elements????????? Commented Mar 1, 2014 at 19:02
  • 1
    Pretty certain that if (a[j] > a[j + 1]); { doesn't do what you hope/think/expect it to do.... Commented Mar 1, 2014 at 19:02

2 Answers 2

6

There are several problems with your bubble sort implementation.

First, this line:

if (a[j] > a[j + 1]); {

is incorrect. The semi-colon terminates the conditional. As a result, the following block executes on every iteration of the inner loop and you end up unconditionally swapping a[j] and a[j+1] for every value of j. This means you're performing a nonsensical rearrangement of the array.

Second, you're not dealing correctly with edge cases in the inner loop. When j == 19, you access a[j+1], which is a[20], which is beyond the end of the array. You thus import garbage data into your array.

Lastly, even after correcting the above, your implementation is needlessly inefficient, in that your inner loop goes through the entire array on each iteration of the outer loop, which it doesn't have to. Hint: Try to think about how the initialization or termination condition of the inner loop could depend on i.

Update (after the OP's rewrite): You only addressed the second issue.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 "You thus import garbage" - love that phrase. assuming the UB doesn't crash the process, that is exactly what will happen (and as a bonus, a piece of relevant data may well be "exported" =P
1
int main() {
    int a[20];
    srand(time(0));

    // array values initialization
    for (int i = 0; i < 19; i++) {
        a[i] = rand() % (81);
    }

    // array sorting
    bsort(a);

    // array printing
    for (int i = 0; i < 19; i++) {
        printf("%3d", a[i]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.