Write a C++ program to Sort Elements of the array ; Bubble Sorting

In this program , arr[] array is declared and n numbers is stored in it which are entered by the user. Then each and every numbers are being compared each other (Bubble Sort) and finally numbers are sorted and displayed as output

#include<iostream.h>
#include<conio.h>
using namespace std;


void main()
    {
    clrscr();
    int arr[100],i,j,n,temp;
    cout<<"How many element? ";
    cin>>n;
    cout<<"Enter the elements of array: \n";
    for(i=0;i<n;i++)
    cin>>arr[i];
    cout<<"The elements of array Before Sorting: ";
    for(i=0;i<n;i++)
    cout<<arr[i]<<" ";
    cout<<"\n";
    for(i=0;i<n;i++)
    {
    for(j=0;j<n-1-i;j++)
    {
    if(arr[j]>arr[j+1])
    {
    temp=arr[j];
    arr[j]=arr[j+1];
    arr[j+1]=temp;
    }
    }
    }
    cout<<"Elements of array After Sorting:\n ";
    for(i=0;i<n;i++)
    cout<<arr[i]<<" ";
    getch();
    }

Post a Comment

0 Comments