Program to sort an array of integers in ascending order using bubble sort.

#include <iostream>
using namespace std;

void bubble(int arr[],int n)
{
  for(int i=0;i<n;++i)
    for(int j=0;j<n-i-1;++j)
   if (arr[j]>arr[j+1])
   {
       int temp=arr[j];
       arr[j]=arr[j+1];
       arr[j+1]=temp;
   }
}

int main()
{
    int input[] = {10, 50, 21, 2, 6, 66, 802, 75, 24, 170};
  int n=sizeof(input)/sizeof(input[0]);
  bubble(input,n);
    cout<<"Sorted Array : \n";
 for(int i=0;i<n;++i)
  cout<<input[i]<<" ";
cout<<"\n/*\nName-Sanjampreet Singh\nRoll Number-1507967\n*/";
    return 0;
}

Output:


Comments

Popular posts from this blog

To create an html page with frames and frameset

1. Write a program to insert a new element at end as well as at a given position in an array in Java

1. To familiarize with network devices like switch, hub, routers and bridges in LaTex