New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

Week 9 Notes

by: Taylor Kahl

Week 9 Notes CSC 2310

Taylor Kahl
GPA 4.21

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Notes on Chapter 13, Searching and Sorting
Princliples of Computer Programming
Kebina Manandhar
Class Notes
25 ?




Popular in Princliples of Computer Programming

Popular in ComputerScienence

This 5 page Class Notes was uploaded by Taylor Kahl on Saturday March 12, 2016. The Class Notes belongs to CSC 2310 at Georgia State University taught by Kebina Manandhar in Winter 2016. Since its upload, it has received 12 views. For similar materials see Princliples of Computer Programming in ComputerScienence at Georgia State University.

Similar to CSC 2310 at GSU

Popular in ComputerScienence


Reviews for Week 9 Notes


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 03/12/16
Chapter 13: Searching and Sorting  Sequential search – examines every value in an array of list from start to finish until it locates the target element o returns the index of the target element o returns –1 if element not found  Binary search – locates target element by eliminating half of the array or list at a time o have to import java.util.* or java.util.Arrays to use methods from the Array class o can only use on sorted lists o compares the target element to the middle element of the list  if target > middle, eliminate 1 half, search 2 ndhalf  if target < middle, eliminate 2 ndhalf, search 1 half o repeats the process until it returns the index of the target element o if the element is not found, returns –(insertionPoint + 1)  insertionPoint is the index where the target element would appear if it were in the sorted array o Example: int[a] = [1, 4, 17, 18, 20]. Target element = 6. 6 is not in the array, but if it were, it would be between 4 and 17, at index 2 binarySearch will return –(2 + 1) = – 3  Binary search is usually more efficient than a sequential search  Useful methods from the Arrays class: binarySearch(array, value) searches entire array binarySerach(array, minIndex, searches a range of indexes in the maxIndex, value) array, from minIndex(inclusive) to maxIndex(exclusive) copyOf(array, length) returns a copy of an array with a new length equals(array1, array2) returns true if arrays contain the same elements in the same order fill(array, value) sets every element in the array to given value sort(array) sorts elements toString(array) “[10, 30, 2]” Remember that these are static methods, so the syntax is: Arrays.methodName(parameters)  Binary search code: public static int binarySearch(int[] a, int target) { int min = 0; int max = a.length – 1; while (min <= max) { int mid = (min + max) / 2; if (a[mid] < target) { //too small min = mid + 1; } else if (a[mid] > target) { //too big max = mid – 1; } else { //a[mid] = target return mid; } } return – (min + 1) //if target isn’t found } Here’s how this code would use binarySearch(a, 6); for my example int[] a = [1, 4, 17, 18, 20] min = 0 max = a. length – 1 = 5 – 1 = 4 min <= max mid = (min + max) / 2 = (0 + 4) / 2 = 2 a[2] = 17 17 > 6 max = mid – 1 = 2 – 1 = 1 min <= max, go through the loop again mid = (min + max) / 2 = (0 + 1) / 2 = 0 a[0] = 1 1 < 6 min = mid + 1 = 0 + 1 = 1 min <= max mid = (min + max) /2 = (1 + 1) / 2 = 1 a[1] = 4 4 < 6 min = mid + 1 = 1 + 1 = 2 min > max, end the loop return –(min + 1) = – 3  A recursive method for binarySearch: o if the target is not found, return -1 o this method has a helper method public static int binarySearch(int[] a, int target) { return binarySearch(a, target, 0, a.length – 1); //calls the helper method } private static int binarySearch(int[] a, int target, int min, int max) {//extra parameters min & max if (min > max) { return -1; } else { int mid = (min + max) / 2; if (a[mid] < target) { //too small return binarySearch(a, target, mid + 1, max);//change min } else if (a[mid] > target) { //too big return binarySearch(a, target, min, mid – 1); //change max } else { return mid; //a[mid] = target } } }  binarySearch can also be used on an array of Strings Strings o strings have a natural ordering  they can be arranged alphabetically  a String is “less than” another if it comes first alphabetically o binarySearch uses the compareTo method o A.compareTo(B) returns:  < 0 if A before B  > 0 if A after B  0 if A == B Runtime efficiency:  Efficiency: a measure of computing sources required to run your code o usually refers to runtime  To calculate efficiency, assume that: o Every 1 java statement has the same runtime o A method’s runtime = total # of statements in its body o A loop’s runtime, if the loop runs N times = N * # of statements in its body  Example: Statement1; Statement2; 2 for (int i = 1; i <= N, i++) { statement3; 2N statement4; } for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { 2 N*N = N statement5; N } } 2 This code will take N + 2N + 2 units of time to run  Growth rate – change in runtime as N changes. Runtime is proportional to the input data size, N  If N is extremely large, use the highest order of N to determine growth rate o Ignore constants or coefficients of N – we’re only concerned with how quickly runtime grows in proportion to N. The actual value doesn’t matter 2  Example: N + 2N + 2 o N dominates the overall run time  because if N is extremely large, 2N and 3 are so small compared 2 to N that we won’t count them 2 o This algorithm runs “on the order of” N o This algorithm is “Big-Oh” of N , written as O(N )2 3 2  Another example: 0.4N + 25N + 8N + 17 o This is O(N ). N is the highest order of N. Ignore the 0.4  Complexity class – category of algorithm efficiency based on algorithm’s relationship to N o an algorithm in the lowest complexity class (with the smallest order of N) is the most efficient and will run fastest Class Big-Oh If you double N, the growth rate constant O(1) doesn’t change logarithmic O(log 2) increases slightly linear O(N) doubles log-linear O(N log 2) slightly more than 2 doubles quadratic O(N ) quadruples cubic O(N ) multiplies by 8 exponential O(2 ) multiplies drastically – AVOID! this could take years to run  What’s the complexity class of binarySearch? o for an array of size N, it eliminates ½ of the elements until 1 remains o The number of elements goes in a series: N, N/2, N/4…4, 2, 1 o From the other direction, how many times do you need to multiply by 2 to reach N?  1, 2, 4…N/4, N/2, N  xxnumber of multiplications  2 = N (after multiplying by 2 x times, arrive at N)  x = log2N  logarithmic complexity class Happy Spring Break!!!


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Janice Dongeun University of Washington

"I used the money I made selling my notes & study guides to pay for spring break in Olympia, Washington...which was Sweet!"

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

Parker Thompson 500 Startups

"It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.