Published using Google Docs
BasicSortings
Updated automatically every 5 minutes

/* PROGRAM TO SORT THE GIVEN LIST OF ELEMENTS BY  USING DIFFERENT TECHNIQUES */

/* HERE YOU HAVE WRITE THE MAIN FUNCTION, BASED ON YOUR REQUIREMENTS */

#include"Array.cpp"

class Sort : public Array

{

public:

void bubbleSort();

void selectionSort();

void insertionSort();

void swap(int *p, int *q) {int temp; temp=*p; *p=*q; *q=temp; }

};

/* BUBBLE SORT ROUTINE*/

void Sort :: bubbleSort()

{

int i,j,temp;

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

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

            {

              if(a[j]>a[j+1]){

                       /*    temp=a[j];

                    a[j]=a[j+1];

                    a[j+1]=temp;  */

                    swap(&a[j],&a[j+1]);

              }

            }

}

/* SELECTION SORT ROUTINE */

void Sort :: selectionSort()

{

 int i,j,pos, temp;

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

    {    pos=i;

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

                    if(a[pos]>a[j]){ pos=j;}

             }

            //temp=a[pos]; a[pos]=a[i]; a[i]=temp;

            swap(&a[pos],&a[i]);

    }

}

/* INSERTION SORT ROUTINE */

void Sort :: insertionSort()

{

int i,j,temp;

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

    {           temp=a[i];

            for(j=i;(j>0)&&(a[j-1]>temp);j--)

            {

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

            }

            a[j]=temp;

    }

}