INFO 16029 Problem Solving and Programming Logic Arrays
INFO 16029 Problem Solving and Programming Logic Arrays Sorting
Array Concepts data 0 1 Array Index 2 length = n 3 Element 4 n-2 n-1 10/20/2021 Wendi Jollymore, ACES 2
Array Concepts Why do we use them? See example in notes 10/20/2021 Wendi Jollymore, ACES 3
Coding Arrays Define the type and length Pseudocode: integer array. Name[n] Assumes 0 -based array First index is 0, last index is n-1 Java: int[] array. Name = new int[n]; 10/20/2021 Wendi Jollymore, ACES 4
Coding Arrays Accessing elements: Pseudocode: Print numbers[i] first. Names[i] = “Fred” Java: System. out. println(numbers[i]); first. Names[i] = “Fred”; 10/20/2021 Wendi Jollymore, ACES 5
Coding Arrays Fill an array with values: integer numbers[10] For counter = 0 to 9 Print "Enter number ", counter+1 Get numbers[counter] Print and calculate average: For counter = 0 to 9 Print numbers[counter] Add numbers[counter] to total Print total / 10 10/20/2021 Wendi Jollymore, ACES 6
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. 10/20/2021 Wendi Jollymore, ACES 7
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. 10/20/2021 Wendi Jollymore, ACES 8
Parallel Arrays For grades example What if we wanted to display course code? index See code in notes String[] courses 10/20/2021 float[] grades PROG 10082 0 87. 0 INFO 16029 1 88. 2 MATH 26507 2 92. 5 SYST 16529 3 79. 8 SYST 10049 4 85. 7 Wendi Jollymore, ACES 9
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. 10/20/2021 Wendi Jollymore, ACES 10
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… 10/20/2021 Wendi Jollymore, ACES 11
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 ∑ i i=1 10/20/2021 Wendi Jollymore, ACES 12
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? 10/20/2021 Wendi Jollymore, ACES 13
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[curr. El] > grades[curr. El+ 1] Then temp. Grade = grades[curr. El] = grades[curr. El + 1] = temp. Grade End If 10/20/2021 Wendi Jollymore, ACES 14
Bubble Sort Logic One Pass: num. Comparisons = number. Elements - 1 curr. El= 0 While curr. El < num. Comparisons If grades[curr. El] > grades[curr. El + 1] Then temp. Grade = grades[curr. El] = grades[curr. El + 1] = temp. Grade End If Add 1 to curr. El 10/20/2021 Wendi Jollymore, ACES 15
Bubble Sort Logic All Passes: pass. Number = 1 num. Comparisons = number. Elements - 1 While pass. Number <= number. Elements – 1 curr. El= 0 While curr. El < num. Comparisons If grades[curr. El] > grades[curr. El + 1] Then temp. Grade = grades[curr. El] = grades[curr. El + 1] = temp. Grade End If Add 1 to curr. El Subtract 1 from num. Comparisons Add 1 to pass. Number 10/20/2021 Wendi Jollymore, ACES 16
Other Sort Algorithms Sorting is usually part of a larger course in Data Structures See links in notes for more sort algorithms 10/20/2021 Wendi Jollymore, ACES 17
- Slides: 17