Presentation is loading. Please wait.

Presentation is loading. Please wait.

INFO 16029 Problem Solving and Programming Logic INFO 16029 Problem Solving and Programming Logic Arrays Sorting.

Similar presentations


Presentation on theme: "INFO 16029 Problem Solving and Programming Logic INFO 16029 Problem Solving and Programming Logic Arrays Sorting."— Presentation transcript:

1 INFO 16029 Problem Solving and Programming Logic INFO 16029 Problem Solving and Programming Logic Arrays Sorting

2 9/20/2015Wendi Jollymore, ACES2 Array Concepts Index data 0 1 2 3 4 n - 2 n - 1 Array Element length = n

3 9/20/2015Wendi Jollymore, ACES3 Array Concepts Why do we use them? See example in notes

4 9/20/2015Wendi Jollymore, ACES4 Coding Arrays Define the type and length Pseudocode: integer arrayName[n] Assumes 0-based array First index is 0, last index is n-1 Java: int[] arrayName = new int[n];

5 9/20/2015Wendi Jollymore, ACES5 Coding Arrays Accessing elements: Pseudocode: Print numbers[i] firstNames[i] = “Fred” Java: System.out.println(numbers[i]); firstNames[i] = “Fred”;

6 9/20/2015Wendi Jollymore, ACES6 Coding Arrays Fill an array with values: Print and calculate average: integer numbers[10] For counter = 0 to 9 Print "Enter number ", counter+1 Get numbers[counter] For counter = 0 to 9 Print numbers[counter] Add numbers[counter] to total Print total / 10

7 9/20/2015Wendi Jollymore, ACES7 Exercises Try the three exercises in the notes: A string array called names[] with 25 elements contains the last names of all the salespeople in a company. Write the pseudocode to ask the user for a salesperson's name, then sequentially search the array for that person's name. For all matches in the list, display the index number. Otherwise, display a "not found" message.

8 9/20/2015Wendi Jollymore, ACES8 Exercises Rewrite the pseudocode at the beginning of this lesson using an array for the grades, but record grades for 6 courses. Ask the user to enter five numbers. Store the numbers in an array and then determine if the numbers were entered in ascending order. Display a message indicating whether they are sorted or not.

9 9/20/2015Wendi Jollymore, ACES9 Parallel Arrays For grades example What if we wanted to display course code? See code in notes PROG10082 INFO16029 MATH26507 SYST16529 SYST10049 87.0 88.2 92.5 79.8 85.7 String[] coursesfloat[] grades 0 1 2 3 4 index

10 9/20/2015Wendi Jollymore, ACES10 Exercise A program calculates the total revenue for various items sold in a kiosk. There are 2 arrays: price[] and quantity[]. The price[] array contains the prices of 25 items, and the quantity[] array contains the quantity[] of each of the 25 items sold. For example, price[3] contains the price of item 4 and quantity[3] contains the number of item 4's sold. Create a third array, revenue[] that is parallel to price[] and quantity[]. Fill this array with the total revenue of each of the 25 items. Revenue = price * quantity sold.

11 9/20/2015Wendi Jollymore, ACES11 Sorting Arrays Many different sort algorithms Choice depends on size of list Bubble Sort Simplest, easiest to learn Good for small lists (n <= 28) Demo…

12 9/20/2015Wendi Jollymore, ACES12 Sorting Arrays Bubble Sort summary: Number of passes: n – 1 Each pass puts one more element in its correct position Each pass has x – 1 comparisons Let x = # of unsorted elements Maximum number of comparisons: The sum of the values from 1 to n-1 n-1 ∑ i i=1

13 9/20/2015Wendi Jollymore, ACES13 Exercises Sort these in ascending order: 64 33 25 51 19 40 Try sorting in descending order For an array with 6 elements: How many passes? How many comparisons in total? For an array with 7 elements?

14 9/20/2015Wendi Jollymore, ACES14 Bubble Sort Logic Explained in the notes The main action taking place is swapping E.g. if a pair of items are out of order, swap! If grades[currEl] > grades[currEl+ 1] Then tempGrade = grades[currEl] grades[currEl] = grades[currEl + 1] grades[currEl + 1] = tempGrade End If

15 9/20/2015Wendi Jollymore, ACES15 Bubble Sort Logic One Pass: numComparisons = numberElements - 1 currEl= 0 While currEl < numComparisons If grades[currEl] > grades[currEl + 1] Then tempGrade = grades[currEl] grades[currEl] = grades[currEl + 1] grades[currEl + 1] = tempGrade End If Add 1 to currEl

16 Bubble Sort Logic All Passes: 9/20/2015Wendi Jollymore, ACES16 passNumber = 1 numComparisons = numberElements - 1 While passNumber <= numberElements – 1 currEl= 0 While currEl < numComparisons If grades[currEl] > grades[currEl + 1] Then tempGrade = grades[currEl] grades[currEl] = grades[currEl + 1] grades[currEl + 1] = tempGrade End If Add 1 to currEl Subtract 1 from numComparisons Add 1 to passNumber

17 Other Sort Algorithms Sorting is usually part of a larger course in Data Structures See links in notes for more sort algorithms 9/20/2015Wendi Jollymore, ACES17


Download ppt "INFO 16029 Problem Solving and Programming Logic INFO 16029 Problem Solving and Programming Logic Arrays Sorting."

Similar presentations


Ads by Google