summaryrefslogtreecommitdiff
path: root/1034/CH7/EX7.5/7s5.sce
diff options
context:
space:
mode:
Diffstat (limited to '1034/CH7/EX7.5/7s5.sce')
-rwxr-xr-x1034/CH7/EX7.5/7s5.sce48
1 files changed, 48 insertions, 0 deletions
diff --git a/1034/CH7/EX7.5/7s5.sce b/1034/CH7/EX7.5/7s5.sce
new file mode 100755
index 000000000..d8ee9e8f1
--- /dev/null
+++ b/1034/CH7/EX7.5/7s5.sce
@@ -0,0 +1,48 @@
+clear;
+clc;
+disp("Example 7.5");
+funcprot(0);
+function[a1]=mergesort(a,p,r)
+ if(p<r)
+ q=int((p+r)/2);
+ a=mergesort(a,p,q);
+ a=mergesort(a,q+1,r);
+ a=merge(a,p,q,r);
+ else
+ a1=a;
+ return;
+ end
+ a1=a;
+endfunction
+function[a1]=merge(a,p,q,r)
+ n1=q-p+1;
+ n2=r-q;
+ left=zeros(n1+1);
+ right=zeros(n2+1);
+ for i=1:n1
+ left(i)=a(p+i-1);
+ end
+ for i1=1:n2
+ right(i1)=a(q+i1);
+ end
+ left(n1+1)=999999999;
+ right(n2+1)=999999999;
+ i=1;
+ j=1;
+ k=p;
+ for k=p:r
+ if(left(i)<=right(j))
+ a(k)=left(i);
+ i=i+1;
+ else
+ a(k)=right(j);
+ j=j+1;
+ end
+ end
+ a1=a;
+endfunction
+//Calling Routine:
+a=[26 5 77 1 61 11 59 15 48 19]
+disp(a,"Given Array");
+a1=mergesort(a,1,10)
+disp(a1,"Sorted array is:"); \ No newline at end of file