Chapter 5 - Searching & Sorting Flashcards

1
Q

What does sorting also make it easier to do?

A

Search for the data you need. An unsorted list (often) takes much longer to search than a sorted list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is searching?

A

Looking through a file to see if a particular piece of data exists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is sorting?

A

Putting items/records of data into a particular order - alphabetical or numerical.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the stages of bubble sort?

A

1) Start at the beginning of the list
2)Compare the first & second values. Swap if necessary
3)Compare the second & third valves. Swap if necessary
4)Continue working up the list until the end
5)If any swaps were made, return to the beginning and repeat.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How many passes does bubble sort take?

A

the number of passes and 1 more to check

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Code bubble sort part

A

swapInPass = True
while swapInPass == True:
swapInPass = False
for I in range(len(myList)-1):
if myList[i] > myList[I+1]:
temp = myList[I]
myList[I] = myList[I+1]
myList[I+1] = temp
swapInPass = True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What type of graph is a bubble sort graph

A

Exponential

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the stages of insertion sort?

A

1)If there is only item in the list then stop.
2)Start with the second item and compare with the first. Swap if necessary.
3)Compare the third and second. Swap if necessary. Repeat step 2.
4)Compare the fourth and third. Swap if necessary. Repeat step 3
5)Continue until the last element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between bubble and insertion sort?

A

Bubble keeps swapping elements until the list is sorted, whereas insertion sort takes each element and positions it in the best place for it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the code for insertion sort?

A

for i in range(1,len(myList)):
for j in range(I, 0, -1):
if myList[j] < myList[j-1]:
temp = myList[j-1]:
myList[j-1] = myList[j]
myList[j] = temp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How much faster is insertion sort compared to bubble sort for long lists

A

3x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the stages of merge sort?

A

1) If there is only one element in the list then stop
2)Divide the list in half (or as close as you can)
3)Recursively decide each half until the size of each list is one.
4)Merge the lists, assembling the elements into the correct order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is an advantage of Bubble sort?

A

Simplest to code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an advantage of insertion sort?

A

Simple to code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an advantage of merge sort?

A

Fastest of the 3 algorithm to sort long lists

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a disadvantage of Bubble sort?

A

Slowest for long lists

15
Q

What is a disadvantage of Insertion sort?

A

Twice as fast as bubble sort, but slower than merge sort for long lists

16
Q

What is a disadvantage of merge sort?

A

Hardest to code out of the 3

17
Q

What is Linear search?

A

A simple, sequential search. Start at the beginning of the list and move through the elements until you either find the search criteria or reach the end of the list

18
Q

What does linear search not require

A

Does not require the list to be sorted first. If just one search is needed, this can save a lot of time. However, linear sea

19
Q

What is binary search

A

Searches ordered lists only and is an average, much faster than linear search

20
Q

What are the stages of binary search?

A

-Select the middle element and compare to search criteria if it matches, stop
-If it is lower than search criteria, discard left half
-if it is higher than search criteria, discard right half
-Repeat until search criteria is found, or list length is one

21
Q
A