170 likes | 181 Views
Arrays Sorting. Array. 0. data. 1. Index. data. 2. length = n. Element. data. 3. data. 4. data. n - 2. data. n - 1. Array Concepts. data. Array Concepts. Why do we use them? See example in notes. Coding Arrays. Define the type and length Pseudocode: integer arrayName[n]
E N D
Arrays Sorting
Array 0 data 1 Index data 2 length = n Element data 3 data 4 data n - 2 data n - 1 Array Concepts data Wendi Jollymore, ACES
Array Concepts • Why do we use them? • See example in notes Wendi Jollymore, ACES
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]; Wendi Jollymore, ACES
Coding Arrays • Accessing elements: • Pseudocode: • Print numbers[i] • firstNames[i] = “Fred” • Java: • System.out.println(numbers[i]); • firstNames[i] = “Fred”; Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
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. Wendi Jollymore, ACES
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. Wendi Jollymore, ACES
Parallel Arrays • For grades example • What if we wanted to display course code? • See code in notes index String[] courses float[] grades Wendi Jollymore, ACES
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. Wendi Jollymore, ACES
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… Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
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? Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
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 Wendi Jollymore, ACES
Bubble Sort Logic • All Passes: 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 Wendi Jollymore, ACES
Other Sort Algorithms • Sorting is usually part of a larger course in Data Structures • See links in notes for more sort algorithms Wendi Jollymore, ACES