0% found this document useful (0 votes)
5 views

Assignment 1

The document defines key computer science concepts including algorithms, data structures, time complexity, and space complexity. It provides Python code examples for manipulating lists and explains the differences between arrays and linked lists, along with scenarios for their use. Additionally, it analyzes the time complexity of a function that sums elements in an array and presents a program to find the minimum and maximum values in an array.

Uploaded by

nyame3434
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment 1

The document defines key computer science concepts including algorithms, data structures, time complexity, and space complexity. It provides Python code examples for manipulating lists and explains the differences between arrays and linked lists, along with scenarios for their use. Additionally, it analyzes the time complexity of a function that sums elements in an array and presents a program to find the minimum and maximum values in an array.

Uploaded by

nyame3434
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Nyame Nkrumah Emmanuel

Index number:052441340027
Course code:BDS 111
Assignment 1

1. Define the following terms:


a. An algorithm is a well-defined, step-by-step procedure for solving a problem
or achieving a specific goal. It's a set of instructions that can be executed by a
computer or a human to produce a desired output.

b. Data structure:A data structure is a way to organize, store, and manage data
in a computer so that it can be efficiently accessed, modified, and manipulated. It
provides a framework for storing and retrieving data, and enables efficient
operations such as insertion, deletion, searching, and sorting.

C. Time complexity :Time complexity refers to the amount of time an algorithm


takes to complete, usually expressed as a function of the size of the input. It's a
measure of an algorithm's efficiency, typically represented using Big O notation
(e.g., O(n), O(n log n), etc.).

d. Space complexity:Space complexity refers to the amount of memory an


algorithm uses, usually expressed as a function of the size of the input. It's a
measure of an algorithm's memory efficiency, typically represented using Big O
notation (e.g., O(n), O(log n), etc.).

2. Consider the python list:


nuns=[10,20,30,40,50].
Write python code to:
a. Insert 25 at index 2
b. Remove the value 40 from the list
c. Reverse the list

# Initial list
nums = [10, 20, 30, 40, 50]

# a. Insert 25 at index 2
nums.insert(2, 25)
print("After inserting 25 at index 2:", nums)
After inserting 25 at index 2: [10, 20, 25, 30, 40, 50]

# b. Remove the value 40 from the list


nums.remove(40)
print("After removing 40:", nums)
After removing 40: [10, 20, 25, 30, 50]

# c. Reverse the list


nums.reverse()
print("After reversing the list:", nums)
After reversing the list: [50, 30, 25, 20, 10]
3. Explain the difference between a linked list and an array. Provide examples of
scenarios where each would be preferred.

A linked list and an array are both data structures used to store collections of
elements, but they differ in how they store and manage the elements:

Array
- Stores elements in contiguous memory locations.
- Each element is identified by an index or subscript.
- Elements are accessed using their index, allowing for fast and efficient access.
- Insertion or deletion of elements can be slow, as it requires shifting all
subsequent elements.

Linked List
- Stores elements as separate objects, each containing a value and a reference
(i.e., a "link") to the next element.
- Elements are accessed by traversing the list from the beginning, following the
links between elements.
- Insertion or deletion of elements is faster, as only the affected elements and
their links need to be updated.

In summary, arrays offer fast access to elements but slow insertion/deletion, while
linked lists offer fast insertion/deletion but slower access to elements. The choice
between the two depends on the specific requirements of the application.

Here are some examples of scenarios where each data structure would be preferred:

Arrays:
1. Fixed-size data: When the number of elements is fixed and known in advance,
arrays are a good choice. For example, a game with a fixed number of players.
2. Fast access: When fast access to elements is critical, arrays are preferred. For
example, in a database query where you need to quickly retrieve data.
3. Cache efficiency: Arrays are cache-friendly, making them suitable for
applications where cache efficiency is important, such as in scientific simulations.

Linked Lists:

1. Dynamic data: When the number of elements is constantly changing, linked


lists are a better choice. For example, a database of users where new users are
added or removed frequently.
2. Frequent insertions/deletions: When elements are frequently
inserted or deleted, linked lists are more efficient. For example, a browser's
history list where pages are constantly added and removed.
3. Memory efficiency: Linked lists can be more memory-efficient than
arrays, especially when dealing with large amounts of data. For example, a file
system where files are constantly being added and removed.
4.Analyze the following python code snippet and determine it’s time complexity;

def sum_ elements(arr):


total=0
for i in arr:
total+= i
return total

. Time Complexity Analysis


Code Snippet:

def sum_elements(arr):
total = 0
for i in arr:
total += i
return total

Analysis:
The function iterates through each element of the array arr once.
The operation total += i is a constant-time operation (O(1)) for each iteration.
If the array has n elements, the loop runs n times.

Time Complexity: O(n), where n is the size of the input array.

5. Python Program to Find Maximum and Minimum in an Array Without Built-In Functions
Here’s the program:

python code:

def find_min_max(arr):
if not arr: # Check if the array is empty
return None, None

# Initialize min and max with the first element of the array
min_element = arr[0]
max_element = arr[0]

# Iterate through the array


for num in arr:
if num < min_element:
min_element = num
if num > max_element:
max_element = num

return min_element, max_element


# Example usage
array = [3, 1, 7, 9, 2, -5, 8]
minimum, maximum = find_min_max(array)
print("Minimum:", minimum)
print("Maximum:", maximum)
Output:

makefile

Minimum: -5
Maximum: 9
Time Complexity: O(n), as the function iterates through the array once.
Space Complexity: O(1), as no additional

You might also like