Chapter 16 Sorting, Searching, and Algorithm Analysis

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

• There are many strategies for sorting arrays. Some examples are...

- Bubble sort - Selection sort - Insertion sort - Quick sort - Merge sort

Criteria for Measuring Efficiency

• Time: the time efficiency of an algorithm is measured by the time complexity function of the algorithm. • Space: the space efficiency of an algorithm is measured by its space complexity function.

Efficiency of Binary Search

A basic step in binary search is to split the array, compare X to the middle element, and then select the half of the array in which to continue the search • Each basic step reduces the size of the array to half the previous size till the size of the array is 1 in the worse case • If the array has size N, binary search will require no more than log N basic steps in the worst case Binary Search is very efficient: large increases in the size of the array require very small increases in the number of basic steps, approximately: size of array # steps needed 500 8 1 thousand 10 1 million 20

Define computational problem

A computational problem is a problem that is meant to be solved by an algorithm. • A computational problem is described by specifying the data to be input to the algorithm, and the output that should be produced by the algorithm • Each possible input is called an instance of the problem

Define Quick Sort:

A divide-and-conquer algorithm • Partition an array into items that are less than the pivot and those that are greater than or equal to the pivot • Sort the left section • Sort the right section

Bubble sort is one of the simplest sorting algorithms, but not one of the most ___________.

Efficent

Define Selections Sort:

Find the largest item. Swap it with the last item. • Find the second largest item. Swap it with the second to last item. • Find the third largest item. Swap it with the third to last item. • Repeat finding the next largest item, and swapping it into the correct position until the array is sorted.

How does Insertion sort function?

Insertion sort begins with the 2nd element. It compares the 2nd element with the 1st and puts it before the first if it is not larger. • Next the 3rd element is put into the correct position among the first 3 elements. The third element is compared with the first element, and if it is larger than the first element, it is compared with the second element; it is inserted into the correct position among the first three elements.

• A basic step is also called a _____ ________

basic operation

A _____ ____ is an operation that is executed in _______ _____, that is, the time to execute the operation does not increase even if the size of the input increases

basic step constant time

One algorithm may be more _________ than another, that is, it may need less time, or less memory, to solve a problem of a given size

efficient

• Each instance is characterized by its ____, the amount of memory occupied by the input data that describes the instance.

size

• The ____ of an instance is the number of bits occupied by the input, but is usually specified by giving an integer that allows us to deduce the actual size in bits.

size

How does Mergesort function?

• A divide-and-conquer sorting algorithm • Gives the same performance, regardless of the initial order of the array items • Strategy • Divide an array into halves • Sort each half • Merge the sorted halves into one sorted array • It is an extremely efficient algorithm with respect to time • But, it requires a second array as large as the original array

Define Binary Search

• Also known as half-interval search that finds the position of a target value within a sorted array • Compares the target value with the middle element of the array. If they are equal, the search ends • If the target value is greater than the middle element, search the right half of the array • If the target value is less than the middle element, search the left half of the array • If the search ends with the remaining half being empty, the target is not in the array.

Define an array of numbers sorted in ascending order:

• An array A[ ] of N numbers is sorted in ascending order if the array entries increase (or never decrease) as indices increase: A[0] ≤ A[1] ≤ ≤ A[N − 2] ≤ A[N −1] Example of an array sorted in ascending order: 20 25 30 40 55

How does bubble sort work?

• It puts a list into increasing order by successively comparing adjacent elements, interchanging them if they are in the wrong order. • To carry out the bubble sort, we perform the basic operation, that is, interchanging a larger element with a smaller one following it, starting at the beginning of the list, for a full pass. • We iterate this procedure until the sort is complete.

Recursive Binary Search:

• The logic of binary search has a natural recursive implementation: • If first > last, then return -1 (base case). • Compare X to A[middle], where middle is the midpoint between lower and upper: middle = (first + last)/2 • If X == A[middle], return middle (we found it!) • If X < A[middle], then continue search in A[first..middle-1] • If X > A[middle], then continue search in A[middle+1..last]

Define Sequential Search

• To search an array A[0..N-1] for a value X, start an index at one end of the array, say 0. • Step index through the array, examining each A[index] to see if it is equal to X. • Stop if you find X and return index. Otherwise you get to the end of the array and return -1.


Kaugnay na mga set ng pag-aaral

Chapter 9: Teaching and Counseling

View Set

Combining like terms, distribution, and factoring

View Set

More on Membrane Potentials, Intro to Nervous System, Central Nervous System

View Set