AP Computer Science Chapter 8

Ace your homework & exams now with Quizwiz!

insertion sort rules

-again most likely won't need to know how to write it -short circuit evaluation: if and statement and the first part is false it automatically stops

merge sort rules

-the one with a start middle and end -start + end /2 -while loop with if else inside recursively sorts both halves of the arrays -sorts them separately and then combines them back together -the recursive method is very important

rules for selection sort

-​if statement inside for loop inside for loop -min and temp variables necessary -in inside the nested for loop just an if statement -in outer for loop after the inside loop, use temp and array of min as well as array of x -inner for loop meant to find location of the min value -they then swap it with the current value their looking at -won't have to actually write a selection sort probably -if it has the temp thing its usually a selection sort

binary search analysis

1. In the best case, the key is found on the first try (i.e., (low+ high)/2 is the index of key). 2. In the worst case, the key is not in the list or is at either end of a sublist. Here the n elements must be divided by 2 until there is just one element, and then that last element must be tested. An easy way to find the number of comparisons in the worst case is to round n up to the next power of 2 and take the exponenet . For example, in the array above, n=9. Suppose 21 were the key. Round 9 up to 16, which equals 2^4. Thus you would need four comparisons to find it. Try it!

steps of merge sort

1. Start with an unsorted list of n elements. 2. The recursive calls break the list into n sublists, each of length Note that these n arrays, each containing just one element, are sorted! 3. Recursively merge adjacent pairs of lists. These are then approximately n/2 lists of length 2; then, approximately n/4 lists of approximate length 4, and so on, until there is just one list of length n.

passes of insertion sort: 8146

1st pass: 1846 2nd pass: 1486 3rd pass: 1468 1. For an array of n elements, the array is sorted after n-1 passes. 2. After the kth pass, a[0], a[1], ... , a[k] are sortedwith respect to each other but not necessarily in their fianl sorted positions. 3. The worst case for insertion sort occurs if the array is initially sorted in reverse order, since this will lead to the maximum possible number of comparisons and moves. 4. The best case for insertion sort occurs if the array is already sorted in increasing order. In this case, each pass pass through the array will involve just one coparison, which will indicate that "it" is in its correct position with respect to the sorted list. Therefore, no elements will be removed.

passes of selection sort: 8416

1st pass: 1846 2nd pass: 1486 3rd pass: 1468 1. For an array of n elements, the array is sorted after n-1 passes. 2. After the kth pass, the first k elements are in their final sorted position.

passes for merge sort

5 -3 2 4 0 6 5 -3 2 406 5 -3 2 40 6 5 -3 2 4 0 6 -325 046 -302456 1. The major disadvantage of mergesort is that it needs a temporary array that is as large as the original array to be sorted. This could be a problem if space is a factor. 2. Mergesort is not affected by the initial ordering of the elements. Thus, best, worst, and average cases have similar run times.

binary search

If the elements are in a sorted array. Assume that a [low] ... a[high] is sorted in acesding order and that a method binSearch returns tthe index of key. If key is in the array, it returns -1.

merge sort

If there is more than one element in the arrat -Break the array into two halves. -Mergesort the left half. -Mergesort the right half -Merge the two subarrays into a sorted array.

sequential search

Starts at the first element and compares the key to each element in turn until the key is found or there are no more elements to examine in the list. If the list is sorted, in acesnding order, say, stop searching as soon as the key is less than the current list. 1. The best case has key in the first slot, 2. The worst case occurs if the key is in the last slot or not in the list. In the worst case, all n elements must be examined. 3. On average, there will be n/2 comparisons.

insertion sort

Think of the first element in the array, a[0], as being sorted with respect to itself. The array can now be thought of as consisting of two parts, a sorted list followed by an unsorted list. The idea of insertion sort is to move elements from the unsorted list to the sorted list one at a time; as each item is moved, it is inserted into its correct position int eh sorted list. In order to place the new item, some elements may need to be moved down to create a slot.

selection sort

This is a "search-and-swap" algorith. Find the element in the array and exchange it. with a [0], the first element. Now find the smallest element in the subarray a[1] ... a[n-1] and swap it with a[1], the second element in the array. Continue this process until jus the last two elements remain to be sorted, a[n-2] and a[n-1]. The smaller of these two elements is placed in a [n-2]; the larger , in a[n-1]; and the sort is complete.

insertion sort code

for(int i=1;i<nums.length;i++) { int val=nums[i]; int pos=i; //shift larger values to the right while(pos > 0&&val< nums[pos-1]) //the bolded symbol flipped is what makes this descending rather than the ascending it is now { nums[pos]=nums[pos-1]; pos--; ​} //place the smaller value where it belongs nums[pos]=val; ​}

binary search code

int start=0; int end=nums.length-1; int mid=(start+end)/2; while(nums[mid]!=value&&start<=end) { if(value>nums[mid]) start=mid+1; ​else end=mid-1; ​mid=(start+end)/2; ​} if(nums[mid]==value) return mid; ​else //value not found return -1;

selection sort code

or(int x=0; x<array.length-1;x++) { min=x; //scan the array looking for the smallest value for(int y=x+1; y<array.length;y++) { if(array[y] < array[min]) min=y; ​​} //swap the values temp=array[min]; array[min]=array[x]; array[x]=temp; ​}


Related study sets

Anatomy respiration III - Pleurae, lungs and tracheobronchial tree

View Set

Chapter 52: Assessment and Management of Patients With Endocrine Disorders

View Set

exam 5 immune questions ch 17, 20

View Set

Health & Nutrition Chapter 2 - Pellagra, Scientific Method

View Set

Real Estate Study Questions pt.4

View Set

CRJ 321 Intro to Crime Scene Mid-Term

View Set