Chapter 5 Flashcards
What is searching?
Looking through a file to see if a particular piece of data exists
What is sorting?
Putting items of data into a particular order - alphabetical or numerical.
What is bubble sort?
An algorithm to sort an unordered list by comparing adjacent items and swapping them if needed.
How does bubble sort work?
1) Start at the beginning of the list.
2) Compare the first and second values. Swap if necessary.
3) Compare the second and third values. Swap if necessary.
4) Continue working up the list untill the end.
5) If the list still isn’t sorted, start the next pass by going to the beginning.
6) extra pass to check
Advantages of bubble sort (2)
Simplest
easiest to code
Disadvantages of bubble sort (1)
Slowest
What insertion sort?
An algorithm used to sort lists by examining each item in turn and inserting it into its correct position.
How does insertion sort work?
1) If there is only one item in the list then stop
2) Start with the second item and compare it to the first and swap if necessary
3) compare the third and second and swap if necessary. Repeat step 2.
4) Compare the third and fourth and swap if necessary. Repeat step 3.
5) Continue till the last element.
Advantages of insertion sort (3)
Simple
easy to code
Twice as fast as bubble sort
Disadvantages of insertion sort
Slower than merge sort
What is merge sort?
A sorting algorithm which sorts lists bu recursively dividing a list smaller and smaller untill the size of each list is one, and then add them up together while sorting them.
How does merge sort work?
1) if there is only one item in the list then stop
2) Divide the list into 2 parts
3) Recursively divde these lists untill the size of each becomes one
4) Recursively merge the lists with the items in order
Advantages of merge sort (1)
Fastest of the three algorithms
Disadvantages of merge sort (2)
Hardest to code
Harderst on the processor
How does linear search work?
Start at the beginning of the list
move through each element until you find the search criterion
or reach the end of list
Does not need the list to be sorted first