0

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
}
2
Contributors
2
Replies
4
Views
10 Years
Discussion Span
Last Post by matk
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.