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

Program For Bubble Sorting of 1D Array

This document contains code for bubble sorting a 1D array in C++. It prompts the user to enter the number of elements for the array, accepts user input to populate the array, prints the array before and after sorting, and uses nested for loops and an if statement to compare and swap adjacent elements into ascending order using the bubble sort algorithm.

Uploaded by

Nitish Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Program For Bubble Sorting of 1D Array

This document contains code for bubble sorting a 1D array in C++. It prompts the user to enter the number of elements for the array, accepts user input to populate the array, prints the array before and after sorting, and uses nested for loops and an if statement to compare and swap adjacent elements into ascending order using the bubble sort algorithm.

Uploaded by

Nitish Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Program for bubble sorting of 1D array

In gcc
#include <iostream>
using namespace std;
int main()
{
int n,a,b,c,a1[100];
cout<<"Enter how many number u want to enter : "<<endl;
cin>>n;
for(a=0;a<n;a++)
{
cout<<"Enter a number : "<<endl;
cin>>a1[a];
}
cout<<"Array before Sorting "<<endl;
for(a=0;a<n;a++)
{
cout<<a1[a]<<" ";
}
cout<<"\nArray after sorting "<<endl;
for(a=0;a<(n-1);a++)
{
for(b=a;b<n;b++)
{
if(a1[a]>a1[b])
{
c=a1[a];
a1[a]=a1[b];
a1[b]=c;
}
}
}
for(a=0;a<n;a++)
cout<<a1[a]<<" ";
cout<<endl;
return 0;
}

You might also like