diff options
Diffstat (limited to '3808/CH3')
-rw-r--r-- | 3808/CH3/EX3.1/Ex3_1.sce | 18 | ||||
-rw-r--r-- | 3808/CH3/EX3.2/Ex3_2.sce | 24 | ||||
-rw-r--r-- | 3808/CH3/EX3.3/Ex3_3.sce | 28 | ||||
-rw-r--r-- | 3808/CH3/EX3.4/Ex3_4.sce | 27 | ||||
-rw-r--r-- | 3808/CH3/EX3.5/Ex3_5.sce | 24 |
5 files changed, 121 insertions, 0 deletions
diff --git a/3808/CH3/EX3.1/Ex3_1.sce b/3808/CH3/EX3.1/Ex3_1.sce new file mode 100644 index 000000000..0b13d0d42 --- /dev/null +++ b/3808/CH3/EX3.1/Ex3_1.sce @@ -0,0 +1,18 @@ +//Chapter 03: Algorithms + +clc; +clear; + +ar=[] +max_v=0 +n=input('Enter the number of elements in the finite sequence:') +disp('Enter the elements one after the other!') +for i=1:n + ar(i)=input(' ') +end +for i=1:n + if ar(i)>max_v then + max_v=ar(i) + end +end +disp(max_v,'The largest element is:') diff --git a/3808/CH3/EX3.2/Ex3_2.sce b/3808/CH3/EX3.2/Ex3_2.sce new file mode 100644 index 000000000..23ee5ab2c --- /dev/null +++ b/3808/CH3/EX3.2/Ex3_2.sce @@ -0,0 +1,24 @@ +//Chapter 03: Algorithms + +clc; +clear; + +//Linear Search is also known as Sequential Search +function []= linearsearch (a ,n , ie ) +i =1; +j =0; +for i =1: n +if ( arr(i) == ie ) +printf ( "\nElement:%d found at position %d\n " ,ie , i ) ; +j =1; +end +end +if ( j ==0) +disp ( "Element Not Found!") ; +end +endfunction + +arr =[1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22] +l=length(arr) +disp (arr , " Given array:" ) ; +linearsearch (arr ,l ,19) //Note:input format for function is (array,length,element to be searched) diff --git a/3808/CH3/EX3.3/Ex3_3.sce b/3808/CH3/EX3.3/Ex3_3.sce new file mode 100644 index 000000000..a3a469d65 --- /dev/null +++ b/3808/CH3/EX3.3/Ex3_3.sce @@ -0,0 +1,28 @@ +//Chapter 03: Algorithms + +clc; +clear; + +function []= binarysearch (arr ,n ,i) +last =1; +h=n; +while (last <= h ) +mid = int (( last + h ) /2) ; +if ( arr ( mid ) == i ) +printf ( "\nElement:%d found at position %d",i ,mid) ; +break ; +else +if ( arr ( mid ) >i ) +h = mid -1; +else +last = mid +1; +end +end +end +endfunction + +//Note:input array has to be sorted +ar =[1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22] +l=length(ar) +disp (ar , " Given array " ) ; +binarysearch (ar ,l ,19) //Note:input format for function is (array,length,element to be searched) diff --git a/3808/CH3/EX3.4/Ex3_4.sce b/3808/CH3/EX3.4/Ex3_4.sce new file mode 100644 index 000000000..4d54fa302 --- /dev/null +++ b/3808/CH3/EX3.4/Ex3_4.sce @@ -0,0 +1,27 @@ +//Chapter 03: Algorithms + +clc; +clear; + +function [ res ]= bubblesort (a , n ) +i =1; +j =1; +temp =0; +for i =1: n -1 +for j =1: n - i +if ( a ( j ) >a ( j +1) ) +temp = a ( j ) ; +a ( j ) = a ( j +1) ; +a ( j +1) = temp ; +end +j = j +1; +end +i = i +1; +end +res = a ; +disp ( res ,"Sorted Array :") ; +endfunction + + a =[3 2 4 1 5] + disp (a , " Given Array " ) +a1 = bubblesort (a ,5) diff --git a/3808/CH3/EX3.5/Ex3_5.sce b/3808/CH3/EX3.5/Ex3_5.sce new file mode 100644 index 000000000..87528b20a --- /dev/null +++ b/3808/CH3/EX3.5/Ex3_5.sce @@ -0,0 +1,24 @@ +//Chapter 03: Algorithms + +clc; +clear; + +function result = insertionSort(Arr) + for i=2:length(Arr) + A = Arr(i); + j = i-1; + while (j>0 & Arr(j) > A) + Arr(j+1) = Arr(j); + j = j-1; + end + Arr(j+1) = A; + end + +result = Arr; +endfunction + +arr=[3 2 4 1 5] +disp(arr,"Given Array") +arr_s=insertionSort(arr) +disp(arr_s,"Sorted Array") + |