0% found this document useful (0 votes)
10 views58 pages

Pyhon Module 5

The document covers array techniques in programming, including operations such as reversal, counting, finding maximum values, and removing duplicates. It provides examples of Python list operations, including methods like append, pop, and sort, as well as techniques for reversing arrays using slicing and built-in methods. Additionally, it discusses the use of the Array module and NumPy for handling arrays in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views58 pages

Pyhon Module 5

The document covers array techniques in programming, including operations such as reversal, counting, finding maximum values, and removing duplicates. It provides examples of Python list operations, including methods like append, pop, and sort, as well as techniques for reversing arrays using slicing and built-in methods. Additionally, it discusses the use of the Array module and NumPy for handling arrays in Python.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

CSE1021

Introduction to Problem Solving and Programming

School of Computing Science and Engineering


VIT Bhopal University

Unit 5 Array Techniques / 1


Unit - V Array Techniques
Introduction – Array Order Reversal – Array Counting – Finding
the Maximum number in a set – Removal of Duplicates from an
ordered array – Partitioning an array – Finding the Kth smallest
element.

Python Lists: List operations – Tuples – Sets Operations –


Dictionaries – Time Tradeoff.

Unit 5 Array Techniques / 2


Introduction to Array Techniques
• Array is a container which can hold a fix number of items and these items should be
of the same type. Most of the data structures make use of arrays to implement their
algorithms. Following are the important terms to understand the concept of Array.
• Element− Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index, which is used
to identi
Array Representation
• Arrays can be declared in various ways in different languages. Below is an illustration.

Unit 5 Array Techniques / 3


Introduction to Array Techniques
• An array is a special variable, which can hold more than one value at a time.
cars = ["Ford", "Volvo", "BMW"]
print(cars)
• If you have a list of items (a list of car names, for example), storing the cars in single
variables could look like this:
• car1 = "Ford"
car2 = "Volvo"
car3 = "BMW“

Unit 5 Array Techniques / 4


Introduction to Array Techniques
• Access the Elements of an Array
• You refer to an array element by referring to the index number.
• cars = ["Ford", "Volvo", "BMW"]
• x = cars[0]
• print(x) #Ford

• Modify the value of the first array item:


• cars = ["Ford", "Volvo", "BMW"]
• cars[0] = "Toyota“
• print(cars) #['Toyota', 'Volvo', 'BMW']

Unit 5 Array Techniques / 5


Introduction to Array Techniques
• The Length of an Array
• Use the len() method to return the length of an array (the number of elements in an
array).
• cars = ["Ford", "Volvo", "BMW"]
• x = len(cars)
• print(x) #3

Note: The length of an array is always one more than the highest array index.

Unit 5 Array Techniques / 6


Introduction to Array Techniques
• Looping Array Elements
• You can use the for in loop to loop through all the elements of an array.
• Example
• Print each item in the cars array:
cars = ["Ford", "Volvo", "BMW"]
for x in cars:
print(x)

Unit 5 Array Techniques / 7


Introduction to Array Techniques
• Adding Array Elements
• You can use the append() method to add an element to an array.
• Example
• Add one more element to the cars array:
Example:
cars = ["Ford", "Volvo", "BMW"]
cars.append("Honda")
print(cars) # ['Ford', 'Volvo', 'BMW', 'Honda']

Example:
cars = ["Ford", "Volvo", "BMW"]
cars[3] = "Toyota“
print(cars) #[' Ford ', 'Volvo', 'BMW‘, ‘Toyota’]
Unit 5 Array Techniques / 8
Introduction to Array Techniques
• Removing Array Elements
• You can use the pop() method to remove an element from the array.
• Example
• Delete the second element of the cars array:
cars = ["Ford", "Volvo", "BMW"]
cars.pop(1)
print(cars) # ['Ford', 'BMW']

• You can also use the remove() method to remove an element from the array.
• Delete the element that has the value "Volvo":
cars = ["Ford", "Volvo", "BMW"]
cars.remove("Volvo")
print(cars) # ['Ford', 'BMW']
Unit 5 Array Techniques / 9
Array Methods
• Python has a set of built-in methods that you can use on lists/arrays.
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Unit 5 Array Techniques / 10
Unit 5 Array Techniques / 11
Introduction to Array Techniques
• Python List append() Method
• Example
• Add an element to the fruits list:
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits) # ['apple', 'banana', 'cherry', 'orange']
----
a = ["apple", "banana", "cherry"]
b = ["Ford", "BMW", "Volvo"]
a.append(b)
print(a) # ['apple', 'banana', 'cherry', ["Ford", "BMW", "Volvo"]]

Unit 5 Array Techniques / 12


Introduction to Array Techniques
• Python List clear() Method
• Example
• Remove all elements from the fruits list:
fruits = ["apple", "banana", "cherry"]
fruits.clear()
print(fruits) # []

Unit 5 Array Techniques / 13


Introduction to Array Techniques
• Python List copy() Method
• Example
• Copy the fruits list:
fruits = ["apple", "banana", "cherry"]
x = fruits.copy()
print(x) # ['apple', 'banana', 'cherry']

Unit 5 Array Techniques / 14


Introduction to Array Techniques
• Python List count() Method
• Example
• Return the number of times the value "cherry" appears in the fruits list:
fruits = ["apple", "banana", "cherry","cherry","cherry"]
x = fruits.count("cherry")
print(x) #3

• The count() method returns the number of elements with the specified value.
• Return the number of times the value 9 appears int the list:
fruits = [1, 4, 2, 9, 7, 8, 9, 3, 1]
x = fruits.count(9)
print(x) #2

Unit 5 Array Techniques / 15


Introduction to Array Techniques
• Python List extend() Method
• Add the elements of cars to the fruits list:
fruits = ['apple', 'banana', 'cherry']
cars = ['Ford', 'BMW', 'Volvo']
fruits.extend(cars)
print(fruits) # ['apple', 'banana', 'cherry', 'Ford', 'BMW', 'Volvo']
----
fruits = ['apple', 'banana', 'cherry']
points = (1, 4, 5, 9)
fruits.extend(points)
print(fruits) #['apple', 'banana', 'cherry', 1, 4, 5, 9]

Unit 5 Array Techniques / 16


Introduction to Array Techniques
• Python List index() Method
• What is the position of the value "cherry":
fruits = ['apple', 'banana', 'cherry']
x = fruits.index("cherry")
print(x) #2

fruits = [4, 55, 64, 32, 16, 32]


x = fruits.index(32)
print(x) #3

Note: The index() method only returns the first occurrence of the value.

Unit 5 Array Techniques / 17


Introduction to Array Techniques
• Python List insert() Method
• Insert the value "orange" as the second element of the fruit list:
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "orange")
print(fruits) # ['apple', 'orange', 'banana', 'cherry']

Unit 5 Array Techniques / 18


Introduction to Array Techniques
• Python List pop() Method
• The pop() method removes the element at the specified position.
• Remove the second element of the fruit list:
fruits = ['apple', 'banana', 'cherry']
fruits.pop(1)
print(fruits) # ['apple', 'cherry']
-----
fruits = ['apple', 'banana', 'cherry']
x = fruits.pop(1)
print(x) # banana
Note: The pop() method returns removed value.

Unit 5 Array Techniques / 19


Introduction to Array Techniques
• Python List remove() Method
• The remove() method removes the first occurrence of the element with the specified
value.
• Remove the "banana" element of the fruit list:
fruits = ['apple', 'banana', 'cherry']
fruits.remove("banana")
print(fruits) # banana

Unit 5 Array Techniques / 20


Introduction to Array Techniques
• Python List reverse() Method
• The reverse() method reverses the sorting order of the elements.
• Reverse the order of the fruit list:
fruits = ['apple', 'banana', 'cherry']
fruits.reverse()
print(fruits) #['cherry', 'banana', 'apple']

Unit 5 Array Techniques / 21


Introduction to Array Techniques
• Python List sort() Method
• The sort() method sorts the list ascending by default.
• Sort the list alphabetically:
cars = ['Ford', 'BMW', 'Volvo']
cars.sort()
print(cars) # ['BMW', 'Ford', 'Volvo']

Sort the list descending:


cars = ['Ford', 'BMW', 'Volvo']
cars.sort(reverse=True)
print(cars) # ['Volvo', 'Ford', 'BMW']

Unit 5 Array Techniques / 22


Array Order Reversal
• 1. Using List Slicing to Reverse an Array in Python
• We can reverse a list array using slicing methods. In this way, we actually create a
new list in the reverse order as that of the original one. Let us see how:
• #The original array
arr = [11, 22, 33, 44, 55]
print("Array is :",arr)
res = arr[::-1] #reversing using list slicing
print("Resultant new reversed array:",res)
• Output:
• Array is : [1, 2, 3, 4, 5]
• Resultant new reversed array: [5, 4, 3, 2, 1]

Unit 5 Array Techniques / 23


Array Order Reversal
• 2. Using reverse() Method
• Python also provides a built-in method reverse() that directly reverses the order of
list items right at the original place.
• Note: In this way, we change the order of the actual list. Hence, the original order is
lost.
• #The original array
arr = [11, 22, 33, 44, 55]
print("Before reversal Array is :",arr)
arr.reverse() #reversing using reverse()
print("After reversing Array:",arr)
• Output:
• Before reversal Array is : [11, 22, 33, 44, 55]
• After reversing Array: [55, 44, 33, 22, 11]
Unit 5 Array Techniques / 24
Array Order Reversal
• 3. Using reversed() Method
• We have yet another method, reversed() which when passed with a list returns an
iterable having just items of the list in reverse order. If we use the list() method on
this iterable object, we get a new list which contains our reversed array.
• #The original array
arr = [12, 34, 56, 78]
print("Original Array is :",arr)
#reversing using reversed()
result=list(reversed(arr))
print("Resultant new reversed Array:",result)
• Output:
• Original Array is : [12, 34, 56, 78]
• Resultant new reversed Array: [78, 56, 34, 12]
Unit 5 Array Techniques / 25
Array Order Reversal
• Reversing an Array of Array Module in Python
• Even though Python doesn’t support arrays, we can use the Array module to create
array-like objects of different data types. Though this module enforces a lot of
restrictions when it comes to the array’s data type, it is widely used to work with
array data structures in Python.
• Now, let us see how we can reverse an array in Python created with the Array
module.

Unit 5 Array Techniques / 26


Array Order Reversal
• 1. Using reverse() Method
• Similar to lists, the reverse() method can also be used to directly reverse an array in
Python of the Array module. It reverses an array at its original location, hence
doesn’t require extra space for storing the results.
import array
#The original array
new_arr=array.array('i',[2,4,6,8,10,12])
print("Original Array is :",new_arr)
#reversing using reverse()
new_arr.reverse()
print("Reversed Array:",new_arr)
• Output:
• Original Array is : array('i', [2, 4, 6, 8, 10, 12])
• Resultant new reversed Array: array('i', [12,
Unit 5 Array 10, 8,
Techniques / 6, 4, 2]) 27
Array Order Reversal
• 2. Using reversed() Method
• Again, the reversed() method when passed with an array, returns an iterable with
elements in reverse order. Look at the example below, it shows how we can reverse
an array using this method.
import array
#The original array
new_arr=array.array('i',[10,20,30,40])
print("Original Array is :",new_arr)
#reversing using reversed()
res_arr=array.array('i',reversed(new_arr))
print("Resultant Reversed Array:",res_arr)
• Output:
• Original Array is : array('i', [10, 20, 30, 40])
• Resultant Reversed Array: array('i', [40, 30,Techniques
Unit 5 Array 20, 10]) / 28
Array Order Reversal
• Reversing a NumPy Array in Python
• The Numpy module allows us to use array data structures in Python which are
really fast and only allow same data type arrays.
• Here, we are going to reverse an array in Python built with the NumPy module.
• 1. Using flip() Method
• The flip() method in the NumPy module reverses the order of a NumPy array and
returns the NumPy array object.
import numpy as np
#The original NumPy array
new_arr=np.array(['A','s','k','P','y','t','h','o','n'])
print("Original Array is :",new_arr) Output:
#reversing using flip() Method Original Array is : ['A' 's' 'k' 'P' 'y' 't' 'h' 'o' 'n']
Resultant Reversed Array: ['n' 'o' 'h' 't' 'y' 'P' 'k' 's' 'A']
res_arr=np.flip(new_arr)
print("Resultant Reversed Array:",res_arr)
Unit 5 Array Techniques / 29
Array Order Reversal
• 3. Using Simple Slicing
• As we did earlier with lists, we can reverse an array in Python built with Numpy
using slicing. We create a new NumPy array object which holds items in a reversed
order.
import numpy as np
#The original NumPy array
new_arr=np.array([1,3,5,7,9])
print("Original Array is :",new_arr)
#reversing using array slicing
res_arr=new_arr[::-1]
print("Resultant Reversed Array:",res_arr)
• Output:
• Original Array is : [1 3 5 7 9]
• Resultant Reversed Array: [9 7 5 3 1]Unit 5 Array Techniques / 30
Array Order Reversal
• 2. Using flipud() Method
• The flipud() method is yet another method in the NumPy module which flips an array
up/down. It can also be used to reverse a NumPy array in Python. Let us see how we
can use it in a small example.
import numpy as np
#The original NumPy array
new_arr=np.array(['A','s','k','P','y','t','h','o','n'])
print("Original Array is :",new_arr)
#reversing using flipud() Method
res_arr=np.flipud(new_arr)
print("Resultant Reversed Array:",res_arr)
• Output:
• Original Array is : ['A' 's' 'k' 'P' 'y' 't' 'h' 'o' 'n']
• Resultant Reversed Array: ['n' 'o' 'h'Unit 't'5'y'
Array'P' 'k' 's'/ 'A']
Techniques 31
Array Counting
• Python List count()
• The count() method returns the number of times the specified element appears in
the list.
• Example
# create a list
numbers = [2, 3, 5, 2, 11, 2, 7]
# check the count of 2
count = numbers.count(2)
print('Count of 2:', count)
# Output: Count of 2: 3
• Return value from count()
• The count() method returns the number of times element appears in the list.

Unit 5 Array Techniques / 32


Array Counting
Example 1: Use of count()
# vowels list
vowels = ['a', 'e', 'i', 'o', 'i', 'u']
# count element 'i'
count = vowels.count('i')
# print count
print('The count of i is:', count) Output
The count of i is: 2
# count element 'p'
The count of p is: 0
count = vowels.count('p')
# print count
print('The count of p is:', count)

Unit 5 Array Techniques / 33


Array Counting
Example 2: Count Tuple and List Elements Inside List
# random list
random = ['a', ('a', 'b'), ('a', 'b'), [3, 4]]
# count element ('a', 'b')
count = random.count(('a', 'b'))
# print count
print("The count of ('a', 'b') is:", count) Output
The count of ('a', 'b') is: 2
# count element [3, 4]
The count of [3, 4] is: 1
count = random.count([3, 4])
# print count
print("The count of [3, 4] is:", count)

Unit 5 Array Techniques / 34


Finding the Maximum number in a set
• Sets are used to store multiple items in a single variable.
• Set is one of 4 built-in data types in Python used to store collections of data, the other 3
are List, Tuple and Dictionary, all with different qualities and usage.
• A set is a collection which is unordered, unchangeable*, and unindexed.
• * Note: Set items are unchangeable, but you can remove items and add new items.

• Sets are written with curly brackets.


• Example
• Create a Set:
thisset = {"apple", "banana", "cherry"}
print(thisset) # {'apple', 'cherry', 'banana'}
• # Note: the set list is unordered, meaning: the items will appear in a random order.
• # change in the result next time run same code.

Unit 5 Array Techniques / 35


Finding the Maximum number in a set
• Duplicates Not Allowed
• Sets cannot have two items with the same value.
• Duplicate values will be ignored:
• thisset = {"apple", "banana", "cherry", "apple"}
• print(thisset) #{'banana', 'cherry', 'apple'}

• Get the Length of a Set


• To determine how many items a set has, use the len() method.
• Get the number of items in a set:
• thisset = {"apple", "banana", "cherry"}
• print(len(thisset)) #3

Unit 5 Array Techniques / 36


Finding the Maximum number in a set
• Set Items - Data Types
• Set items can be of any data type:
• String, int and boolean data types:
set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
Output
print(set1)
{'cherry', 'apple', 'banana'}
print(set2) {1, 3, 5, 7, 9}
print(set3) {False, True}

Unit 5 Array Techniques / 37


Finding the Maximum number in a set
• A set can contain different data types:
• A set with strings, integers and boolean values:
• set1 = {"abc", 34, True, 40, "male"} Output
• print(set1) {True, 34, 40, 'male', 'abc'}

• More details
• https://www.w3schools.com/python/python_sets.asp

Unit 5 Array Techniques / 38


Finding the Maximum number in a set
• Write a Python program to find maximum and the minimum value in a set.
• Python Code:
#Create a set
setn = {5, 10, 3, 15, 2, 20}
print("Original set elements:")
print(setn)
print(type(setn)) Sample Output:
Original set elements:
print("\nMaximum value of the said set:") {2, 3, 5, 10, 15, 20}
print(max(setn)) <class 'set'>
Maximum value of the said set: 20
print("\nMinimum value of the said set:") Minimum value of the said set: 2
print(min(setn))

Unit 5 Array Techniques / 39


Removal of Duplicates from an ordered array
• Given a sorted array, the task is to remove the duplicate elements from the array.
Examples:

• Input : arr[] = {2, 2, 2, 2, 2}


• Output : arr[] = {2}
• new size = 1

• Input : arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5}


• Output : arr[] = {1, 2, 3, 4, 5}
• new size = 5

• https://www.geeksforgeeks.org/remove-duplicates-sorted-array/

Unit 5 Array Techniques / 40


Removal of Duplicates from an ordered array
• Algorithm
• Create an auxiliary array temp[] to store unique elements.
• Traverse input array and one by one copy unique elements of arr[] to temp[]. Also
keep track of count of unique elements. Let this count be j.
• Copy j elements from temp[] to arr[] and return j

Unit 5 Array Techniques / 41


Removal of Duplicates from an ordered array
# Python3 program to remove duplicates Function to remove duplicate elements
# This function returns new size of modified array.

def removeDuplicates(arr, n):


# Return, if array is empty or contains a single element
if n == 0 or n == 1:
return n
temp = list(range(n))
# Start traversing elements
j = 0;
for i in range(0, n-1):
# If current element is not equal to next element then store that current element
if arr[i] != arr[i+1]:
temp[j] = arr[i]
j += 1
# Store the last element as whether it is unique or repeated, it hasn't stored previously
temp[j] = arr[n-1]
j += 1
Unit 5 Array Techniques / 42
Removal of Duplicates from an ordered array
# Modify original array
for i in range(0, j):
arr[i] = temp[i]
return j
# Driver code
arr = [1, 2, 2, 3, 4, 4, 4, 5, 5]
n = len(arr)
# removeDuplicates() returns new size of array.
n = removeDuplicates(arr, n)
# Print updated array
for i in range(n):
print ("%d"%(arr[i]), end = " ")

Output:
1 2 3 4 5

Unit 5 Array Techniques / 43


Finding the Kth smallest element
• Given an array and a number k where k is smaller than the size of the array, we need to find the k’th
smallest element in the given array. It is given that all array elements are distinct.

# Python3 program to find k'th smallest element


# Function to return k'th smallest element in a given array
def kthSmallest(arr, n, k):
if __name__=='__main__':
# Sort the given array
arr = [12, 3, 5, 7, 19]
arr.sort()
n = len(arr)
# Return k'th element in the k=2
# sorted array print("K'th smallest element is",
return arr[k-1] kthSmallest(arr, n, k))
# Driver code
# This code is contributed by
Output # Shrikant13
K'th smallest element is 5

Unit 5 Array Techniques / 44


Chapter 5: Syllabus
Array Techniques: Introduction Python Lists: List operations
– Array Order Reversal – Tuples
– Array Counting – Sets Operations
– Finding the Maximum number in a set – Dictionaries
– Removal of Duplicates from an ordered array – Time Tradeoff.
– Partitioning an array
– Finding the Kth smallest element
Python List: List Operations
Method Description

append() Adds an element at the end of the list

clear() Removes all the elements from the list

copy() Returns a copy of the list

count() Returns the number of elements with the specified value

extend() Add the elements of a list (or any iterable), to the end of the current list

index() Returns the index of the first element with the specified value

insert() Adds an element at the specified position

pop() Removes the element at the specified position

remove() Removes the first item with the specified value

reverse() Reverses the order of the list

sort() Sorts the list


Unit 5 Array Techniques / 47
Some Examples:
Tuple Operations

Method Description

count() Returns the number of times a specified value occurs in a tuple

index() Searches the tuple for a specified value and returns the position of
where it was found
Set Operations
Method Description

add() Adds an element to the set

clear() Removes all the elements from the set

copy() Returns a copy of the set

difference() Returns a set containing the difference between two or more sets

difference_update() Removes the items in this set that are also included in another, specified set

discard() Remove the specified item

intersection() Returns a set, that is the intersection of two or more sets

intersection_update() Removes the items in this set that are not present in other, specified set(s)

isdisjoint() Returns whether two sets have a intersection or not


Set Operations
Method Description

issubset() Returns whether another set contains this set or not

issuperset() Returns whether this set contains another set or not

pop() Removes an element from the set

remove() Removes the specified element

symmetric_difference() Returns a set with the symmetric differences of two sets

symmetric_difference_update() inserts the symmetric differences from this set and another

union() Return a set containing the union of sets

update() Update the set with another set, or any other iterable
Dictionary Operations
Method Description

clear() Removes all the elements from the dictionary

copy() Returns a copy of the dictionary

fromkeys() Returns a dictionary with the specified keys and value

get() Returns the value of the specified key

items() Returns a list containing a tuple for each key value pair

keys() Returns a list containing the dictionary's keys

pop() Removes the element with the specified key

popitem() Removes the last inserted key-value pair

setdefault() Returns the value of the specified key. If the key does not exist: insert the key,
with the specified value

update() Updates the dictionary with the specified key-value pairs

values() Returns a list of all the values in the dictionary


String Operations
Method Description

capitalize() Converts the first character to uppercase

casefold() Converts string into lower case

center() Returns a centred string

count() Returns the number of times a specified value occurs in a string

islower() Returns True if all characters in the string are lower case

isupper() Returns True if all characters in the string are upper case

endswith() Returns true if the string ends with the specified value

expandtabs() Sets the tab size of the string

find() Searches the string for a specified value and returns the position of where it
was found
String Operations
Method Description

format() Formats specified values in a string

format_map() Formats specified values in a string

join() Converts the elements of an iterable into a string

lower() Converts a string into lower case

upper() Converts a string into upper case

title() Converts the first character of each word to upper case

replace() Returns a string where a specified value is replaced with a specified value

split() Splits the string at the specified separator, and returns a list

split() Splits the string at the specified separator, and returns a list
Some Examples
Time Tradeoff
Sometimes the choice of an algorithm that involves a space-time tradeoff. That is by increasing the amount
of space for storing the data, we may be able to reduce the time needed for processing data or vice-versa.
Basically it is a way of solving problems in:
1. Less time by using more memory
2. Solving a problem in little space by spending a long time.

TYPE OF TIME SPACE TRADE OFF:


1. Compressed / Uncompressed data
2. Re -Rendering / Sorted image
3. Smaller code / Loop Unrolling
4. Lookup table / Recalculation
Time Tradeoff Cont.
1. Compressed / Uncompressed data:
● A space time trade off can be applied to the problem of solving data storage. If data is uncompressed it
take more space but less time.
● If data is compressed it takes less space but more time to run decompressed data.

1. Re -Rendering / Sorted image:


● Sorting only the source and rendering an image every time a page is requested would be trading time
for space.
● More time used but less space.
● Sorting the image would be trade space for time.
● More space used but less time.
Time Tradeoff Cont.
3. Smaller code(with loop) / Larger code(without loop):
● Smaller code occupies less space in memory but it require high computation time which is required for
jumping back to the beginning of the loop at the end of each iteration.
● Larger code or loop unrolling can be trade of higher programming speed. It occupies more space in
memory but require less computation time.

4. Lookup Table/ Recalculation:


● In lookup table an implementation can include the entire table which reduce computing time but
increase the amount of memory needed.
● It can recalculate i.e. compute table entire as needed, increasing computing time but reducing memory
requirement.

You might also like