Showing posts with label Sorting. Show all posts
Showing posts with label Sorting. Show all posts

Saturday, August 27, 2011

How to sort an array using selection sort in C++.


//Program to sort an array using  selection sort in C++.
#include<iostream.h>
#include<conio.h>
void main()
{
 int a[50], n, temp, k , loc, i;
 int min, j;
 clrscr();
 cout<<endl<<"Enter the number of elements in array: ";
 cin>>n;
 cout<<endl<<"The array :";
 for (i=0;i<n;i++)
 {
  cin>>a[i];
 }
 for (k=0; k<n-1; k++)
 {
    min=a[k];
    loc=k;
    for (j=k+1; j<n; j++)
    {
     if(min>a[j])
      {
       min=a[j];
loc=j;
      }
    }
   temp=a[k];
   a[k]=a[loc];
   a[loc]=temp;
 }
 cout<<endl<<"The sorted array is: ";
 for (i=0; i<n; i++)
 {
  cout<<a[i]<<endl;
 }
 getch();
}

Program to sort an array using Radix sort in C++.


//Program to sort an array using  Radix sort in C++.
#include<iostream.h>
#include<conio.h>
void main()
{
 int a[50],i,n;
 void radixsort(int[],int);
 clrscr();
 cout<<endl<<" Enter the number of elements in array: ";
 cin>>n;
 cout<<endl<<"enter the elements of array: ";
 for(i=0;i<n;i++)
 {
  cin>>a[i];
 }
 radixsort(a,n);
 cout<<endl<<"The sorted array is as follow:"<<endl;
 for(i=0;i<n;i++)
 {
  cout<<a[i]<<" ";
 }
 getch();
}
void radixsort(int a[],int n)
{
 int bucket[10][20],bucketcount[10];
 int i,j,k,r,digitcount=0,divisor=1,largest,passno;
 largest=a[0];
 for(i=0;i<n;i++)
 {
  if(a[i]>largest)
  {
   largest=a[i];
  }
 }
 while(largest>0)
 {
  digitcount++;
  largest=largest/10;
 }
 for(passno=0;passno<digitcount;passno++)
 {
  for(k=0;k<10;k++)
  {
   bucketcount[k]=0;
  }
  for(i=0;i<n;i++)
   {
    r=(a[i]/divisor)%10;
    bucket[r][bucketcount[r]++]=a[i];
   }
   i=0;
   for(k=0;k<10;k++)
    {
     for(j=0;j<bucketcount[k];j++)
     {
       a[i]=bucket[k][j];
       i++;
     }
    }
  divisor=divisor*10;
 }
}

Sunday, August 21, 2011

How to Sort array in c++ using Bubble sort.

//How to Sort array in c++ using Bubble sort.
#include<iostream.h>
#include<conio.h>
void main()
{
  int a[40],n,i,t,k;
  clrscr();
  cout<<endl<<"Enter the no. of element in the array: ";
  cin>>n;
  cout<<endl<<"Enter the values of "<<n<<" elements: ";
  for(i=0;i<n;i++)
  {
    cin>>a[i];
  }
  for(k=0;k<n-1;k++)
  {
    for(i=0;i<n-1-k;i++)
    {
      if(a[i]>a[i+1])
      {
       t=a[i];
       a[i]=a[i+1];
       a[i+1]=t;
      }
    }
  }
  cout<<endl<<"The sorted array is:";
  for(i=0;i<n;i++)
  {
    cout<<endl<<a[i];
  }
  getch();
}

Saturday, August 20, 2011

QuickSort using recursion

//QuickSort using recursion
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
main()
{
      int i,*a,n;
      void create(int **a,int n);
      void display(int *a,int n);
      void quicksort(int *a,int lb,int ub);
      extern int quick(int *s,int left,int right);

      a=NULL;
      printf("Enter the number of elements you want in your array : " );
      scanf("%d",&n);
      create(&a,n);
      display(a,n);
      quicksort(a,0,n-1);
      display(a,n);
     getch();
      return 0;
}
void create(int **a,int n)
{
     int i;
     int *s;
     s=(int *)malloc(n*sizeof(int));
     printf("\n Enter the value of array elements " );
     for(i=0;i<n;i++)
     {
                  scanf("%d",&s[i]);
     }
     *a=s;
}
void display(int *a,int n)
{
     int i;
     printf("\n array elements " );
     for(i=0;i<n;i++)
     {
                  printf("%d ",a[i]);
     }
}
void quicksort(int *a,int lb,int ub)
{
     int loc;
     if(lb<ub)
     {
         loc= quick(a,lb,ub);
          quicksort(a,lb,loc-1);
          quicksort(a,loc+1,ub);
     }  
                       
}
int quick(int *s,int left,int right)
{
    int loc,temp;
    loc=left;
    while(1)
    {
            while(s[loc]<=s[right]&&loc!=right)
              {
                  right=right-1;
             
              }
            if(loc==right)
              return loc;
            if(s[loc]>s[right])
            {
               temp=s[loc];
               s[loc]=s[right];
               s[right]=temp;
            }
         
            loc=right;
            while(s[left]<=s[loc]&&loc!=left)
              left=left+1;
            if(loc==left)
              return loc;
            if(s[left]>s[loc])
            {
               temp=s[loc];
               s[loc]=s[left];
               s[left]=temp;
            }
            loc=left;
    }
       
}

Sort an array using selection sort.

//Program to sort an array using  selection sort..
#include<stdio.h>
#include<conio.h>
void main()
{
 int *a, n, temp, k , loc, i;
 int min, j;
 clrscr();
 printf("\nEnter the number of elements in array: ");
 scanf("%d",&n);
 a=(int *)malloc(n*sizeof(int));
  printf("\nEnter The array :");
 for (i=0;i<n;i++)
 {
   scanf("%d",&a[i]);;
 }
 for (k=0; k<n-1; k++)
 {
    min=a[k];
    loc=k;
    for (j=k+1; j<n; j++)
    {
     if(min>a[j])
      {
       min=a[j];
    loc=j;
      }
    }
   temp=a[k];
   a[k]=a[loc];
   a[loc]=temp;
 }
 printf("\nThe sorted array is:");
  for(i=0;i<n;i++)
  {
      printf("\n %d",a[i]);
  }
 getch();
}

Next Home
Twitter Delicious Facebook Favorites More