#include<iostream>
using namespace std;
int main()
{
    int n;int*p;
    int i=0,j=0,temp=0;
    cin>>n;
    p=new int[n];
    for(int i=0;i<n;i++)
    {
            cin>>p[i];
            }
            for(j=0;j<n;j++)
            {
                    for (i=j;i<n;i++)
                    {
                        if(p[i]>p[i+1])
                        {
                                       temp=p[i];
                                       p[i]=p[i+1];
                                       p[i+1]=temp;
                                       }
                                       }
                                       }
                                       for(i=0;i<n;i++)
                                       cout<<p[i]<<endl;
                                       return 0;
                                       }

Recommended Answers

All 3 Replies

Please help us to help you. What input do you give it, what output do you get,and how is that different from what you want? Is this intended to be a bubble sort implementation?

yes it is sorting implementation...program hangs on when input size grows longer..program does not gives correct output..i am not getting any logical or syntax error in my program ..if you people can figure out then please let me know.

Your program is using a bad algorithm. It just doesn't work. You're doing two things wrong. Firstly, when you go through the array, do not check the LAST element in the array. You compare each element with the next element, so what are you comparing the LAST element with? The last element does not have a next element.

The second mistake is that you should run over the array as many times as it takes until you don't make any changes in it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.