Download presentation
Presentation is loading. Please wait.
Published byGervase Rich Modified over 8 years ago
1
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 19, 2005
2
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 2 Today Review arrays Searching arrays for a particular value Sorting arrays
3
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 3 Exercises 1. Find Sum and Average of Array 2. Determine Largest and Smallest Elements in Array 01 23
4
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 4 Example double[] sale={3.5, 4.6, 5.2, 3.8}; double sum = 0; double average; Find Sum and Average of Array 01 23
5
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 5 Example double[] sale={3.5, 4.6, 5.2, 3.8}; double sum = 0; for(int ind = 0; ind < sale.length; ind++) { sum = sum + sale[ind]; } double average; if(sale.length != 0) average = sum / sale.length; else average = 0.0; Find Sum and Average of Array 01 23
6
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 6 Example double[] sale={3.5, 4.6, 5.2, 3.8}; int maxIndex = 0, minIndex = 0; double largestSale; double smallestSale; Determining Largest/Smallest Element in Array 01 23
7
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 7 Example double[] sale={3.5, 4.6, 5.2, 3.8}; int maxIndex = 0, minIndex = 0; for (int ind = 1; ind < sale.length; ind++) { if (sale[ind] > sale[maxIndex]) maxIndex = ind; else if (sale[ind] < sale[minIndex]) minIndex = ind; } double largestSale = sale[maxIndex]; double smallestSale = sale[minIndex]; Determining Largest/Smallest Element in Array 01 23
8
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 8 Searching Arrays Find one/several particular element(s) in an array of many elements Complexity (How Long To Search?) ♦ find a parking space - linear ♦ look up a word in a dictionary - complex 500K+ words in Oxford Dictionary search - very complex over 3 trillion web pages
9
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 9 Time Complexity Important feature in Computer Science research: ♦ How long does an algorithm take to complete? ♦ Given an input (e.g. an array) of size n, how long does it take for the algorithm (e.g. search for a particular value) to complete? Linear algorithm: time = k 1 * n Quadratic algorithm: time = k 2 * n 2 etc.
10
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 10 Linear Searching Algorithm: ♦ Get a test value and a list of values list can be ordered or unordered ♦ loop through the list repeatedly ask: Is this a match? quit when the answer is yes (use break statement) ♦ if you finish all items, there is no match Inefficient ♦ worst time to search is the length of the list Relatively easy to program
11
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 11 Example int[] list={3, 6, 27, 8, 33, 54, 23}; int foundAt = -1, element = 33; Linear Search 01 23
12
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 12 Example int[] list={3, 6, 27, 8, 33, 54, 23}; int foundAt = -1, element = 33; for(int i=0; i<list.length; i++) { if(list[i] == element) { foundAt = i; break; } } Linear Search 01 23
13
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 13 Array of Objects Declare array of Student objects (ref. variables) Instantiate array of size 10 Instantiate each of the Student objects ♦ Ask for age (int) and name (String) ♦ Instantiate object
14
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 14 Example Student[] students; students = new Student[10]; for(int i = 1; i < students.length; i++) { //get int age //get String name students[i]=new Student(name, age); } Array of Student objects 01 23
15
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 15 Example int foundAt=-1; String name=“Mark”; for(int i = 0; i < students.length; i++) { if(students[i].getName().equals(name)) { fountAt=i; break; } Search for student Mark 01 23
16
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 16 Binary Search Requires ordered (sorted) list Set searchRange to the entire list Repeat: ♦ pick a “test value” in the middle of searchRange ♦ if test value == value searching for Stop! ♦ if test value > value searching for searchRange = lower half of searchRange ♦ if test value < value searching for searchRange = upper half of searchRange
17
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 17 Example 2 4 5 12 16 19 22 26 29 32 37 41 46 50 Looking for 46 Trial 1 2 3 2 4 5 12 16 19 2226 29 32 37 41 46 50 2 4 5 12 16 19 22 26 29 32 3741 46 50
18
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 18 Sorting Sort students by birth date Get a group of 5 students Another student will sort them by birth date Analyze the sorting strategy ♦ Can we devise an algorithm following that strategy? Goal: sort following a methodology ♦ We should be able to write it as an algorithm and then program it Demonstrate selection sort Write algorithm
19
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 19 Selection Sort General Algorithm Scan the list to find the smallest value Swap that value with the value in the first position in the list Scan rest of list to find the next smallest value Swap that value with the value in the second position in the list And so on, until you get to the end of the list
20
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 20 Selection Sort Methods Scan the list to find the smallest value Swap that value with the value in the first position in the list Scan rest of list to find the next smallest value Swap that value with the value in the second position in the list And so on, until you get to the end of the list loop
21
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 21 Selection Sort Sorts in ascending order Can be changed to sort in descending order ♦ look for max instead of min
22
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 22 Homework 6 Read/write files Array of ints Array of Participant objects Next: ♦ Sort array of ints ♦ Sort array of participants ♦ Extra credit: group participants
23
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 23 Tomorrow Java applets HTML GUIs Reading Assignment: ♦ Chapter 6 pp. 302-328 (pp. 264-290 in old book) ♦ skim Chapter 13 pp. 835-915 (pp. 725-806 in old book)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.