You are on page 1of 17

1

/* IMPLEMENT MERGE SORT WITHOUT RECURSION */

#include<stdio.h>

#include<conio.h>

#define MAX 20

int main()

{ int a[MAX],temp[MAX],i,j,k,n,size,l1,h1,l2,h2;

printf ("\nEnter the number of elements : ");

scanf ("%d",&n);

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

{ printf ("\nEnter element %d :",i+1);

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

} printf ("\nUnsorted list is :");

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

printf("%d", a[i]);

for (size=1; size < n; size=size*2 )

{ l1 = 0;

k = 0;

while( l1+size < n)

{ h1=l1+size-1;

l2=h1+1;

h2=l2+size-1;

if ( h2>=n )

h2=n-1;

i=l1;
2

j=l2;

while(i<=h1 && j<=h2 )

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

temp[k++]=a[i++];

else

temp[k++]=a[ j++];

} while(i<=h1)

temp[k++]=a[i++];

while( j<=h2)

temp[k++]=a[ j++];

l1 = h2+1;

} for (i=l1; k<n; i++)

temp[k++]=a[i];

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

a[i]=temp[i];

printf ("\nSize=%d \nElements are : ",size);

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

printf ("%d", a[i]);

} printf ("\nSorted list is :\n");

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

printf ("%d", a[i]);

return 0;

}
3

/* BUBBLE SORT */

#include<stdio.h>

#include<conio.h>

#define MAXSIZE 50

void bubble(int x[],int n)

{ int i,j,tmp;

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

{ for(j=0;j<n-1;j++)

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

{ tmp=x[j];

x[j]=x[j+1];

x[j+1]=tmp; }}}

}int main()

{ int x[MAXSIZE],n,i;

printf("Enter the number of elements: ");

scanf("%d",&n);

printf("\nEnter the elements: \n");

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

scanf("%d",&x[i]);

bubble(x,n);

printf("\nThe sorted output:\n");

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

printf("\n%d",x[i]);
4

return 0;

INSERTION SORT

#include<stdio.h>

#include<conio.h>

void insertion(int x[],int n)

{ int i,j,temp;

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

{ temp=x[i];

for(j=i-1;j>=0;j--)

{ if(temp<x[j])

x[j+1]=x[j];

else

} x[j+1]=temp;

}} int main()

{ int x[10],n,i;

printf("Enter the number of elements: ");

scanf("%d",&n);

printf("\nEnter the elements: \n");

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

scanf("%d",&x[i]);

insertion(x,n);

printf("\nThe sorted output:\n");

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

printf("\n%d",x[i]);

return 0; }

/* Selection Sort */

#include<stdio.h>

#include<conio.h>

#define MAXSIZE 50

void selection(int x[], int n)

{ int i, j, larg,pos,c=0;

for (i = n-1; i>0; i--){

larg = x[0];

pos = 0;

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

{ c++;

if (x[j] > larg){

larg = x[j];

pos = j;

}} x[pos] = x[i];

x[i] = larg; }

printf("\n Number of comparisons required = %d \n",c);

} int main()

{ int x[MAXSIZE],n, i;

printf("\n Enter number of elements ");

scanf("%d",&n);

printf("\n Unsorted list is as follows\n");


6

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

{ printf("\n Enter element %d ",i+1);

scanf("%d",&x[i]);

} selection(x, n);

printf("\n Sorted list is:\n\n");

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

printf("%d ", x[i]); }

MAX-HEAPIFY

#include <stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<malloc.h>

#include<dos.h>

#include<time.h>

void main()

{ clock_t start,end;

int heap[10], no, i, j, c, root, temp;

printf("\n Enter no of elements :");

scanf("%d", &no);

start = clock();

int *p=(int*) malloc(no*sizeof(int));

printf("\n Enter the nos : ");

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

{ *(p+i)=rand();
7

} for (i = 1; i < no; i++)

{ c = i;

do

{ root = (c - 1) / 2;

if (heap[root] < heap[c])

{ temp = heap[root];

heap[root] = heap[c];

heap[c] = temp;

{ c = root;

} while (c != 0);

{ printf("Heap array : ");

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

printf("%d\t ", heap[i]);

for (j = no - 1; j >= 0; j--)

{ temp = heap[0];

heap[0] = heap[j];

heap[j] = temp;

root = 0;

do

{ c = 2 * root + 1;

if ((heap[c] < heap[c + 1]) && c < j-1)

c++;

if (heap[root]<heap[c] && c<j)

{ temp = heap[root];
8

heap[root] = heap[c];

heap[c] = temp;

{ root = c;

} while (c < j);

{ printf("\nThe sorted array is : ");

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

printf("\t %d", heap[i]);

end = clock();

printf("\nThe time for the event was: %f",(float)(end-start)/CLK_TCK);

return 0;

HEAP-SORT

#include<stdio.h>

#include<conio.h>

int a[20],n;

void display()

{ int i;

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

printf ("%d",a[i]);

printf ("\n");

} void insert(int num,int loc)

{ int parent;

while(loc>0)

{ parent=(loc-1)/2;
9

if (num<=a[parent])

{ a[loc]=num;

return;

} a[loc]=a[parent];

loc=parent;

} a[0]=num;

} void create_heap()

{ int i;

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

insert(a[i],i);

} void del_root(int last)

{ int left,right,i,temp;

i=0;

temp=a[i];

a[i]=a[last];

a[last]=temp;

left=2*i+1;

right=2*i+2;

while( right < last)

{ if ( a[i]>=a[left] && a[i]>=a[right] )

return;

if ( a[right]<=a[left] )

{ temp=a[i];

a[i]=a[left];
10

a[left]=temp;

i=left;

} else

{ temp=a[i];

a[i]=a[right];

a[right]=temp;

i=right;

} left=2*i+1;

right=2*i+2;

} if ( left==last-1 && a[i]<a[left] )

{ temp=a[i];

a[i]=a[left];

a[left]=temp;

}} void heap_sort()

{ int last;

for(last=n-1; last>0; last--)

del_root(last);

} int main()

{ int i;

printf ("\nEnter number of elements :");

scanf ("%d",&n);

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

{ printf ("\nEnter element %d :",i+1);

scanf ("%d",&a[i]);
11

} printf ("\nEntered list is :\n");

display();

create_heap();

printf ("\nHeap is :\n");

display();

heap_sort();

printf ("\nSorted list is :\n");

display();

return 0;

LAB – 7

#include <stdio.h>

int array[100], n;

int main()

{ int choice, num;

n = 0;

while(1)

{ printf("1.Insert the element \n");

printf("2.Delete the element \n");

printf("3.Display all elements \n");

printf("4.Quit \n");

printf("Enter your choice : ");

scanf("%d", &choice);

switch(choice)
12

{ case 1:

printf("Enter the element to be inserted to the list : ");

scanf("%d", &num);

insert(num, n);

n = n + 1;

break;

case 2:

printf("Enter the elements to be deleted from the list: ");

scanf("%d", &num);

delete(num);

break;

case 3:

display();

break;

case 4:

exit(0);

default:

printf("Invalid choice \n");

}} return 0;

} display()

{ int i;

if (n == 0)

{ printf("Heap is empty \n");

return;
13

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

printf("%d ", array[i]);

printf("\n");

} insert(int num, int location

{ int parentnode;

while (location > 0)

{ parentnode =(location - 1)/2;

if (num <= array[parentnode])

{ array[location] = num;

return;

} array[location] = array[parentnode];

location = parentnode;

} array[0] = num;

} delete(int num)

{ int left, right, i, temp, parentnode;

for (i = 0; i < num; i++) {

if (num == array[i])

break;

{ if (num != array[i])

{ printf("%d not found in heap list\n", num);

return;

} array[i] = array[n - 1];

n = n - 1;

parentnode =(i - 1) / 2;
14

if (array[i] > array[parentnode])

{ insert(array[i], i);

return;

} left = 2 * i + 1;

right = 2 * i + 2;

while (right < n)

{ if (array[i] >= array[left] && array[i] >= array[right])

return;

if (array[right] <= array[left])

{ temp = array[i];

array[i] = array[left];

array[left] = temp;

i = left;

{ else

{ temp = array[i];

array[i] = array[right];

array[right] = temp;

i = right;

{ left = 2 * i + 1;

right = 2 * i + 2;

{ if (left == n - 1 && array[i]) {

temp = array[i];

array[i] = array[left];

array[left] = temp;
15

return 0;

}}

COUNTING – SORT

#include <stdio.h>

#include<stdlib.h>

#include<malloc.h>

#include<dos.h>

#include<time.h>

void counting_sort(int A[], int k, int n)

{ int i, j;

int B[15], C[100];

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

C[i] = 0;

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

C[A[j]] = C[A[j]] + 1;

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

C[i] = C[i] + C[i-1];

for (j = n; j >= 1; j--)

{ B[C[A[j]]] = A[j];

C[A[j]] = C[A[j]] - 1;

} printf("The Sorted array is : ");

counting_sort(A, k, n);

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

printf("%d ", B[i]);


16

printf("\n");

} int main()

{ clock_t start,end;

int n, k = 0, A[15], i;

printf("Enter the number of input : ");

scanf("%d", &n);

start = clock();

printf("\nEnter the elements to be sorted :\n");

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

{ scanf("%d", &A[i]);

if (A[i] > k) {

k = A[i];

}} end = clock();

printf("\nThe time for the event was: %f",(float)(end-


start)/CLK_TCK); }

BUCKET – SORT

#include <stdio.h>

void Bucket_Sort(int array[], int n)

{ int i, j;

int count[n];

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

count[i] = 0;

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


17

(count[array[i]])++;

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

for(; count[i] > 0; (count[i])--)

array[j++] = i;

} int main()

{ int array[100], i, num;

printf("Enter the size of array : ");

scanf("%d", &num);

printf("Enter the %d elements to be sorted:\n",num);

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

scanf("%d", &array[i]);

printf("\nThe array of elements before sorting : \n");

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

printf("%d ", array[i]);

printf("\nThe array of elements after sorting : \n");

Bucket_Sort(array, num);

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

printf("%d ", array[i]);

printf("\n"); }

You might also like