CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Bubble Sort-question

1. Member
Join Date
Apr 2013
Posts
45

## Bubble Sort-question

Hey!!!I have a question!!!!Why at the following code,which is an implematation of the bubble sort :
#include <stdio.h>
int main()
{
int a[10]={34,2,56,78,34,5,76,10,47,29};
int n=10;
int i,j,temp;
for(i=0;i<10;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
we use this command for(i=0;i<10;i++) and not this for(i=0;i<=10;i++)???

2. ## Re: Bubble Sort-question

If an array is defined as

Code:
`int a[10];`
What are the first and last element index of the array as used by c/c++?

3. Member
Join Date
Apr 2013
Posts
45

## Re: Bubble Sort-question

The first is a[0],the last a[9]...

4. ## Re: Bubble Sort-question

There is also an improvment you can make to your bubble-sort that will give a small performance improvement when the data is in near-sort order.

5. ## Re: Bubble Sort-question

Originally Posted by mathmari
The first is a[0],the last a[9]...
So why for(i=0;i<10;i++) then and not for(i=0;i<=10;i++) ??

6. Member
Join Date
Apr 2013
Posts
45

## Re: Bubble Sort-question

Because there is no a[10]!!!!

7. ## Re: Bubble Sort-question

Originally Posted by mathmari
Because there is no a[10]!!!!

8. Member
Join Date
Apr 2013
Posts
45

## Re: Bubble Sort-question

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.