-
May 27th, 2013, 01:35 PM
#1
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
#2
Re: Bubble Sort-question
If an array is defined as
What are the first and last element index of the array as used by c/c++?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 27th, 2013, 02:22 PM
#3
Re: Bubble Sort-question
The first is a[0],the last a[9]...
-
May 27th, 2013, 02:31 PM
#4
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++) ??
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 27th, 2013, 02:25 PM
#5
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 27th, 2013, 03:09 PM
#6
Re: Bubble Sort-question
Because there is no a[10]!!!!
-
May 27th, 2013, 03:14 PM
#7
Re: Bubble Sort-question
Originally Posted by mathmari
Because there is no a[10]!!!!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 27th, 2013, 03:38 PM
#8
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|