Вы находитесь на странице: 1из 7

Homework Title/No: HW#1 Course Code: CAP553

Course Instructor: Lect.Jaswinder Singh Course Tutor (if applicable):


Date of Allotment: 24-07-10 Date of submission:09/08/2010
Student’s Roll number: RA3902A08 Section No.: A3902

Declaration:
I declare that this assignment is my individual work. I have not
copied from any other student’s work or from any other source except where
due acknowledgement is made explicitly in the text, nor has any part been
written for me by another person.

Student’sSign: TARUN GUPTA


1. Why structuring of data is required .Take one example problem and discuss
various problems that can be faced if wrong choice has been made in selecting
data structure for the problem.

Sol:- It is the ability of a computer to fetch and store data at any place in its memory,
specified by an address is called Data structures. A bit string that can be itself stored in
memory and manipulated by the program. Thus the record and array data structures are based
on computing the addresses of data items with arithmetic operations; while the linked data
structures are based on storing addresses of data items within the structure itself. Structuring
of data is required to save our memory space and to have fast access on the data.
In organization or in databases we don’t have to deal with small numbers of data there can be
million and billion of data records.
Let us consider an example where we have to find the maximum value in a set of 100
numbers. In this we can either use 100 variables or a data structure, such as an array of size
100, to store the numbers. When 100 different variables are used to store the numbers, the
algorithm to determine the maximum value among the numbers can be written as:

step 1. Accept 100 numbers and store them in num1, num2, num3, .. num100
step 2. Set maximum = num1
step 3. If num2 > maximum then: maximum = num2
step 4. If num3 > maximum then: maximum = num3:
.
.
step 102. If num100 > maximum then maximum = num100
step 103. Display maximum
Now look at the diference On the other hand, when an array of size 100 is used, the algorithm
can be written as:-
step1. Set maximum = num[0]
step 2. Repeat step3 varying i from 1 to 99
step 3. If numimum [i] > maximum then: maximum = num[i]
step 4. Display maximum

It is clear from the above two algorithms, that the algorithm using an array manipulates
memory much more efficiently than the algorithm using 100 variables. Also, the algorithm
using an array involves few steps (4 to 5) and is therefore, easier to understand and implement
as compared to the algorithm that uses 100 variables.

2. Elaborate the concept of “Algorithm complexity” and Complexity notations with


suitable examples.
Sol:- Nowadays in Computer Science age, it is very important to measure the quality of
algorithms, specially the specific amount of a certain resource an algorithm needs. Examples
of such resources would be time or memory storage (which is known as time space
complexity). Nowadays, memory storage is almost a nonessential factor when designing
algorithms but be aware that several systems still have memory constraints, such as Digital
Signal Processors in embedded systems.
Different algorithms may complete the same task with a different set of instructions in less or
more time, space or effort than other. The analysis and study of algorithms complexity of
such situation offers us to decide which algorithm is best to use.
Time complexity:- Best case time complexity is the least amount of time it would take for an
algorithm to execute. Small differences such as built in commands, and variable accesses are
ignored, but loops and recursion have a much more profound effect on runtime.
The worst case complexity for the algorithm is the same as its best case. This
algorithm is in O(n). In many cases however the best case does not equal the worst case. In
these instances it is quite easy to find a O1(n) such that O(n) = O(O1(n)), finding the tight
bound however, ie. the inf(S), requires looking at possible sequences that yields the best
results and proving there is no such sequence that yields a smaller O1(n).

Space complexity :- Space complexity is the measure of the amount of space, excluding the
original data passed in, that it takes an algorithm to execute on an input. Any algorithm that
must always store at least all of the data at a time runs in &O(n) of space complexity. If
however an algorithm only needed to store a counter of some kind its space complexity would
be log(n) where n is the final value of the counter. This is the reason as it only takes log2 bits
to store a number.

Complexity Notation :- The symbol O is used to describe an asymptotic upper bound for the
magnitude of a function in terms of another, simpler function. This means that for x > k, when
x tends to infinity, the value f(x) will always be inferior to C *g(x) (with C a constant).

O(1) -- Log2 n -- O(n) -- nlog2n -- O(n2 ) -- O(n3) -- O(nn)

3. Apply Bubble sort and Insertion sort on following


12 7 14 8 9 15 22 4
Sol:-

Bubble sorting is a simple sorting technique in sorting algorithm. In bubble sorting


algorithm, we arrange the elements of the list by forming pairs of adjacent elements. It means
we repeatedly step through the list which we want to sort, compare two items at a time and
swap them if they are not in the right order. Another way to visualize the bubble sort
algorithm is as its name, the smaller element bubble to the top. Here is the source
code implements bubble sorting algorithm in C which sorts an unordered list of integer.

PASS K=1 K=2 K=3 K=4 K=5 K=6 K=7 K=8


A[0] 12 7 7 7 7 7 7 7
A[1] 7 12 12 12 12 12 12 12
A[2] 14 14 14 8 8 8 8 8
A[3] 8 8 8 14 9 9 9 9
A[4] 9 9 9 9 14 14 14 14
A[5] 15 15 15 15 15 15 15 15
A[6] 22 22 22 22 22 22 22 4
A[7] 4 4 4 4 4 4 4 22

PASS K=1 K=2 K=3 K=4 K=5 K=6 K=7 K=8


A[0] 7 7 7 7 7 7 7 7
A[1] 12 12 8 8 8 8 8 8
A[2] 8 8 12 9 9 9 9 9
A[3] 9 9 9 12 12 12 12 12
A[4] 14 14 14 14 14 14 14 14
A[5] 15 15 15 15 15 15 4 4
A[6] 4 4 4 4 4 4 15 15
A[7] 22 22 22 22 22 22 22 22

PASS K=1 K=2 K=3 K=4 K=5 K=6 K=7 K=8


A[0] 7 7 7 7 7 7 7 7
A[1] 8 8 8 8 8 8 8 8
A[2] 9 9 9 9 9 9 9 9
A[3] 12 12 12 12 12 12 12 12
A[4] 14 14 14 14 14 4 4 4
A[5] 4 4 4 4 4 14 14 14
A[6] 15 15 15 15 15 15 15 15
A[7] 22 22 22 22 22 22 22 22

PASS K=1 K=2 K=3 K=4 K=5 K=6 K=7 K=8


A[0] 7 7 7 7 7 7 7 7
A[1] 8 8 8 8 8 8 8 8
A[2] 9 9 9 9 9 9 9 9
A[3] 12 12 12 12 4 4 4 4
A[4] 4 4 4 4 12 12 12 12
A[5] 14 14 14 14 14 14 14 14
A[6] 15 15 15 15 15 15 15 15
A[7] 22 22 22 22 22 22 22 22

PASS K=1 K=2 K=3 K=4 K=5 K=6 K=7 K=8


A[0] 7 7 7 7 7 7 7 7
A[1] 8 8 8 8 8 8 8 8
A[2] 9 9 9 4 4 4 4 4
A[3] 4 4 4 9 9 9 9 9
A[4] 12 12 12 12 12 12 12 12
A[5] 14 14 14 14 14 14 14 14
A[6] 15 15 15 15 15 15 15 15
A[7] 22 22 22 22 22 22 22 22

PASS K=0 K=1 K=2 K=3 K=4 K=5 K=6 K=7


A[0] 7 7 7 7 7 7 7 7
A[1] 8 8 4 4 4 4 4 4
A[2] 4 4 8 8 8 8 8 8
A[3] 9 9 9 9 9 9 9 9
A[4] 12 12 12 12 12 12 12 12
A[5] 14 14 14 14 14 14 14 14
A[6] 15 15 15 15 15 15 15 15
A[7] 22 22 22 22 22 22 22 22

PASS K=0 K=1 K=2 K=3 K=4 K=5 K=6 K=7


A[0] 7 4 4 4 4 4 4 4
A[1] 4 7 7 7 7 7 7 7
A[2] 8 8 8 8 8 8 8 8
A[3] 9 9 9 9 9 9 9 9
A[4] 12 12 12 12 12 12 12 12
A[5] 14 14 14 14 14 14 14 14
A[6] 15 15 15 15 15 15 15 15
A[7] 22 22 22 22 22 22 22 22

Insertion sort belongs to the O(n2) sorting algorithms. Unlike many sorting algorithms with
quadratic complexity, it is actually applied in practice for sorting small arrays of data. For
instance, it is used to improve quick sort. Some sources notice, that people use same
algorithm ordering items, for example, hand of cards.

ALGORITHM FOR INSERTION SORT:


.
step 1. Set ar[0]=-infinity
step 2. Repeat step 3 to 5 for i in 1, 2, to …….n.
step 3. Set TEMP =ar[i] and PTR=i-1.
step 4. Repeat while TEMP<ar[PTR]:
I. Set ar[PTR+1]:=ar[PTR]
II. Set PTR =PTR-1.
End for
step 5. Set ar[PTR+1]=TEMP.
End while
step 6. Stop and exit.
Passes A[0] A[1] A[2] A[5] A[6] A[7] A[8] A[9] A[10]
1 (Inf) 12 (7) 14 8 9 15 22 4
2 Inf 7 12 14 8 9 15 22 4
3 Inf 7 (12) 14 (8) 9 15 22 4
4 Inf 7 8 (12) 14 (9) 15 22 4
5 Inf 7 8 9 12 14 15 22 4
6 Inf 7 8 9 12 14 15 22 4
7 (Inf) 7 8 9 12 14 15 22 (4)
8 Inf 4 7 8 9 12 14 15 22

Part-B

4. An array A contain 20 positive integers .Write algorithm which find the number
of elements of A which are even ,and the number of elements of array which are
odd.
sol:- [SCE(sequence counter of even no.), SCO(sequence counter of odd no.)]
step 1. start
step 2. Set a[20] read values and set SCE=0 && SCO=0.
step 3. repeat step 3 while i is 1 to 20
step 4. if a[i]%2==0 SCE=SEC+1
i. Else SCO=SCO+1
step 5. end if
step 6. end while
step 7. display SCE and SCO
step 8. end stop and exit

5. Each student of a class of 30 students takes 6 test in which scores ranges from 0
to 100 .Suppose the test Scores are stored in 30*6 array test. Find the average
grade for each test
Sol:- {array ar[30][6],item=0,sum=0,avg(average)=0}
step 1. Start
step 2. Set ar[30][6]
step 3. Repeat step 3 and 4 for i 1 to 30
step 4. Read marks for student i :
i. Repeat step (i-ii-iii) For j 1 to 6
ii. Read marks of the test j : i.e. ar[i][j]=item
iii. End for
step 5. End for
step 6. Repeat step 6 and 7 for i 1 to 30
step 7. Display marks for student i :
i. Repeat step (i-ii-iii) For j 1 to 6
ii. display marks of the test j : i.e. ar[i][j]=item
iii. if ar[i]j]<40 display GRADE:F
iv. else if ar[i][j]>=40 && ar[i][j]<60 display GRADE:D
v. else if ar[i][j]>=60 && ar[i][j]<70 display GRADE:C
vi. else if ar[i][j]>=70 && ar[i][j]<80 display GRADE:B
vii. else if ar[i][j]>=80 && ar[i][j]<90 display GRADE:A
viii. else if ar[i][j]>=90 && ar[i][j]<100 display GRADE:A+
ix. end if
x. End for
step 8. End for
step 9. Stop and exit
6. An array A contain 20 positive integers .Write algorithm which find the number
of elements of A which divisible by 2.
Sol:-

[SCD(sequence counter of divisible by 2 no.)]


step 1. start
step 2. Set a[20] read values and set SCD=0
step 3. repeat step 4 while i is 1 to 20
step 4. if a[i]%2==0 SCD=SCD+1
step 5. end if
step 6. end while
step 7. display SCD
step 8. end stop and exit

Вам также может понравиться