You are on page 1of 8

CHINHOYI UNIVERSITY OF TECHNOLOGY

SCHOOL OF ENGINEERING SCIENCE AND TECHNOLOGY

DEPARTMENT OF ICT AND ELECTRONICS

COURSE: DATA STRUCTURES AND ALGORITHMS (assignment one)


CODE: CUIT205
REG NUMBER: C21147146V
NAME: TARANIKE EMMANUEL
DUE DATE: 11 APRIL 2023
Question 1

Def sort_array_0s_1s_2s(A,n);
i = 0;
countzero=0;
countone=0;
counttwo=0;

while(i < n)
{
if(A[i]==0)
{
countzero=countzero + 1;
}
elif(A[i]==1)
{
countone=countone + 1;
}
else{ counttwo=counttwo + 1; }
i = i + 1;
}

i = 0;
while(i < countzero)
{
A[i]=0;
i = i +1;
i = countzero;
}
while(i < countzero + countone)
{
A[i]=1;
i = i + 1;
i = countzero + countone;
}
while(i < n)
{
A[i]=2
i = i + 1;
}

print(“Enter size of array: “);


n = int(input());
print(“Enter array elements: \n”);
i = 0;
A = []
while(i < n)
{
ele = int(input());
A.append(ele);
i = i +1;
}
Print(“Array after sorting: \n”);
Sort_array_0s_1s_2s(A,n)
i = 0;

for i in A { print(i); }

QUESTION 2
Answer:
a) RDBMS- Array of structures
An array is a type of data structure that consists of a set of identically sized elements (values or
variables), each of which is identifiable by an array index or key. An array is stored in a way that
allows a mathematical formula to determine each element's position given its index tuple.
Network Data Model- Graphs
The non-linear data structure known as a graph consists of vertices and edges. The edges of a graph
are the lines or arcs that connect any two nodes, and the vertices of a graph are also frequently
referred to as nodes.
Hierarchical data model- Trees
To describe and arrange data in a way that is simple to explore and find, a tree data structure is
utilized. The nodes are arranged in a hierarchical fashion and are connected to one another by edges.
The nodes that are located beneath the root node are known as the child nodes, and the root node is
the uppermost node of the tree. A recursive structure is created when each node has numerous child
nodes, each of which has the ability to have more children of its own.
b) The lack of merge sort results from the need for more RAM to store the auxiliary arrays. The rapid
sort, however, is in use because it doesn't need any more storage. In situations involving bigger array
sizes or datasets, merge sort is more effective and operates more quickly than quick sort. Whereas,
when an array or dataset is smaller, Quick sort is more effective and completes the task faster than
merge sort. Quick sort worst time complexity is O (n2) whereas, merge sort worst time complexity is
O (n log n).
b) The type of data structure appropriate for this database would be the RDBMS. A relational
database management system (RDBMS) is a collection of programs and capabilities that enable IT
teams and others to create, update, administer and otherwise interact with a relational database.
RDBMSes store data in the form of tables, with most commercial relational database management
systems using Structured Query Language (SQL) to access the database. It would be best to use in this
scenario because it is flexible updating data efficiently, maintenance and control is easy for
administrators. The table format in RDBMS is easy to understand and provides an organized and
structural manner through which entries are matched by firing queries.

QUESTION 3
Answer:
i) Shell Sort
8 12 3 17 20 1 4 15
8 12 3 17 20 1 4 15
8 1 3 17 20 12 4 15
8 1 3 17 20 12 4 15
8 1 3 15 20 12 4 17

3 1 8 15 20 12 4 17
3 1 8 15 20 12 4 17
3 1 8 15 20 12 4 17
3 1 8 15 20 12 4 17

1 3 4 12 8 15 20 17
1 3 4 12 8 15 20 17
1 3 4 8 12 15 20 17
1 3 4 8 12 15 20 17
1 3 4 8 12 15 20 17
Final answer 1 3 4 8 12 15 17 20

ii) Selection Sort


1 12 3 17 20 8 4 15
1 3 12 17 20 8 4 15
1 3 4 17 20 8 12 15
1 3 4 8 20 17 12 15
1 3 4 8 12 17 20 15
1 3 4 8 12 15 20 17
Final answer 1 3 4 8 12 15 17 20

Insertion sort

8 12 3 17 20 1 4 15
8 12 3 17 20 1 4 15
8 3 12 17 20 1 4 15
3 8 12 17 20 1 4 15
3 8 12 17 20 1 4 15
3 8 12 17 1 20 4 15
3 8 12 1 17 20 4 15
3 8 1 12 17 20 4 15
3 1 8 12 17 20 4 15
1 3 8 12 17 20 4 15
1 3 8 12 17 4 20 15
1 3 8 12 4 17 20 15
1 3 8 4 12 17 20 15
1 3 4 8 12 17 20 15
1 3 4 8 12 17 15 20
1 3 4 8 12 15 17 20

b. A program that implements a stack


stack = [ ]
stack.append(‘9’)
stack.append(‘3’)
stack.append(‘4’)

print(‘Element at the top of the stack: 9’)


print(stack)

print(‘\nElemens: ‘)
print(stack.pop())
print(stack.pop())
print(stack.pop())
print(‘Stack full : false’)
print(‘Stack empty: true’)

QUESTION 4

a)
Import calendar
From datetime import date
#ask user for birth day, month and year
Birthmonth= int(input(What is your birthmonth(1-12?))
Birthday= int(input(What is your birthday(1-12?))
Birthyear= int(input(What is your birthyear(1-12?))
#Display calendar for birth month
Print (calendar.month(birthyear, birthmonth))
#calculate age
today= date.today()
age = today.year- birthyear – ((today.month, today.day) < (birthmonth, birthday))
# Display birthdate and age
Print (“You were born on {}/{}/{}, You are {} years old”.format(birthmonth, birthday,birthyear,
age))
b)
nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
print("Fibonacci 5th term:", fib(5))
print("Fibonacci 6th term:", fib(6))
print("Fibonacci 7th term:", fib(7))

You might also like