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

Slides:



Advertisements
Similar presentations
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Advertisements

Chapter 9: Advanced Array Manipulation
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Visual C++ Programming: Concepts and Projects
Searching and Sorting Linear Search Binary Search Selection Sort
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
Arrays Arrays are data structures consisting of data items of the same type. Arrays are ‘static’ entities, in that they remain the same size once they.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
An Introduction to Programming with C++ Fifth Edition
Searching and Sorting Copyright Prentice Hall (with modifications by Evan Korth)
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Unit 271 Searching and Sorting Linear Search Binary Search Selection Sort Insertion Sort Bubble (or Exchange) Sort Exercises.
Algorithms for Sorting Things. Why do we need to sort things? Internal Telephone Directory –sorted by department then by name My local video store holds.
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
CS1101: Programming Methodology Aaron Tan.
COS 260 DAY 16 Tony Gauvin.
Chapter 9: Advanced Array Concepts
Lists in Python.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 19: Searching and Sorting Algorithms
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
1 Searching and Sorting Linear Search Binary Search.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Chapter 6: Arrays: Lists and Tables
CS1101: Programming Methodology Aaron Tan.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
CS1101: Programming Methodology
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
CSCI 51 Introduction to Programming March 12, 2009.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections – Exercises 1 Xiang Lian The University of Texas – Pan American Edinburg,
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Chapter 16: Searching, Sorting, and the vector Type.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
CIS 115 All Exercises Devry University (Devry) FOR MORE CLASSES VISIT CIS 115 All Exercises Devry University.
Copyright Prentice Hall Modified by Sana odeh, NYU
Chapter 16: Searching, Sorting, and the vector Type
Chapter 9: Sorting and Searching Arrays
An Introduction to Programming with C++ Sixth Edition
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Review of Arrays and Pointers
24 Searching and Sorting.
Data Structures (CS212D) Week # 2: Arrays.
Presentation transcript:

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

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

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

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];

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”;

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

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.

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/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 SYST String[] coursesfloat[] grades index

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.

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…

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

9/20/2015Wendi Jollymore, ACES13 Exercises Sort these in ascending order: 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?

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

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

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

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