Following C++ Program Sorts Characters in Ascending Order......
I also Used Templates, it will help you alot....
Regards,
Mudassar Malik
Here is the code,, Copy it and paste it in C++ editor and Compile....
Don't Forget to practice it....
#include<iostream>
#include<conio.h>
using namespace std;
template<class type>
void Sort(type* arr,int s)
{
type temp;
for(int i=0;i<s-1;i++)
{
for(int j=1;j<s-i;j++)
{
if(arr[j-1]>arr[j])
{
temp=arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
}
}
}
}
void main()
{
char *Array;
int Size;
cout<<"Enter Size of Array: ";
cin>>Size;
cout<<endl;
Array=new char[Size];
for(int i=0;i<Size;i++)
{
cout<<"Enter Value: ";
cin>>Array[i];
cout<<endl;
}
cout<<"Unsorted Array: "<<endl;
for(i=0;i<Size;i++)
{
cout<<" "<<Array[i];
}
cout<<endl;
Sort<char>(Array,Size);
cout<<"Sorted Array: "<<endl;
for(i=0;i<Size;i++)
{
cout<<" "<<Array[i];
}
cout<<endl;
getch();
}
I also Used Templates, it will help you alot....
Regards,
Mudassar Malik
Here is the code,, Copy it and paste it in C++ editor and Compile....
Don't Forget to practice it....
#include<iostream>
#include<conio.h>
using namespace std;
template<class type>
void Sort(type* arr,int s)
{
type temp;
for(int i=0;i<s-1;i++)
{
for(int j=1;j<s-i;j++)
{
if(arr[j-1]>arr[j])
{
temp=arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
}
}
}
}
void main()
{
char *Array;
int Size;
cout<<"Enter Size of Array: ";
cin>>Size;
cout<<endl;
Array=new char[Size];
for(int i=0;i<Size;i++)
{
cout<<"Enter Value: ";
cin>>Array[i];
cout<<endl;
}
cout<<"Unsorted Array: "<<endl;
for(i=0;i<Size;i++)
{
cout<<" "<<Array[i];
}
cout<<endl;
Sort<char>(Array,Size);
cout<<"Sorted Array: "<<endl;
for(i=0;i<Size;i++)
{
cout<<" "<<Array[i];
}
cout<<endl;
getch();
}
No comments:
Post a Comment