Search And Sort Algorithims. Flashcards

1
Q

What is a binary search?

A

-1) Find middle item in ordered list.
2) If this is the item your looking for stop if not compare the item your looking for with the middle item and if it comes before the middle item discard the left, and if it comes after the item then discard the right.
Repeat until you find the item.
TO BE USED IN AN ORDERED LIST.

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

What is a linear search?

A
  • Used on an unordered list.
  • Checks each item in turn to see if it is the correct one.
  • Stops when it has find the item it was looking for or has checked everything.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What can be compared between an binary search and an linear search?

A
  • Linear Search= Simpler but not as efficient.
  • Linear can be used on any type of list, doesn’t have to be ordered.
  • Linear searches are often used on small lists as its inefficent.
  • Binary searches are more suitable for longer and large lists of items.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Bubble sort?

A
  • Sort an unordered list of items.
  • Simple but takes a while.
    Steps:
    1) Look at first two items in a list.
    2) If they are in right order don’t do anything, if they are in the wrong order swap them.
    3) Move onto the next pair of items.
    4) repeat until you get to the end of the list- this is called a pass and the last item will be in the correct place so don’t include it in next pass.
    Repeat until there are no swaps in pass.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the advantages and disadvantages of a bubble sort?

A

Pros:

  • Simple Algorithm + Can be easily implemented
  • Not much memory used as sorting is used on original list.

Cons:

  • Inefficient.
  • Not suitable for long list of items.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a Merge sort?

A
  • Splits the list apart.
  • Merges it back together.
  • Small lists are easier to sort than large lists.
  • Easier to merge to ordered list than two unordered list.
    Steps:
    1) Split the list in half. (Smaller lists= sub-lists). 2nd sub list should start with the middle item.
    2) Keep splitting the sub-lists until all lists contain only one item.
    3) Merge pairs of sub-lists and each time you merge sort the items in right order.
    4) Repeat until you’ve merged all sub-lists together.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the pros and cons of a merge list?

A

Pros:

  • More efficient and quicker than a bubble sort.
  • More efficient than insertion sort for large lists of items/
  • Consistent running time no matter how ordered the original list is.

Cons:

  • Slow at small lists.
  • Even if the list is ordered, it goes through the whole process still
  • Uses a lot of memory for the split process.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an insertion sort?

A
  • Simplest.
    1) Look at the 2nd item in a list.
    2) Compare to all items before it and insert the number in right place.
    3) Repeat step 2, until last number in the list has been inserted into the correct place.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly