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();
}

Sort an array using Insertion sort.

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

Sort an array using Bubble sort.

//Program to sort an array using Bubble sort.
#include<stdio.h>
#include<conio.h>
void main()
{
  int *a,n,i,t,k;
  clrscr();
  printf("\nEnter the no. of element in the array: ");
  scanf("%d",&n);
  a=(int *)malloc(n* sizeof(int));
  printf("\nEnter the values of %d elements: ",n);
  for(i=0;i<n;i++)
  {
    scanf("%d",&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;
      }
    }
  }
 printf("\nThe sorted array is:");
  for(i=0;i<n;i++)
  {
      printf("\n %d",a[i]);
  }
  getch();
}

Monday, July 11, 2011

How to perform Bubble Sort on Array created dynamically.


//How to perform Bubble Sort on Array created dynamically.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
main()
{
struct node *start=NULL,*start1=NULL,*start2=NULL;
int n,m;
void create(struct node **, int);
void show(struct node *);
void bubsort(struct node *);
printf("\nEnter the number of elements you want in your first linked list :");
scanf("%d",&m);
create(&start,m);
printf("\nlinked list is :\n");
show(start);
bubsort(start);
printf("\nlinked list after sorting :\n");
show(start);
getch();  
}

void create(struct node **q,int n)
{
int i;
struct node *lst;
struct node *p;
lst=NULL;
for(i=1;i<=n;i++)
{
 p=(struct node *)malloc(sizeof(struct node));
 printf("\nEnter the %d element :",i);
 scanf("%d",&(p->data));
 p->next=NULL;
 if(*q==NULL)
    *q=p;
 else
   lst->next=p;
  lst=p;
}
}

void show(struct node *start)
{
while(start!=NULL)
{
printf(" %d",start->data);
start=start->next;
}
}

void bubsort(struct node *start)
{
     struct node *ptr1,*ptr2,*t=NULL;
     int temp;
     ptr1=start;
     if(ptr1==NULL)
     {
       printf("\nList is empty");
       return;
     }
     while(ptr1->next!=NULL)
     {
       ptr2=start;
       while(ptr2->next!=t)
       {
         if(ptr2->data>(ptr2->next)->data)
         {
            temp= ptr2->data;
            ptr2->data=(ptr2->next)->data;
            (ptr2->next)->data=temp;
         }
         ptr2=ptr2->next;
       }
       t=ptr2;
       ptr1=ptr1->next;
     }
}              
                         

Next Home
Twitter Delicious Facebook Favorites More