Data types, data structures and algorithms Flashcards
What is a string?
A collection of characters
1 nybble=
1 nybble = 4 bits
The least significant bit of a binary number is the one furthest to the…
right
BINARY ADDITION
0+0+0=
0+0+1=
1+1+0=
1+1+1=
0+0+0=0
0+0+1=1
1+1+0= 10 (0 carry 1)
1+1+1= 11 (1 carry 1)
Explain sign magnitude
A way of representing negative numbers in binary
Ignore most sig bit and convert remaining bits to decimal then change the sign
e.g 10101101 =173
1 10101101 = -173 (1 for negative)
0 10101101 = +173 (0 for positive)
Explain two’s compliment
Its a way of representing negative numbers in binary
This is done by making the most significant bit negative
eg 7 = 00000111
In two’s complement
-7=-128+64+32+16+8+1
=11111001
What is an easy way of converting a number to two’s complement?
Flip the bits in the positive version and adding one
eg 7 = 00000111
In two’s complement
=11111000 then add one
=11111001
Number bases
Decimal = base 10
Binary = base 2
Hexadecimal = base 16
HEX conversion values
0-9 = 0-9
A-F = 10-15
Convert hex 4E7F into decimal
16^3 : 4 (4X4096)
16^2 : E (14X256)
16^1 : 7 (7X16)
16^0 : F (15X1)
(4X4096) + (14X256) + (7X16) + (1X15)= 20095
Convert hex B2 into decimal
16x11 =B
1X2= 2
(16X11) + (1X2) 178
OR
B=11 2=2
11=1011 2=0010
COMBINE: 10110010
10110010=178
Can be split into 2 parts mantissa and exponent
What is this?
Floating point binary numbers
Why does floating point binary need to be normalised?
To make sure they are as precise as possible in a given number of bits
(meaning making use of as much mantissa as possible)
To normalise number make sure it starts with:
01 for positive num
10 for negative num
What is meant by a character set?
A published collection of characters and corresponding codes which can be used by computers for respresenting text
2 examples of character sets are ASCII and Unicode
Give examples of character sets
ASCII and Unicode
ASCII
Example of a character set
-Uses 7 bits (2^7= 128 characters)
-Capital A-Z=65-90
-Lower a-z= 97-122
-Also has codes to represent other symbols and numbers
-Can only represent characters in English language
UNICODE
-Uses a varying number of bits
-Better than ASCII as it can represent more characters meaning that it can be used for more different languages rather than just English
-Can represent letters, numbers, symbols, emojis….
What is an array?
An ordered, finite set of elements of a single type
-Taken to be zero-indexed (first index 0) unless stated in question
Linear array = 1D
What is a record made up of?
Row in a file made up of fields
-eg used in databases
Lists can contain elements of more than one data type
TRUE OR FALSE
TRUE
(Unlike arrays)
What is a list?
A data structure consisting of a number of ordered elements where each element can occur more than once
(ordered because the elements retain the sequence in which they were added until you change them)
A difference between lists and arrays is that the values in a list are stored non-contiguously.
What does this mean?
Non contiguous = The values don’t have to be stored in consecutive memory locations. Each element of a list can be in different places in memory.
Because lists store references (or pointers) to their values, which may be scattered in memory. This flexibility allows lists to grow dynamically and hold elements of different types.
Arrays, on the other hand, store values in contiguous memory locations, which ensures fast access using indexes but requires fixed size memory allocation.
Give ways to represent graphs
-Adjacency matrix
-Adjacency list
Give advantages of using adjacency matrices
Advantages:
Fast Edge Lookup:
-Checking whether an edge exists between two vertices is O(1), as it’s just a direct index access.
Simplicity:
-Easy to implement and understand, especially for dense graphs.
-Efficient for Dense Graphs:
Works well when the number of edges is close to the maximum possible (otherwise wasted memory space as it allocates space in memory for every possible edge in the graph even the ones that dont exist)
Give advantages of using adjacency lists
-Better to work with for small, sparse graphs
-No wasted memory space because it only allocates memory to the edges that exist within the graph (rather than all possible ones like with matrices)
What is meant by sparse graph?
A graph in which the number of edges is relatively SMALL compared to the number of POSSIBLE edges among the vertices.
(small edge to vertex ratio)
LESS EDGES
What is meant by a dense graph?
A graph in which the number of edges is relatively large compared to the number of POSSIBLE edges among the vertices.
(large edge to vertex ratio)
MORE EDGES
What is a stack?
-Last in first out (LIFO) data structure
-Can be implemented as a static or dynamic structure
-Used to reverse an action
-They are implemented using a pointer which points to the top of the stack
What is a queue?
-First in first out (FIFO) data structure
-Can use an array to implement it
-2 POINTERS one at the start and the other at the end of queue
Give a disadvantage of linear queues and why they aren’t effective at implementing queues
They can lead to inefficient memory usage due to the “wasted space” problem
In a linear queue, elements are added at the back and removed from the front . As items are removed, even tho the front of the queue technically moves forward, the space at the front is not reused.
Over time, the queue might reach the end of the allocated memory even if there’s free space at the beginning.
-To combat this: either shifting all elements to the front (which is inefficient) or setting a maximum size that limits the queue’s use ( so that the queue appears full).
So circular queues are preferred, as they allow the queue to wrap around and reuse the memory efficiently.
How do Circular queues work?
FIFO
Get around the ‘waste space’ problem from linear queues:
START
-Front Pointer: The front pointer typically starts at 0, because the first element in the queue will be added at index 0.
-Rear Pointer: The rear pointer also starts at 0 because initially, no items have been added yet. However, after adding the first element (enqueue), the rear pointer moves to 1.
END
-When the rear pointer reaches maxsize - 1, it wraps around to index 0, ensuring that the queue efficiently uses the available space.
What is meant by a subtree?
A subsection of a tree consisting of a parent and all the children of a parent