Search code examples
c++sortingbubble-sort

Sorting algorithm gives wrong results


#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
    int a, b, c[5], d;

    cout << "enter any five number\n";
    for(a=0; a<5; a++)
    {
        cin>>c[a];
    }
    for(a=0; a<5; a++)
    {
        for(b=++a; b<5; b++)
        {
            if(c[b] < c[a])
            {
                d = c[b];
                c[b] = c[a];
                c[a] = d;
            }
        }       
    }

    cout << "\nhere is the entered numbers in order\n"; 
    for(a=0; a<5; a++)
    {
        cout << c[a];
        cout << endl;
    }
    getch();
    return 3;
}

I am desk checking this program and I expect the program to sort numbers in ascending order but I am getting the wrong output help please.


Solution

  • in the inner loop it should be a + 1 not ++a