Bubble Sort-question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Bubble Sort-question

  1. #1
    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. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,704

    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++?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2013
    Posts
    45

    Re: Bubble Sort-question

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

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,704

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,704

    Re: Bubble Sort-question

    Quote Originally Posted by mathmari View Post
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Apr 2013
    Posts
    45

    Re: Bubble Sort-question

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

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,704

    Re: Bubble Sort-question

    Quote Originally Posted by mathmari View Post
    Because there is no a[10]!!!!
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center