# Bubble Sort-question

• May 27th, 2013, 01:35 PM
mathmari
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++)???
• May 27th, 2013, 02:18 PM
2kaud
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++?
• May 27th, 2013, 02:22 PM
mathmari
Re: Bubble Sort-question
The first is a[0],the last a[9]...
• May 27th, 2013, 02:25 PM
2kaud
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.
• May 27th, 2013, 02:31 PM
2kaud
Re: Bubble Sort-question
Quote:

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++) ??
• May 27th, 2013, 03:09 PM
mathmari
Re: Bubble Sort-question
Because there is no a[10]!!!!
• May 27th, 2013, 03:14 PM
2kaud
Re: Bubble Sort-question
Quote:

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

:thumb:
• May 27th, 2013, 03:38 PM
mathmari
Re: Bubble Sort-question
:)