Life Hacks


      

Write a program of the Bubble Sort.



#include<iostream.h>

#include<conio.h>

void main( ) /* main start */

{

clrscr( );

int num[56], n, k, c, ptr, temp; /* local variable */

cout<<"enter the number of elements in an array is : ”;

cin>>n; // inserting no. of elements in the array.

cout<<"enter the value of elements of array is : ";

for(c = 1; c <= n; c++) // inserting elements in the array.

{

cin>>num[c];

} /* End of for */

for(k = 1; k <= n – 1; k++) // Loop for sorting the elements of an array.

{

ptr = 1;

while(ptr <= n – k)

{

if(num[ptr] > num[ptr + 1])

{

temp = num[ptr];

num[ptr] = num[ptr + 1];

num[ptr + 1] = temp;

}

ptr = ptr + 1;

} /* End of while loop */

} /* End of for loop*/

cout<<"\n Sorted array is:";

for(c = 1; c <= n; c++)

{

cout <<" \n " <<num[c];

} /* End of for loop*/

getch( );

}

Output:-

clip_image002

Leave a Reply