CSCI 2 Chapter 18 Searching and Sorting

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/22

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

23 Terms

1
New cards

Selection and insertion, are called…

Quadratic sorts

2
New cards

Merge sort

orders values by recursively dividing the list in half until each sub-list has one element, then recombining

– divide the list into two roughly equal parts.

– recursively divide each part in half, continuing until a part contains only one element.

– merge the two parts into one sorted list.

– continue to merge parts as the recursion unfolds

3
New cards

Phase one of merge sort

a) Divide the unsorted list into sub-arrays of two smaller halves.

b) Then, the smaller lists are broken down further until they can no longer be divided, leaving one element item in each halved list

4
New cards

Merge sort phase two

Sort and merge two of the sub-arrays until you have sorted and merged all the sub-arrays that were created in phase one.

Note: always start the sorting and merge in the reverse order of phase one

5
New cards

Merge sort is also called

logarithimic sorts

6
New cards

Searching

the process of finding a target element among a group of items (the search pool) or determining that it isn't there

requires repetitively comparing the target to candidates in the search pool

7
New cards

An efficient search…

performs no more comparisons than it has to.

8
New cards

Linear search

examines each item in the search pool, one at a time, until either the target is found or until the pool is exhausted

does not assume the items in the search pool are in any particular order

9
New cards

binary search

eliminates large parts of the search pool with each comparison

Begins in the middle, rather than at one end

If the target isn't found, we know that if it is in the pool at all, it is in one half or the other

We can then jump to the middle of that half, and continue similarly

Each comparison in a binary search eliminates half of the viable candidates that remain in the search pool

10
New cards

A binary search algorithm is often…

implemented recursively.

Each recursive call searches a smaller portion of the search pool.

The base case is when there are no more viable candidates.

At any point there may be two “middle” values, in which case the first is used.

11
New cards

Binary search steps

1) Add the smallest index to the largest index then divide it by 2. Note: you are doing integer division.

2) Then compare the target with the middle value found at the index in part 1 above.

a) If the target value and the middle value are equal, then return the index of the middle value.

b) If the target value is greater than the middle value, then the target must be in the 2nd half of the subarray.

c) If the target is less than the middle value, then the target must be in the 1st half of the subarray.

3) Repeat the steps above starting a step #1 on the new subarray until the target is found or the list is exhausted (that is, the target is not found in the list).

12
New cards

Sorting

The process of arranging a group of items into a defined order based on particular criteria

13
New cards

Selection sort

orders a list of values by repetitively putting a particular value into its final position

– find the smallest value in the list.

– switch it with the value in the first position.

– find the next smallest value in the list.

– switch it with the value in the second position.

– repeat until all values are in their proper places

14
New cards

Insertion sort

orders a values by repetitively inserting a particular value into a sorted subset of the list

15
New cards

Insertion sort steps

1) If it is the first element, it is already sorted.

2) Pick the next element and compare it with all the elements in the sorted sub-list.

3) Shift if necessary to the right all the elements in the sorted sub- list to that is greater than the value to be sorted.

4) Repeat the steps starting at step#2 until the list is sorted.

16
New cards
17
New cards
18
New cards
19
New cards
20
New cards
21
New cards
22
New cards
23
New cards