2.1 - sorting Flashcards

1
Q

2 methods of searching

A

Binary search
Linear search

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

3 types of sorting

A

Bubble
Merge
Insertion

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

How does binary search work

A
  1. Set counter to middle of list
  2. If value is a match, end search
  3. If the value at midpoint is smaller than the search value, the lower half is ignored and search continues in upper half
  4. If the value at midpoint is greater than the search value, top half of numbers is ignored and search continues in lower half
  5. Search continues to midpoint of remaining values and 2-4 are related
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the binary search code look like

A

Searchitem = 11
list = [“2”, “3”, “4”, “6”, “8”]
Last = length(list)-1
First = 0

While first <= last and found = false:
Midpoint = (first + last)/2

      If list[midpoint] == searchitem:
                Found = true
     
      Else:
                If searchitem < list[midpoint]:
                           Last = midpoint - 1
                Else:
                            First = midpoint + 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does linear search work

A
  1. Find length of data set
  2. Set counter to 0
  3. Examine value at counter position
  4. If found, end search
  5. If not found, add 1 to counter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Code for linear search

A

Searchitem = 2
Counter = 0
Found = false

While counter < list and found = false:
If counter == searchitem:
Found = true
Else:
Counter = counter + 1

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

Disadvantages of linear search

A

Can take a while as it goes through every value

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

Advantage of linear search

A

Works on any data set wether in order or not

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

How does a bubble sort work

A
  1. Start at beginning of list
  2. Compare the first value in the list with the next one up. If the first value is bigger, swap the positions of the two values
  3. Move to the second value in the list. Again, compare this value with the next and swap if the value is bigger.
  4. Keep going until there are no more items to compare.
  5. Go back to the start of the list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Code for bubble sort

A

counter = 0
swapped = True
swaps = 0
length = list.length

while swapped == True:

 while counter < length-1:
      if list[counter] > list[counter+1]:
            temp = list[counter]
            list[counter] = list[counter+1]
            list[counter+1] = temp
            swaps = swaps + 1
      counter = counter + 1

if swaps == 0 then
     swapped = False
else:
     swaps = 0
     counter = 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does insertion sort work

A

An insertion sort compares values in turn, starting with the second value in the list. If this value is greater than the value to the left of it, no changes are made. Otherwise this value is repeatedly moved left until it meets a value that is less than it. The sort process then starts again with the next value. This continues until the end of the list is reached.

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

Code for insertion sort

A

For i from 0 to length(list)-1:
Key = list[i]
j = i - 1
while j > -1 and key < list[j]
list[j+1] = list[j]
j = j + 1
list[j + 1]= key
Next i

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

What is a flow diagram?

A

A flow diagram is a diagram that shows an overview of a program. Flow diagrams normally use standard symbols to represent the different types of instruction. These symbols are used to construct the flowchart and show the step-by-step solution to the problem. Flow diagrams are sometimes known as flowcharts.

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

Advantages of flow diagrams

A
  • easy to see how program flows
  • follow an international standard
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Disadvantages of using flow diagrams

A
  • can be large in size and confusing to follow
  • any changes mean large portion of diagram has to be redrawn
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Pros of merge sort

A

Quickest
Most efficient

17
Q

Cons of merge sort

A

Difficult to understand

18
Q

Pros of bubble sort

A

Simple to understand

19
Q

Cons of bubble sort

A

Slow, inefficient, lots of steps

20
Q

Pros of insertion sort

A

Fast, good for small data sets

21
Q

Cons of insertion sort

A

Remembering pivot point/ key