CSE 1321 Modules 6-8 Review Spring 2019

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Computer Programming Sorting and Sorting Algorithms 1.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Sorting and Searching Algorithms Week 11 DSA. Recap etc. Arrays are lists of data 1-D, 2-D etc. Lists associated with searching and sorting Other structures.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Chapter 14: Sorting and searching. Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Chapter 11 Arrays Continued
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
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.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
ALGORITHMS.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Pseudo-code. Pseudo-code Task 1 Preparation Use the following code to declare, initialise and populate an array in a new VB.NET console application:
Algorithm Definition An algorithm is a step-by-step solution to a problem.
Searching and Sorting Searching algorithms with simple arrays
Chapter 9: Sorting and Searching Arrays
CS212: Data Structures and Algorithms
Sorting Mr. Jacobs.
PowerPoint Presentation Authors of Exposure Java
Data Structures I (CPCS-204)
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Recitation 13 Searching and Sorting.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Design and Analysis of Algorithms
Searching and Sorting Linear Search Binary Search ; Reading p
Linear and Binary Search
Algorithm design and Analysis
Describing algorithms in pseudo code
Introduction to Programming
Section 13.1 Introduction.
Chapter 8 Arrays Objectives
CSc 110, Spring 2017 Lecture 39: searching and sorting
Lecture 11 Searching and Sorting Richard Gesick.
Review of Arrays and Pointers
Coding Concepts (Basics)
Searching and Sorting 1-D Arrays
A Robust Data Structure
Linear Search Binary Search Tree
Data Structures (CS212D) Week # 2: Arrays.
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Arrays Week 2.
1D Arrays and Lots of Brackets
Review of Searching and Sorting Algorithms
Chapter 8 Arrays Objectives
Module 8 – Searching & Sorting Algorithms
Module 8 – Searching & Sorting Algorithms
Insertion Sort.
Lecture No 5A Advance Analysis of Institute of Southern Punjab Multan
1D Arrays and Lots of Brackets
Module 8 – Searching & Sorting Algorithms
Review of Classes and Arrays
CSE 1321 Modules 1-5 Review Spring 2019
Module 8 – Searching & Sorting Algorithms
1D Arrays and Lots of Brackets
10.3 Bubble Sort Chapter 10 - Sorting.
Sorting Algorithms.
Presentation transcript:

CSE 1321 Modules 6-8 Review Spring 2019 Introduce yourself and welcome the students

Module 6 OOP & Classes Classes are: Classes have: A complex data type A way of representing the concept of real life objects Classes have: A name Variables (attributes) – should be PRIVATE Constructors (special functions/methods) Functions (methods) GET functions return the object’s current attribute value SET functions change the object’s attribute value

Module 6 Visibility Visibility refers to what can see (and therefore modify) the attribute’s value. It is the Computer Science version of the principle of least access – only those that need to access it can do so. This prevents the data from being changed either accidentally or maliciously. Declaring an attribute as PRIVATE is the method through which this is accomplished and helps to promote encapsulation.

Ps Pseudocode Class CLASS Elevator BEGIN END CLASS

C# & Java Class class Elevator { }

Python Class class Elevator:

Pseudocode Class Attributes CLASS Elevator BEGIN PRIVATE MAX_WEIGHT = 600 PRIVATE MAX_OCCUPANTS = 10 PRIVATE currentFloor PRIVATE currentOccupancy PRIVATE nextStop //Not done with class, so no END

Pseudocode Class Constructor CONSTRUCTOR ELEVATOR(parameters: none) currentFloor = 0 currentOccupancy = 0 nextStop = 0 END CONSTRUCTOR

Pseudocode Class Constructor 2 CONSTRUCTOR ELEVATOR(parameters: occ, stop) currentFloor = 0 currentOccupancy = occ nextStop = stop END CONSTRUCTOR

Pseudocode Class Method 1 METHOD BOOLEAN OVERLIMIT(parameters: numPass, avgWeight) IF (numPass * avgWeight > MAX_WEIGHT * MAX_OCCUPANTS) RETURN TRUE ELSE RETURN FALSE END METHOD

Pseudocode Class Method 2 METHOD INTEGER getNextStop(parameters: curFloor, Stops[]) BEGIN METHOD CREATE NextFloor = curFloor IF (|curFloor – Stops[0]| < |curFloor – Stops[1]|) RETURN Stops[0] ELSE RETURN Stops[1] END METHOD

Pseudocode Class Method 3 METHOD INTEGER getCurrentFloor (parameters: none) return currentFloor END CLASS

Building the Elevators Ps BEGIN MAIN CREATE elevatorNorth, elevatorSouth as ELEVATOR END MAIN

Installing the Elevators Ps BEGIN MAIN CREATE elevatorNorth, elevatorSouth as ELEVATOR elevatorNorth = new ELEVATOR() elevatorSouth = new ELEVATOR(2,100) END MAIN

Ps Using the Elevators BEGIN MAIN END MAIN CREATE elevatorNorth, elevatorSouth as ELEVATOR elevatorNorth = new ELEVATOR() elevatorSouth = new ELEVATOR(2,100) PRINTLINE(elevatorNorth.getCurrentFloor) PRINTLINE(elevatorSouth.getNextStop) END MAIN

In-class Exercise Create a fireplace class that contains firewood and temperature attributes, a method to add firewood (increasing attribute), method to reduce the firewood (as it burns over time), a method that increases the temperature as firewood is added and one to reduce the temperature as firewood is consumed.

In-class Exercise Solution CLASS FIREPLACE BEGIN CLASS PRIVATE firewood PRIVATE temp CONSTRUCTOR FIREPLACE(parameters: none) firewood = 0 temp = 0 END CONSTRUCTOR

In-class Exercise Solution CONSTRUCTOR FIREPLACE(parameters: wood) firewood = wood temp = 300 END CONSTRUCTOR METHOD addWood(parameters: wood) this.firewood += wood increaseTemp(100) END METHOD METHOD increaseTemp(parameters: temp) this.temp += temp

In-class Exercise Solution METHOD burnWood(parameters: wood) this.firewood -= wood reduceTemp(50) END METHOD METHOD reduceTemp(parameters: temp) this.temp -= temp METHOD getTemp() return this.temp

In-class Exercise Solution METHOD getFirewood() return this.firewood END METHOD END CLASS

Module 7 - 1D Arrays Complex Data Type that: Holds several values of the same type Allows instant access by specifying a index number using brackets [ ] (remember to start at 0) Is linear Is static

1D Arrays - Creation Ps BEGIN MAIN CREATE INTEGER Stops[5] END MAIN

1D Arrays - Iteration Ps FOR each I in Stops from 0 to 4 by 1 PRINTLINE(“Next Stop is: “ + Stops[i]) END FOR

1D Arrays – In-class Exercise You have been asked to help a small movie theater create a program to track the ticket prices for the 100 seats in the theater for an upcoming blockbuster movie. Declare a 1D array that can hold 100 prices. Fill the first 20 values with $10 Fill the second 20 values with $20 Fill the third 20 values with $30 Fill the fourth 20 values with $40 Fill the last 20 values with $50

1D Arrays – In-class Exercise BEGIN MAIN CREATE array INTEGER Seats[100] FOR s is 0 to (length of Seats - 1) by 1 IF (s < 20) Seats[s] = 10 ELSE IF (s < 40) Seats[s] = 20 ELSE IF (s < 60) Seats[s] = 30 ELSE IF (s < 80) Seats[s] = 40 ELSE Seats[s] = 50 END FOR END MAIN

Ps Module 7 - 2D Arrays Complex Data Type that: Holds several values of the same type Allows instant access by specifying a index number using brackets [][] (remember to start at 0) Is linear Is static

2D Arrays - Creation Ps BEGIN MAIN CREATE INTEGER Stops[5][5] END MAIN //Willy Wonka elevator that can go 8 directions

2D Arrays - Iteration Ps Use nested loops to iterate through a 2D array FOR each r in ROW from 0 to 4 by 1 FOR each c in COLUMNS from 0 to 4 by 1 PRINTLINE(“Next Stop is: “ + Stops[r][c]) END FOR

2D Arrays – In-class Exercise You have been asked to help a small movie theater revise the program to track the ticket prices for the 100 seats in the theater for an upcoming blockbuster movie. Declare a 2D array that can hold 100 prices. Fill the rows one and two with $10 Fill rows three and four with $20 Fill rows five and six with $30 Fill row seven and eight with $50 Fill the rows nine and ten with $50

2D Arrays – In-class Exercise BEGIN MAIN CREATE array of INTEGERS Seats[10][10] FOR R in ROWS from 0 to (length of ROW - 1) by 1 FOR C in COLS from 0 to (length of COLUMNS - 1) by IF (R = 0 OR 1) Seats[R][C] = 10 ELSE IF (R = 2 OR 3) Seats[R][C] = 20 ELSE IF (R = 4 OR 5) Seats[R][C] = 30 ELSE IF (R = 6 OR 7) Seats[R][C] = 40 ELSE Seats[R][C] = 50 END FOR END MAIN

In-class Exercise – Searching 1D For a special movie premiere event the theater held an auction and stored the bids in their seating array (1D array). They would like you to search through the array and find the highest bid so they know what it was. Hint: Use a linear search since the data is not sorted.

In-class Exercise - Searching BEGIN MAIN CREATE maxBid maxBid ← Seats[0] FOR each b in Seats from 1 to 99 by 1 IF (Seats[b] > maxBid) maxBid = Seats[b] END IF END FOR END MAIN

In-class Exercise – Searching 2D The theater has asked you to write a program that allows a user to select a seat from the seating array (2D array). They would like it to search through the array by price to find the first available seat at that price point. Hint: Use a linear search since the data is not sorted.

In-class Exercise – Searching 2D BEGIN MAIN CREATE price PRINT(“Please enter desired price: $ “) price ← READ user input FOR each row in Seats from 0 to 9 by 1 FOR each col in Seats from 0 to r by 1 IF (Seats[row][col] == price) PRINTLINE(”The seat at “ + [row] + “,” + [col] + “is available for $” + price) END IF END FOR END MAIN

Binary Search - Pseudocode METHOD BinarySearch (parameters: array G, target) BEGIN low ← 0, mid ← 0, high ← the number of elements in G WHILE (true) mid ← (low + high) / 2 IF (target = G[mid]) RETURN true ELSE IF (target < G[mid]) high ← mid ELSE low ← mid ENDIF IF (mid+1 >= high) RETURN false ENDWHILE END BinarySearch

Bubble Sort - Pseudocode FOR each I from 0 to Length of A - 1 FOR each J from I+1 to Length of A – I – 1 IF (A[J] > A[J+1]) temp ← A[J] A[J] = A[J+1] A[J+1] = temp END IF END FOR //Bubble sort compares adjacent elements and moves the larger value to the right.

Exchange Sort - Pseudocode FOR each I from 1 to n FOR each J from I+1 to n IF (A[J] < A[I]) temp ← A[J] A[J] ← A[I] A[I] ← temp ENDIF ENDFOR //Exchange sort finds the smallest element and swaps it with the element in the next position to fill starting at index 0, then moving to the left.

Selection Sort - Pseudocode FOR each I from 0 to n minPos ← I FOR each J from I + 1 to n IF (B[j] < B[minPos]) minPos ← J ENDIF ENDFOR IF (I != minPos AND minPos < n) temp ← B[minPos] B[minPos] ← B[I] B[I] ← temp //Selection sort scans the array to find the smallest element and swaps it with the first element, then continues with the next position and so on. This creates a Sorted sub-array and an Unsorted sub-array, as each element is placed the Sorted sub-array grows, and the Unsorted sub-array shrinks.

Insertion Sort - Pseudocode FOR each index from 2 to n key ← A[index] position ← index // Shift larger values to the right WHILE (position > 0 AND key < A[position-1]) A[position] = A[position - 1] position ← position -1 ENDWHILE list [position] = key ENDFOR //Insertion sort works by inserting the next value to sort (it starts with the 2nd element) into its place. This creates a Sorted set (left side of array) and an Unsorted set, as each element is placed the Sorted set grows, and the Unsorted set shrinks.