Abstraction Flashcards
State what is meant by an abstract data type
Is a collection of data. When we want to use an abstract data type, we need a set of basic operations
List the basic operations that abstract data types have to follow
- Create a new instance of the data structure
- Find an element in the data structure
- Insert a new element into the data structure
- Delete an element from the data structure
- Access all elements stored in the data structure in a systematic manner
Give examples of abstract data types
- Stacks
- Queues
- Linked Lists
- Binary Trees
- Hash Tables
How does a stack work
Items are piled on items on top. The item that is accessible is the one on top of the stack. If we try to find an item in the stack and take it out, we are likely to cause pile to collapse.
The baseofstackpointer will always point to the first slot in the stack.
The topofstackpointer will point to the last element pushed onto the stack.
When an element is removed from the stack, the topofstackpointer will decrease to point to the element now at the top of the stack.
Explain how a queue works
Items added to the queue, get added to the end (endofqueuepointer)
When the item at the front of the queue leaves, we need to move all the other items one step forward. This creates a circular queue where we adjust the frontofqueuepointer
Explain how an insertion sort works
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.
State the advantages of insertion sort
- Simple
- Efficient in practice compared to bubble sort
- Only requires a constant memory space
- Can sort the list as it receives it
State the disadvantages of insertion sort
1.Less efficient for large lists
State how a binary search works
Repeated checking of the middle item in an ordered search list and discarding the half of the list which does not contain the search item
What are the advantages of binary search?
- Faster to search for items than other algorithms
2. Simple algorithm
What are the disadvantages of binary search?
- Not needed for small amount of elements
- Works on lists already sorted
- Lacks efficiency is the list doesn’t have direct access
- Hash lookups are faster