2.1.3 Searching and Sorting Algorithms Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Standard Algorithm

A

Pre loaded to perform a basic task

Sorting and Searching algorithm

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

Examples of Sorting algorithms

A

Sorting - Bubble sort, Merge sort, Insertion sort

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

What is Bubble sort?

A

Each item in a list is compared to the next item. If a swap is needed they will swap. Continue to end of list for a pass

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

Example of Bubble Sort

Data = 5,3,4,7,6,9

A

Data = (5,3,4,7,6,9)
3,5,4,7,6,9
3,4,5,7,6,9
3,4,5,6,7,9
Pass 1

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

Example of Bubble sort with 2 passes

Data = Dan,Ben,Bob,Tom,Sam,Liz

A

Data = (Dan,Ben,Bob,Tom,Sam,Liz)
Ben, Dan, Bob, Tom, Sam, Liz

Ben, Bob, Dan, Tom, Sam, Liz

Ben, Bob, Dan, Sam, Tom, Liz

Ben, Bob, Dan, Sam, Liz, Tom

Pass 1

Ben, Bob, Dan, Liz, Sam, Tom

Pass 2

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

What is a merge sort?

A

The data is broken down, broken down again until single values. Values will be merged into pairs while sorting

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

Example of Merge sort
Data = 10,1,13,24,7,5,36,25

A

Data = 10,1,13,24,7,5,36,25
10,1,13,24 7,5,36,25
10,1 13,24 7,5 36,25
10 1 13 24 7 5 36 25
1,10 13,24 5,7 25,36
1,10,13,24 5,7,25,36
1,5,7,10,13,24,25,36

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

What is an Insertion algorithm?

A

Value will be removed from an unsorted list and placed in the correct location in a sorted list

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

Example of Insertion sort

Data = 15,12,13,10,9,14

A

Data = (15,12,13,10,9,14)
Sorted: (15) 12,13,10,9,14
(12,15) 13,10,9,14
(12,13,15) 10,9,14
(10,12,13,15) 9,14
(9,10,12,13,15) 14
(9,10,12,13,14,15)

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

Examples of Searching algorithms

A

Searching - Linear search and Binary search

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

What is a Linear Search?

A

Starting from the beginning of the list, compare a search value to the first item in the list, continue comparing in order until item is found or not found

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

Example of Linear search

Data = Ben,Bob,Tom,Sam,Dan,Ali

A

Data = (Ben,Bob,Tom,Sam,Dan,Ali)
Search Value = Tom

Compare Tom to Ben (Index 0) No Match

Compare Tom to Bob (Index 1) No Match

Compare Tom to Tom (Index 2) Found

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

Example of Linear search

Data = 5,3,4

A

Data = (5,3,4)
Search Value = 10

Compare 10 to 5 (Index 0) No Match

Compare 10 to 3 (Index 1) No Match

Compare 10 to 4 (Index 2) No Match

Item not found

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

What is Binary Search?

A
  • Data has to be sorted
  • Set a middle boundary
  • Compare search value to middle boundary
  • If it’s a match - found
  • If less than, Discard the upper side and left
  • If more than, Discard the lower side and right
  • Repeat the process

Number of values div 2 = Index of MB

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

Example of Binary search

Data = 1,2,3,4,5,6,7,8,9

A

Data = (1,2,3,4,5(MB),6,7,8,9)
Search Value = 8
Compare 8 to 5 (MB) Not a Match
8>5 so discard the lower side
go to the upper side
Compare 8 to 8(MB) Match item found

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

What is Div and Mod?

A

Div: Integer division eg. 15 div 3 (//)

Mod: Modulus, remainder (%)

17
Q

Examples of Mod and Div

A

12 div 4 = 3
12 mod 4 = 0

10 div 7 = 1
10 mod 7 = 3

12 div 8 = 1
12 mod 8 = 4

13 div 7 = 1
13 mod 7 = 6

15 div 4 = 3
15 mod 4 = 3

18
Q

What are the advantages of a binary search?

A
  • More efficient because half of the data set is discarded after each pass/search
  • Works better with a big data set as less comparisons will be needed
19
Q

What are the disadvantages of a binary search?

A
  • Data has to be sorted
  • Inefficient when searching for the first item
20
Q

What are the advantages of a linear search?

A
  • Works well on sorted data set
  • Efficient when search item is at the front of the list
21
Q

What are the disadvantages of a linear search?

A
  • Not efficient with a large data set