Sorting Techniques

Posted on

Sorting Techniques

 

Insertion Sort

 

#include<stdio.h>

void insertion_sort(int [],int);

void main()

{

int a[20];

int i,n;

printf(“enter n”);

scanf(“%d”,&n);

 

printf(“Enter values”);

for(i=0;i<n;i++)

scanf(“%d”,&a[i]);

 

printf(“\nDuring the passes….\n\n”);

insertion_sort(a,n);

printf(“\nSorted List is…\n”);

printf(“\n”);

for(i=0;i<n;i++)

printf(“%4d”,a[i]);

}

 

void insertion_sort(int a[],int n)

{

int i,j,k,z;

for(i=1;i<n;i++)

{

k = a[i];

j = i;

while ((j>0)&&(a[j-1]>k))

{

a[j] = a[j-1];

j–;

}

a[j] = k;

for(z=0;z<n; z++)

printf(“%3d”,a[z]);

printf(“\n\n”);

}

}

 
Quick Sort

 

#include<stdio.h>

void sort(int [],int,int);

 

void main()

{

int a[20];

int i, j, k, n;

printf(“enter n”);

scanf(“%d”,&n);

 

printf(“Enter values”);

for(i=0;i<n;i++)

scanf(“%d”,&a[i]);

I = 0;

sort(a, i,n-1);

printf(“\nSorted List is…\n”);

printf(“\n”);

for(i=0;i<n;i++)

printf(“%4d”,a[i]);

}

void sort(int a[10],int left,int right)

{

int pivot;

int l,r;

l = left;

r = right;

pivot = a[left];

while(left<right)

{

while((a[right]>=pivot)&&(left<right))

right–;

if(left!=right)

{

a[left] = a[right];

left++;

}

while((a[left]<pivot)&&(left<right))

left++;

if(left!=right)

{

a[right] = a[left];

right–;

}

}

a[left]= pivot;

pivot = left;

left = l;

right = r;

if(left<pivot)

sort(a,left,pivot-1);

if(right>pivot)

sort(a,pivot+1,right);

}
Two Way Merge Sort

 

#include<stdio.h>

void merge_pass(int [],int,int);

void merge_sort(int [],int,int,int);

 

void main()

{

int a[20];

int i,j,k,n;

printf(“enter n”);

scanf(“%d”,&n);

 

printf(“Enter values”);

for(i=0;i<n;i++)

scanf(“%d”,&a[i]);

 

i=0;

merge_pass(a,i,n-1);

printf(“\nMerged List is…\n”);

printf(“\n”);

for(i=0;i<n;i++)

printf(“%4d”,a[i]);

}

 

void merge_sort ( int a[100], int top, int size, int bot )

{

int u,i,j,k;

int temp[100];

 

i= top;

j = size+1;

k = top;

 

while((i<=size)&&(j<=bot))

{

if(a[i]<=a[j])

{

temp[k]=a[i];

i++ ;

}

else

{

temp[k] = a[j];

j++;

}

k++;

}

if(i<=size)

{

while(i<=size)

{

temp[k] =a[i];

k++;

i++;

}

}

else

{

while(j<=bot)

{

temp[k]=a[j];

k++;

j++;

}

}

for(u=top;u<=bot;u++)

a[u] = temp[u];

return;

}

 

void merge_pass(int a[10],int m,int n)

{

int h;

if(m!=n)

{

h = (m+n)/2;

merge_pass(a,m,h);

merge_pass(a,h+1,n);

merge_sort(a,m,h,n);

}

}

 

Heap sort

 

#include<stdio.h>

#include<stdlib.h>

void heapsort(int [],int);

void buildheap(int [],int,int);

void main()

{

int a[20];

int i,n;

printf(“enter n”);

scanf(“%d”,&n);

 

printf(“Enter values”);

for(i=0;i<n;i++)

scanf(“%d”,&a[i]);

 

heapsort(a,n);

printf(“\nSorted List is…\n”);

printf(“\n”);

for(i=0;i<n;i++)

printf(“%4d”,a[i]);

}

void heapsort(int a[20],int max)

{

int i,temp;

for(i=(max/2)-1;i>0;i–)

buildheap(a,i,max);

for(i=max-1;i>=1;i–)

{

temp = a[0];

a[0] = a[i];

a[i] = temp;

buildheap(a,0,i-1);

}

}

void buildheap(int a[20],int r,int l)

{

int flag,max,temp;

flag = 0;

while((r*2<=l)&&(!flag))

{

if(r*2==l)

max = r*2;

else

if(a[r*2]> a[(r%2)+1])

max = r*2;

else

max = r*2+1;

if(a[r]<a[max])

{

temp = a[r];

a[r] = a[max];

a[max] = temp;

r = max;

}

else

flag = 1;

}

}

 

ABAP Interfaces Demo

Posted on

*&———————————————————————*
*& Report  ZABAP_INTERFACE_DEMO
*&
*&———————————————————————*
*&
*&
*&———————————————————————*

report zabap_interface_demo.
interface lif_employee.
  methods:
    add_employee
      importing im_no   type i
                im_name type string
                im_wage type i.
endinterface.

class lcl_company_employees definition.
  public section.
    interfaces lif_employee.
    types:
      begin of t_employee,
        no  type i,
        name type string,
        wage type i,
     end of t_employee.
    methods:
      constructor,
      display_employee_list,
      display_no_of_employees.
  private section.
    class-datai_employee_list type table of t_employee,
                no_of_employees type i.
endclass.
*– CLASS LCL_CompanyEmployees IMPLEMENTATION
class lcl_company_employees implementation.
  method constructor.
    no_of_employees no_of_employees + 1.
  endmethod.
  method lif_employee~add_employee.
*   Adds a new employee to the list of employees
    datal_employee type t_employee.
    l_employeeno im_no.
    l_employeename im_name.
    l_employeewage im_wage.
    append l_employee to i_employee_list.
  endmethod.
  method display_employee_list.
*   Displays all employees and there wage
    datal_employee type t_employee.
    write‘List of Employees’.
    loop at i_employee_list into l_employee.
      write/ l_employeenol_employeenamel_employeewage.
    endloop.
  endmethod.
  method display_no_of_employees.
*   Displays total number of employees
    skip 3.
    write‘Total number of employees:’no_of_employees.
  endmethod.
endclass.

class lcl_bluecollar_employee definition
          inheriting from lcl_company_employees.
  public section.
    methods:
        constructor
          importing im_no             type i
                    im_name           type string
                    im_hours          type i
                    im_hourly_payment type i,
         lif_employee~add_employee redefinition..
  private section.
    data:no             type i,
         name           type string,
         hours          type i,
         hourly_payment type i.
endclass.
*—- CLASS LCL_BlueCollar_Employee IMPLEMENTATION
class lcl_bluecollar_employee implementation.
  method constructor.
*   The superclass constructor method must be called from the subclass
*   constructor method
    call method super->constructor.
    no im_no.
    name im_name.
    hours im_hours.
    hourly_payment im_hourly_payment.
  endmethod.
  method lif_employee~add_employee.
*   Calculate wage an call the superclass method add_employee to add
*   the employee to the employee list
    datal_wage type i.
    l_wage hours * hourly_payment.
    call method super->lif_employee~add_employee
      exporting im_no no
                im_name name
                im_wage l_wage.
  endmethod.
endclass.

class lcl_whitecollar_employee definition
    inheriting from lcl_company_employees.
  public section.
    methods:
        constructor
          importing im_no                 type i
                    im_name               type string
                    im_monthly_salary     type i
                    im_monthly_deducations type i,
         lif_employee~add_employee redefinition.
  private section.
    data:
      no                    type i,
      name                  type string,
      monthly_salary        type i,
      monthly_deducations    type i.
endclass.
*—- CLASS LCL_WhiteCollar_Employee IMPLEMENTATION
class lcl_whitecollar_employee implementation.
  method constructor.
*   The superclass constructor method must be called from the subclass
*   constructor method
    call method super->constructor.
    no im_no.
    name im_name.
    monthly_salary im_monthly_salary.
    monthly_deducations im_monthly_deducations.
  endmethod.
  method lif_employee~add_employee.
*   Calculate wage an call the superclass method add_employee to add
*   the employee to the employee list
    datal_wage type i.
    l_wage monthly_salary – monthly_deducations.
    call method super->lif_employee~add_employee
      exporting im_no no
                im_name name
                im_wage l_wage.
  endmethod.
endclass.

data:
* Object references
  o_bluecollar_employee1  type ref to lcl_bluecollar_employee,
  o_whitecollar_employee1 type ref to lcl_whitecollar_employee.
start-of-selection.
* Create bluecollar employee obeject
  create object o_bluecollar_employee1
      exporting im_no  1
                im_name  ‘Chandrasekhar’
                im_hours 38
                im_hourly_payment 75.
* Add bluecollar employee to employee list
  call method o_bluecollar_employee1->lif_employee~add_employee
      exporting im_no  1
                im_name  ‘Vikram C’
                im_wage 0.
* Create whitecollar employee obeject
  create object o_whitecollar_employee1
      exporting im_no  2
                im_name  ‘Raghava V’
                im_monthly_salary 10000
                im_monthly_deducations 2500.
* Add bluecollar employee to employee list
  call method o_whitecollar_employee1->lif_employee~add_employee
      exporting im_no  1
                im_name  ‘Gylle Karen’
                im_wage 0.
* Display employee list and number of employees. Note that the result
* will be the same when called from o_whitecollar_employee1 or
* o_bluecolarcollar_employee1, because the methods are defined
* as static (CLASS-METHODS)
  call method o_whitecollar_employee1->display_employee_list.
  call method o_whitecollar_employee1->display_no_of_employees.

ABAP OBJECT ORIENTED PROGRAMMING DEMO

Posted on

*&———————————————————————*
*& Report  ZABAP_OO_DEMO
*&
*&———————————————————————*
*&
*&
*&———————————————————————*

report zabap_oo_demo.
class dog definition.
  public section.
  typesttype_of_dog(10type c.

  class-datatype_of_animal(10type value ‘DOG’.

  data:       type_of_dog type ttype_of_dog value ‘BULLDOG’,
              date_of_birth type d.

  methodsbark,
           eat importing type_of_food type i,
           get_age_of_dog returning value(age_of_dogtype i.
  events:
        not_a_baby.

  protected section.

  constants:type_of_food_fish type value 1,
            type_of_food_bone type value 2.

  private section.
endclass.

class dog implementation.
  method bark.
    write:/‘BAUUU WAUUUU’ color col_negative inverse.
  endmethod.

  method eat.
    if type_of_food type_of_food_fish.
      write‘Bwadrrrrr’ color col_negative inverse.
    else.
      write‘Yahooo’ color col_negative inverse.
    endif.
  endmethod.

  method get_age_of_dog.
    age_of_dog sydatum – date_of_birth.
    if  age_of_dog  < 100.
      raise event not_a_baby.
     endif.

  endmethod.

  endclass.

 “******************************** USING THE CLASS **************

  data:my_dog type ref to dog,
        age type i.

 start-of-selection.

 create object:my_dog.

 my_dog->date_of_birth ‘19920622’.

 write:/‘What kind of animal are you?’.
 write:my_dog->type_of_animal.

 write:/‘What kind of dog are you?’.
 write:my_dog->type_of_dog.

* Invoking af method of the dog class that returns a value
write‘How old are you’.
age my_dog->get_age_of_dog).
write/ age color col_negative inverse,
‘days’ color col_negative inverse.
skip.

* Invoking a non parameterized method, that doesn’t
* return a value
write‘BARK !’.
call method my_dog->bark.

skip.

* 2 examples of invoking a parameterized method
write‘Eat a fish !’.
call method my_dog->eat).
skip.

write‘Eat a bone !’.
call method my_dog->eat).

ABAP Inheritance demo

Posted on

*&———————————————————————*
*& Report  ZABAP_OO_INHERITANCE_DEMO
*&
*&———————————————————————*
*&
*&
*&———————————————————————*

report zabap_oo_inheritance_demo.

*———————————————————————-*
*       CLASS EMPLOYEE DEFINITION
*———————————————————————-*
*
*———————————————————————-*
class employee definition.
public section.
data:
nemployeeno type i,
cemployeename(10type c,
inoofunits type i,
ipayperunit type i.
* Methods for calculating and writing pay
methods:
calculatepay returning value(ipaytype i,
writepay importing value(ipaytype i.

endclass.                   

 “EMPLOYEE DEFINITION

*———————————————————————-*
*       CLASS employee IMPLEMENTATION
*———————————————————————-*
*
*———————————————————————-*
class employee implementation.
method calculatepay.
ipay inoofunits * ipayperunit.
endmethod.                    “CalculatePay

method writepay.
write‘The pay for employee ‘,
nemployeenocemployeename,
‘ is : ‘ipay.
endmethod.                    “WritePay
endclass.                    “employee IMPLEMENTATION

************************************************************************
* Subclass Factory Worker
*
* The class FactoryWorker is a subclass of the superclass Employee.
* Note that the attribute iExtraNoUnits have been added and the
* method CalculatePay has been redefined.
*
************************************************************************

class factoryworker definition inheriting from employee.
public section.
* Defining an event
eventslazyemployee.

dataiextranounits type i.

methods:
* Redefinition af the CalculatePay method from the Superclass
calculatepay redefinition.

endclass.

class factoryworker implementation.
method calculatepay.
* Redefined CalculatePay method
ipay inoofunits * ipayperunit +
iextranounits * ipayperunit * ).

* Raising the event LazyEmployee.
if inoofunits < 100.
raise event lazyemployee.
endif.
endmethod.

endclass.

************************************************************************
* This class only to show how you can handle an event that have been
* raised in another class
************************************************************************

class fireemployee definition.

public section.
* Method for handling the event LazyEmployee in the FactoryWorker
* subclass.
methods youarefired
for event lazyemployee
of factoryworker.
endclass.

class fireemployee implementation.
method youarefired.
write:‘You lazy worker, you have produced less than 100 units.’
color col_heading,
‘YOU ARE FIRED !!!’ color col_negative intensified.
endmethod.
endclass.

************************************************************************
* U S I N G T H E C L A S S E S
************************************************************************

datamypay type i.

start-of-selection.
*———————————————————————–
* Using the superclass Employee
*———————————————————————–

* Declare new Employee from the EmployeeClass

datasamsmith type ref to employee.

create objectsamsmith.
samsmith->nemployeeno 433.
samsmith->cemployeename ‘Sam Smith’.
samsmith->inoofunits 150.
samsmith->ipayperunit 3.
mypay samsmith->calculatepay).
call method samsmith->writepaymypay ).
skip.

*———————————————————————–
* Using the subclass FactoryWorker
*———————————————————————–

* Declare new FactoryWorker as subclass of the EmployeeClass
datajessicajones type ref to factoryworker.

create objectjessicajones.

jessicajones->nemployeeno 1500.
jessicajones->cemployeename ‘Jessica Jones’.
jessicajones->inoofunits 300.
jessicajones->iextranounits 500.
jessicajones->ipayperunit 3.

mypay jessicajones->calculatepay).
call method jessicajones->writepaymypay ).
skip.

*———————————————————————–
* Using the subclass FactoryWorker with event
*———————————————————————–
datalazyjohn type ref to factoryworker,
firehim type ref to fireemployee.

create objectlazyjohn,
firehim.

* The SET HANDLER statement, registers a runtime trigger.
* It links a list of handler methods to corresponding event triggers
* Each SET HANDLER statement creates an entry in an event handler table

set handler firehim->youarefired for lazyjohn.

lazyjohn->nemployeeno 1500.
lazyjohn->cemployeename ‘Lazy John’.

lazyjohn->inoofunits 90.
lazyjohn->iextranounits 500.
lazyjohn->ipayperunit 3.
write‘Lazy John has produced less than 100 units’.
mypay lazyjohn->calculatepay).