1 / 17

Arrays Sorting

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]

cjoan
Download Presentation

Arrays Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays Sorting

  2. 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

  3. Array Concepts • Why do we use them? • See example in notes Wendi Jollymore, ACES

  4. 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

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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

More Related