Bubble Sort With Pointer In Data Structure
C++ Coding In Microsoft Visual 6
#include<iostream.h>void bubleSort (int *arr);
//Main Function
void main ()
{
int arr[5];
cout<<"Enter Numbers To Store In Array"<<endl;
for(int i=0; i<5; i++)
{
cin>>arr[i];
}
cout<<"Beofre Sorting"<<endl;
for(i=0; i<5; i++)
{
cout<<arr[i]<<",";
}
//sorting array
bubleSort(arr);
cout<<"After Sorting"<<endl;
for(i=0; i<5; i++)
{
cout<<arr[i]<<",";
}
}
// Sorting Function
void bubleSort (int *arr)
{
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
if(arr[j]>arr[j+1])
{
arr[j]=arr[j]+arr[j+1];
arr[j+1]=arr[j]-arr[j+1];
arr[j]=arr[j]-arr[j+1];
}
}
}
}
0 comments: