0% found this document useful (0 votes)
36 views

Bubble Sort

This C++ program uses bubble sort to sort an array of integers in ascending order. It prompts the user to enter the size of the array and its elements. It then prints the original unsorted array. The bubble_sort function is called to sort the array using bubble sort. It iterates through the array, swapping adjacent elements if they are out of order. After each iteration, it prints the current state of the array. Finally, it prints the sorted array.

Uploaded by

Sahaj Mitra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Bubble Sort

This C++ program uses bubble sort to sort an array of integers in ascending order. It prompts the user to enter the size of the array and its elements. It then prints the original unsorted array. The bubble_sort function is called to sort the array using bubble sort. It iterates through the array, swapping adjacent elements if they are out of order. After each iteration, it prints the current state of the array. Finally, it prints the sorted array.

Uploaded by

Sahaj Mitra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

/* Program to sort the given array in ascending order using bubble

sort method */
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int A[80],n;
cout<<"Enter desired size of array (<80): "; cin>>n;
cout<<"\n\nEnter the array : \n";
for(int i=0; i<n; i++)
cin>>A[i];
cout<<"\n\nArray of elements is as shown below : \n\n";
for(i=0; i<n; i++)
cout<<A[i]<<" ";
cout<<"\n\nNow Elements will be arranged in ascending order using bubble sort
:\n\n";
void bubble_sort(int A[],int n);
bubble_sort(A,n);
getch();
}
void bubble_sort (int A[], int n)
{ int temp; int count=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n-1; j++)
{ if(A[j+1]<A[j])
{ count++;
temp=A[j+1];
A[j+1]=A[j];
A[j]=temp;
cout<<"\n\nArray for iteration "<<count<<" is : \n\n";
for(int k=0; k<n; k++)
cout<<A[k]<<" ";
}
}
}
}

Output:
Enter desired size of array (<80): 5
Enter the array :
9
7
1
3
4
Array for iteration 1 is :
79134
Array for iteration 2 is :
71934
Array for iteration 3 is :
71394
Array for iteration 4 is :
71349
Array for iteration 5 is :
17349
Array for iteration 6 is :
13749
Array for iteration 7 is :
13479
Sorted array is :
13479

You might also like