Hello,

I found the code for bubble sort in C++.
With the code below th e output will be:
23
8
7
6
6
5
2
2
1

Is it possible that sorts the numbers so the output will show all changes. In example:

9,6,5,23,2,6,2,7,1,8
9,6,23,5,2,6,2,7,1,8
....
....
....
23,9,8,7,6,6,5,2,2,1


Thanking in advance

#include <stdio.h>
#include <iostream.h>

void bubbleSort(int *array,int length)//Bubble sort function 
{
    int i,j;
    for(i=0;i<10;i++)
    {
        for(j=0;j<i;j++)
        {
            if(array[i]>array[j])
            {
                int temp=array[i]; //swap 
                array[i]=array[j];
                array[j]=temp;
            }

        }

    }

}

void printElements(int *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<10;i++)
        cout<<array[i]<<endl;
}


void main()
{

    int a[]={9,6,5,23,2,6,2,7,1,8};   // array to sort 
    bubbleSort(a,10);                 //call to bubble sort  
    printElements(a,10);               // print elements
}

Print out the array each time you swap.

Thanks a lot! It was so easy :P :P

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.