Hello,
I am trying to make a program which will sort a array of 10 elements using bubble sort. I searce dthe forum and google but douldnt find a problem similar to mine. I cam up with the following code:

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

void swap(int *, int *);

int main()
{
    int i, j, a[10];
    for(i=0; i<10; i++)
    {
        printf("Enter Value %d: ", i);
        scanf("%d", (a+i));
    }
    for(i=0; i<10; i++)
    {
        for(j=0;j<=9-i; j++)
        {
            if(a[j]>a[j+1])
            {
                swap(&a[j], &a[j+1]);
            }
        }
    }
    for(i=0; i<10; i++)
    {
        printf("%d ", *(a + i));
    }
}

void swap(int *val1, int *val2)
{
    int temp;
    temp = *val1;
    *val1 = *val2;
    *val2 = temp;
}

However it was displaying the following output:
ATTACHED FILE\

Can anyone help me adress my problem

Recommended Answers

All 3 Replies

Hello,
I am trying to make a program which will sort a array of 10 elements using bubble sort. I searce dthe forum and google but douldnt find a problem similar to mine.

You're kidding, right? No one site that explained how the Bubble Sort works sorted numbers? I think you need to search again. Try searching for "Bubble Sort". The first 5 hits show exactly your stated problem.

Hi Shikhin,

Though it should not be a problem in searching for a bubble sort algorithm, but I am happy you tried it by yourself.
Change the for loop as:

for(i=0; i<10; i++)
{
    for(j=0;j<=9-i; j++)
    {
        if(a[j-1]>a[j])
        {
            swap(&a[j], &a[j-1]);
        }
    }
}

I think your code will work fine...

Thanks!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.