Thursday, 5 September 2013

Shell sort in python

class shell:
    #constructor defined
    def __init__(self,datalist):
          self.datalist = datalist
#defining methods inside the class:Encapsulation feature
    def shellsort(self):
          n=len(self.datalist)
          step=int(n/2)
          while step>0:
               i=step
               while i<n:
                    temp=self.datalist[i]
                    j=i
                    while j>=step:
                          if temp<self.datalist[j-step]:
                              self.datalist[j]=self.datalist[j-step]
                          else:
                              break
                          j=j-step
                    self.datalist[j]=temp
                    i=i+1
               step=int(step/2)


    def display(self):
            print(self.datalist)


  # Main Program
input = raw_input("Enter the values: ")     # Accept input from user
inputList = input.split()                   # Convert into list with space separator
inputList = [int(i) for i in inputList]     # Convert input into int array

obj = shell(inputList)

obj.display()
obj.shellsort()
obj.display()

1st prac Veg_&_Fruits

#include <iostream>
#include<string.h>
#define MAX 30
using namespace std;
class veg
{
public:
string v;
int vcount;
void getveg(string s,int p)
{
v=s;
vcount=p;
}
void display_veg(void)
{
cout<<"\n";
cout<<v;
cout<<"\t\t"<<vcount;
}
};
 class fruit
 {
 public:
string f;
int fcount;
void getfruit(string s,int p)
{
f=s;
fcount=p;

}
void display_fruit(void)
{
cout<<"\n";
cout<<f;
cout<<"\t\t"<<fcount;
}
 };

 void arrange(veg *vobj[MAX],fruit *fobj[MAX],int n)
 {
int temp;
string s;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(vobj[i]->vcount<vobj[j+1]->vcount)
{
temp=vobj[i]->vcount;
s=vobj[j]->v;
vobj[j]->vcount=vobj[j+1]->vcount;
vobj[i]->v=vobj[j+1]->v;
vobj[j+1]->vcount=temp;
vobj[j+1]->v=s;

}
}
}
 for(i=0;i<n-1;i++)
  {
  for(j=0;j<n-i-1;j++)
  {
  if(fobj[i]->fcount<fobj[j+1]->fcount)
  {
  temp=fobj[i]->fcount;
  s=fobj[j]->f;
  fobj[j]->fcount=fobj[j+1]->fcount;
  fobj[i]->f=fobj[j+1]->f;
  fobj[j+1]->fcount=temp;
  fobj[j+1]->f=s;
  }
  }
  }
cout<<"\n veg \t\t fruit";
cout<<"\n------------------";
for(i=0;i<n;i++)
{
cout<<"\n";
cout<<vobj[i]->v<<"\t\t"<<fobj[i]->f;
}
}

 int main()
{
veg *vobj[MAX];
string vname="";
int vcount;
fruit *fobj[MAX];
string fname="";
int fcount,i,n;

void arrange(veg *vobj[MAX],fruit *fobj[MAX], int n);

cout<<"\n \t\t\t VEGETABLE AND FRUIT PURCHASE PATTERN";
cout<<"\n how many items?";
cin>>n;
cout<<"\n ENTRE THE VEGETABLE DETAILS \n";
for(i=0;i<n;i++)
{
cout<<"veg name:";
cin>>vname;
cout<<"Total purchase count:";
cin>>vcount;
vobj[i]=new veg;
vobj[i]->getveg(vname,vcount);
}
cout<<"\n Entre fruit details\n";
for(i=0;i<n;i++)
{
cout<<"fruit name:";
cin>>fname;
cout<<"Total purchase count:";
cin>>fcount;
fobj[i]=new fruit;
fobj[i]->getfruit(fname,fcount);
}
cout<<"\n vegetable \t purchase count";
cout<<"\n-----------------------------------------------------";
for(i=0;i<n;i++)
{
vobj[i]->display_veg();

}
cout<<"\n fruit \t\t\t purchase count";
cout<<"\n -----------------------------------------------------";
for(i=0;i<n;i++)
{
fobj[i]-> display_fruit();
}
      cout<<"\n\n arranging according to purchase pattern ---------";
      arrange(vobj,fobj,n);
      return 0;
}

Queue Operations

// Queue.java demonstrates operations on queue


import java.util.Scanner;

class Queue
   {
   private int maxSize;
   private int[] queArray;
   private int front;
   private int rear;
   private int nItems;
   public Queue(int s)         
      {
      maxSize = s;
      queArray = new int[maxSize];
      front = 0;
      rear = -1;
      nItems = 0;
      }
   public void insert(int j)
   {
       if( !isFull())
             {                      
                 queArray[++rear] = j;
                 nItems++;
             }
       else
           System.out.println("Queue is Full");
           
   }
   public int remove()     
   {
       int temp=0;
       if(!isEmpty())
       {
           temp = queArray[front++];    
           nItems--;                               
           System.out.println("Element deleted : " +temp);
         
           if(isFull() )  
               front = 0;
       }
       else
           System.out.println("Queue is Empty");
       return temp;
    }
   public void display()   
      {
         long num;
         int n = front;
         if(isEmpty())
         {
             System.out.println("Queue is Empty");
             return;
         }
         while(n != rear + 1)
         {                           
             num = queArray[n];        
             System.out.print(num);
             System.out.print(" ");
             n++;
         }
         System.out.println("");
      }

   public boolean isEmpty()
      {    
       return (nItems==0);
      }

   public boolean isFull() 
      {
       return (nItems==maxSize);
      }
   }

class QueueApp
{
   public static void main(String[] args)
   {
      Queue theQueue = new Queue(5);
      int ch,num;
      do
      {
          System.out.println("1. Insert in Queue \n2. Delete from Queue");
          System.out.println("3. Display Queue \n4. Quit ");
          System.out.println(" Enter your Choice : ");
          Scanner input = new Scanner(System.in);
          ch = input.nextInt();
          switch(ch)
          {
              case 1:
                            System.out.println("Enter element in Queue");
                          num = input.nextInt();
                          theQueue.insert(num);
                          break;
              case 2:     theQueue.remove();
                          break;
              case 3:     theQueue.display();
                          break;
              case 4:     System.exit(0);                        
              default: System.out.println("Invalid choice!");
          }
      }while(ch > 0);
   }
}