i did a simple bubble sort but it is ot working . help me out.

#include<stdio.h>
main()
{
  int i,j,k;
  int a[]={7,9,4,2,3,6};
  for(i=0;i<7;i++)
    for(j=i+1;j<7;j++)
      if(a[j]<a[j-1])
      {
	k=a[j];
	a[j]=a[j-1];
	a[j-1]=k;
      }
     for(i=0;i<7;i++)
       printf("a[%d]==>%d",i,a[i]);
}

Recommended Answers

By just going through your code I see there could a problem when i is 6.Please check.

i did a simple bubble sort but it is ot working . help me out.

#include<stdio.h>
main()
{
  int i,j,k;
  int a[]={7,9,4,2,3,6};
  for(i=0;i<7;i++)
    for(j=i+1;j<7;j++)
      if(a[j]<a[j-1])
      {
	k=a[j];
	a[j]=a[j-1];
	a[j-1]=k;
      }
     for(i=0;i<7;i++) …
Jump to Post

All 3 Replies

By just going through your code I see there could a problem when i is 6.Please check.

i did a simple bubble sort but it is ot working . help me out.

#include<stdio.h>
main()
{
  int i,j,k;
  int a[]={7,9,4,2,3,6};
  for(i=0;i<7;i++)
    for(j=i+1;j<7;j++)
      if(a[j]<a[j-1])
      {
	k=a[j];
	a[j]=a[j-1];
	a[j-1]=k;
      }
     for(i=0;i<7;i++)
       printf("a[%d]==>%d",i,a[i]);
}

try considering 2nd loop as

for(j=1;j<=N-i-1;j++)

and swap a[j]=a[j+1]
also try it with pointers as well.

Be a part of the DaniWeb community

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