F453 - data structures and manipulation Flashcards
Showing the steps of serial search
e.g. Find York
Aberdeen, Belfast, Cardiff, Oxford, York
- start at ‘Aberdeen’…
- look at each word in turn/then ‘Belfast’, ‘Cardiff’ etc…
- …until ‘York’ is found
Showing the steps of binary search e.g. Find York
Aberdeen,Belfast, Cardiff, Oxford, York
look at middle/‘Cardiff’/‘Glasgow’
‘York’ is in second half of list
repeated halving…
…until ‘York’ is found
What are the advantages of binary search over seial search?
(usually) faster because…
…alf of data is discarded at each step/fewer item
How do you add a data item into a stack?
if stack is full…
…report error and stop
increment pointer
add data item at position ‘pointer
How can quicksort be used to put a set of numbers in ascending order?
e.g. 30 9 46 14 22
highlight first number in the list (the ‘search number’)
pointer at each end of list
repeat:
compare numbers being pointed to…
…f in wrong order, swap
move pointer of non-search number
until pointers coincide so search number in correct position
split list into 2 sublists
quick sort each sublist
repeat until all sublists have a single number
put sublists back together
Name another method or sorting
insertion sort or bubble sort
What is a static data structure?
size is fixed when structure is created/size cannot change
during processing
What would be an advantage of static data structures over dynamic structures?
amount of storage is known/easier to program
How would you merge two files?
e.g. File A: Anna, Cleo,Helen, Pretti
FiLe B: Billy, Ian, Omar, Rob, Tom
(Anna, Billy, Cleo, Helen, Ian, Omar, Pritti, Rob, Tom)
You must: get correct order, use all names used once
State the algorithm for merging two sorted files
open existing files create new file check existing files are not empty use pointers/counters to identify records for comparison repeat compare records indicated by pointers copy earlier value record to new file move correct pointer until end of one file copy remaining records from other file close files assume common key assume if 2 records are the same… …only 1 is written to new file
What is a dynamic data structure?
size changes as data is added & removed/size is not fixed
What would be a disadvantage to the programer of using dynamic data structures
over static ones?
more complex program to write
State a data structure which must be static
array/fixed length record
What steps need to be taken to add a new item to a binary tree
start at root repeat compare new data with current data if new data < current data, follow left pointer else follow right pointer until pointer is null write new data create (null) pointers for new data
How can insertion sort be used to arrange numbers in order?
e.g. 17 2 3 26 5
list of sorted numbers is built up…
…with one number at a time being inserted into correct position