Date post: | 28-Dec-2015 |
Category: |
Documents |
Upload: | lily-wilkinson |
View: | 221 times |
Download: | 2 times |
INFO 16029
Problem Solving and
Programming Logic
INFO 16029
Problem Solving and
Programming Logic
Arrays
Sorting
04/19/23 Wendi Jollymore, ACES 2
Array ConceptsArray Concepts
Index
data 0
1
2
3
4
n - 2
n - 1
Array
Elementlength = n
04/19/23 Wendi Jollymore, ACES 3
Array ConceptsArray Concepts
Why do we use them?See example in notes
04/19/23 Wendi Jollymore, ACES 4
Coding ArraysCoding Arrays
Define the type and lengthPseudocode:
integer arrayName[n]Assumes 0-based array
First index is 0, last index is n-1
Java:int[] arrayName = new int[n];
04/19/23 Wendi Jollymore, ACES 5
Coding ArraysCoding Arrays
Accessing elements:Pseudocode:
Print numbers[i]firstNames[i] = “Fred”
Java:System.out.println(numbers[i]);firstNames[i] = “Fred”;
04/19/23 Wendi Jollymore, ACES 6
Coding ArraysCoding 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
04/19/23 Wendi Jollymore, ACES 7
ExercisesExercises
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.
04/19/23 Wendi Jollymore, ACES 8
ExercisesExercises
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.
04/19/23 Wendi Jollymore, ACES 9
Parallel ArraysParallel Arrays
For grades exampleWhat 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[] courses float[] grades0
1
2
3
4
index
04/19/23 Wendi Jollymore, ACES 10
ExerciseExercise
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.
04/19/23 Wendi Jollymore, ACES 11
Sorting ArraysSorting Arrays
Many different sort algorithmsChoice depends on size of listBubble Sort
Simplest, easiest to learnGood for small lists (n <= 28)Demo…
04/19/23 Wendi Jollymore, ACES 12
Sorting ArraysSorting Arrays
Bubble Sort summary:Number of passes: n – 1Each pass puts one more element in its correct positionEach 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
04/19/23 Wendi Jollymore, ACES 13
ExercisesExercises
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?
04/19/23 Wendi Jollymore, ACES 14
Bubble Sort LogicBubble Sort Logic
Explained in the notesThe 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] = tempGradeEnd If
04/19/23 Wendi Jollymore, ACES 15
Bubble Sort LogicBubble 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
Bubble Sort LogicBubble Sort Logic
All Passes:
04/19/23 Wendi Jollymore, ACES 16
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
Other Sort AlgorithmsOther Sort Algorithms
Sorting is usually part of a larger course in Data StructuresSee links in notes for more sort algorithms
04/19/23 Wendi Jollymore, ACES 17