Monday, October 10, 2011

C++ example program to sort the the array of elements.


  In this example, the algorithm to sort an array of elements is explained.

  This program will compare the adjacent elements in a array and swap based on the result.

  Copy and paste the below code in a text file, and rename it as filename.cpp ...

/* program to Sort the Array*/
#include<iostream.h>
#include<conio.h>
class sort
{
 private:
  int arr[100];
  int val;
 public:
  void sortarr();
};
void sort::sortarr()
{
 printf("How many values"<<endl;
 cin>>val;
 printf("Enter the Values"<<endl;
 for(int i=0;i<val;i++)
 {
  cin>>arr[i];
 }
 for(int i=0;i<val;i++)
 {
  for(int j=i+1;j<val;j++)
  {
   if(arr[i] > arr[j])
   {
    int temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
   }
  }
 }
 printf("Sorted Elements"<<endl;
 for(int i=0;i<val;i++)
 {
  cout<<arr[i]<<endl;
 }
}
void main()
{
 sort sortobj;
 clrscr();
 sortobj.sortarr();
 getch();
}

Hope this helps...

                                                                                                                                          Techytips

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Bluehost Coupons