22
1.MERGE SORT /* c program for merge sorting */ #include<stdio.h> #include<conio.h> void merge(int [],int ,int ,int ); void part(int [],int ,int ); int main() { int arr[30]; int i,size; printf("\n\t------- Merge sorting method -------\n\n"); printf("Enter total no. of elements : "); scanf("%d",&size); for(i=0; i<size; i++) { printf("Enter %d element : ",i+1); scanf("%d",&arr[i]); } part(arr,0,size-1); printf("\n\t------- Merge sorted elements -------\n\n"); for(i=0; i<size; i++) printf("%d ",arr[i]); getch(); return 0; } void part(int arr[],int min,int max) { int mid; if(min<max) { mid=(min+max)/2; part(arr,min,mid); part(arr,mid+1,max); merge(arr,min,mid,max); } } void merge(int arr[],int min,int mid,int max) { int tmp[30];

DSA EXperiments

Embed Size (px)

DESCRIPTION

Datastructure algorithms and programs for mumbai and pune university jounal writeups pattern

Citation preview

1.MERGE SORT/* c program for merge sorting */#includerea8'case C/emptB"&'>rea8'case D/e5it"*&'case P/displaB"&'>rea8'case Q/stac8Ocount"&'>rea8'case R/destroB"&'>rea8'default /printf", Srong choice% Ilease enter correct choice,&'>rea8'444 /* Freate emptB stac8 */!oid create"&(top 2 ;A99'4 /* Fount stac8 elements */!oid stac8Ocount"&(printf",-n ;o. of elements in stac8 / 0d,% count&'4 /* Iush data into stac8 */!oid push"int num&(if "top 22 ;A99&(top 2"struct node *&malloc"1*si+eof"struct node&&'top.ptr 2 ;A99'top.data 2 num'4else(temp 2"struct node *&malloc"1*si+eof"struct node&&'temp.ptr 2 top'temp.data2 num'top 2 temp'4count33'4 /* =isplaB stac8 elements */!oid displaB"&(top1 2 top' if "top1 22 ;A99&(printf",Stac8 is emptB,&'return'4 ?hile "top1 @2 ;A99&(printf",0d ,% top1.data&'top1 2 top1.ptr'4 4 /* Iop Operation on stac8 */!oid pop"&(top1 2 top' if "top1 22 ;A99&(printf",-n Error / TrBing to pop from emptB stac8,&'return'4elsetop1 2 top1.ptr'printf",-n Iopped !alue / 0d,% top.data&'free"top&'top 2 top1'count..'4 /* Return top element */int topelement"&(return"top.data&'4 /* Fhec8 if stac8 is emptB or not */!oid emptB"&(if "top 22 ;A99&printf",-n Stac8 is emptB,&'elseprintf",-n Stac8 is not emptB ?ith 0d elements,% count&'4 /* =estroB entire stac8 */!oid destroB"&(top1 2 top' ?hile "top1 @2 ;A99&(top1 2 top.ptr'free"top&'top 2 top1'top1 2 top1.ptr'4free"top1&'top 2 ;A99' printf",-n Ell stac8 elements destroBed,&'count 2 *'4D.TAEAE AS:;G ERREG#include rea8'case Q/Vueuesi+e"&'>rea8'default/printf",Srong choice% Ilease enter correct choice,&'>rea8'444 /* Freate an emptB Vueue */!oid create"&(front 2 rear 2 ;A99'4 /* Returns Vueue si+e */!oid Vueuesi+e"&(printf",-n Tueue si+e / 0d,% count&'4 /* EnVueing the Vueue */!oid enV"int data&(if "rear 22 ;A99&(rear 2 "struct node *&malloc"1*si+eof"struct node&&'rear.ptr 2 ;A99'rear.info 2 data'front 2 rear'4else(temp2"struct node *&malloc"1*si+eof"struct node&&'rear.ptr 2 temp'temp.info 2 data'temp.ptr 2 ;A99' rear 2 temp'4count33'4 /* =isplaBing the Vueue elements */!oid displaB"&(front1 2 front' if ""front1 22 ;A99& 11 "rear 22 ;A99&&(printf",Tueue is emptB,&'return'4?hile "front1 @2 rear&(printf",0d ,% front1.info&'front1 2 front1.ptr'4if "front1 22 rear&printf",0d,% front1.info&'4 /* =eVueing the Vueue */!oid deV"&(front1 2 front' if "front1 22 ;A99&(printf",-n Error/ TrBing to displaB elements from emptB Vueue,&'return'4elseif "front1.ptr @2 ;A99&(front1 2 front1.ptr'printf",-n =eVued !alue / 0d,% front.info&'free"front&'front 2 front1'4else(printf",-n =eVued !alue / 0d,% front.info&'free"front&'front 2 ;A99'rear 2 ;A99'4count..'4 /* Returns the front element of Vueue */int frontelement"&(if ""front @2 ;A99& 11 "rear @2 ;A99&&return"front.info&'elsereturn *'4 /* =isplaB if Vueue is emptB or not */!oid emptB"&( if ""front 22 ;A99& 11 "rear 22 ;A99&&printf",-n Tueue emptB,&'else printf",Tueue not emptB,&'4Q.:;J:U TO IOST J:U#include