Vaibhav Bansal
Vaibhav Bansal

Reputation: 37

bubble sort not working with dynamic memory allocation in C

I was making a program using dynamic memory allocation that can rearrange the elements of the array. But it is giving the wrong results.

Here is my Code :

#include<stdio.h>
#include<stdlib.h>
int bubble_sort(int n,int *ar)
 {
    int i=0,j=0,temp;
    for(i; i<n;i++)
    {
        for(j; j<n-i-1;j++)
        {
            if(*(ar + j)>*(ar + j + 1))
            {
                temp = *(ar + j);
                 *(ar + j) = *(ar + j + 1);
                *(ar + j + 1) = temp;
            }
        }
    }

}
 main()
{
    int n,i;
    printf("Enter the size of the array : ");
   scanf("%d",&n);
    int *arr1 = (int *)calloc(n,sizeof(int));
    printf("Enter the elements of first array : \n");
     for(i=0;i<n;i++)
    {
        scanf("%d",(arr1+i));
        printf("\n");
    }
    bubble_sort(n,arr1);
    printf("After sorting, array is : \n");
    for(i=0;i<n;i++)
    { 
        printf("%d\t",*(arr1 + i));
    }
}

The output it gives is just a random set of numbers that I enter as elements of the array.

For example, if I enter the number of elements of array as 6 and then the elements as

7, 8, 9, 5, 6, 4

then the output is

7, 8, 5, 6, 4, 9

Upvotes: 0

Views: 1916

Answers (3)

sanjeev
sanjeev

Reputation: 600

Modified the program and it works as expected

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

void FunSwap(int *a, int *b)
{
int temp = *a;
*a= *b;
*b= temp;
}

void BubbleSort(int arr[], int n)
{
for(int i=0;i<n-1;i++)
{
    for(int j =0;j<n-i-1;j++)
    {
        if(arr[j]>arr[j+1])
            FunSwap(&arr[j], &arr[j+1]);
    }
 }
}

 void display(int arr[], int n)
{
 for(int i=0;i<n;i++)
    printf("%d ,", arr[i]);
 printf("\n");
}

int main()
{
int n=0;
printf("\n enter the size of Array :-");
scanf("%d", &n);
int *arr = (int*)malloc(n*sizeof(int));
//int *arr = (int*)calloc(n,sizeof(int)); 
//either calloc or malloc can be used for dynamic memory allocation
printf("\n enter the elements of Array :-");
for(int i=0;i<n;i++)
{
    scanf("%d", &arr[i]);
}
printf("Before sorting, array is : \n");
display(arr,n);
BubbleSort(arr,n);
printf("After sorting, array is : \n");
display(arr,n);

}

Upvotes: 0

user2736738
user2736738

Reputation: 30936

for(j; j<n-i-1;j++) will be for(j=0; j<n-i-1;j++) this j=0 is needed as you will start comparing from beginning again.

Also the for loop of i will be for(i; i<n-1;i++).(This is done because the i=n-1 will never make the inner loop run- it's redundant to iterate one more time)(You have initialized i with 0 outside the block so i=0 not needed and can be skipped) This is the right way to do and this gives you the desired output.

The initialization of i should be moved to for loop as pointed by Tom Karzes, it is much cleaner design and good practice. Also think of it, when you declare local variables out of the scope where you use it - it is much harder to control and keep track of. Even you declare it outside the for block assign the value 0 inside the for loop also.for(i =0;...) or for(int i=0; ...) is a good way to go.

There are couple of things *(arr+i) is same as arr[i]. Don't make things harder to read than it should be. calloc's return value should be checked and the memory allocated should be free when you are done working with it.

Upvotes: 0

Stephen Docy
Stephen Docy

Reputation: 4788

you need to start j at 0 each time through the outer loop, change

for(j; j<n-i-1;j++)

to

for(j=0; j<n-i-1; j++)

In general it is best to initialize the counter variable within the for loop to avoid confusion about its range and avoid bugs like this. So even

int i=0;
for(i; i<n;i++)

would be better written as

int i;
for(i=0; i<n;i++)

or even better (for modern C compilers)

for (int i = 0; i < n; i++)

Upvotes: 1

Related Questions