Programming with Arrays 2

Slides:



Advertisements
Similar presentations
Selection Sort. Selection Sort Algorithm (ascending) 1.Find smallest element (of remaining elements). 2.Swap smallest element with current element (starting.
Advertisements

Session 3 Algorithm. Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
An Introduction to Sorting Chapter Chapter Contents Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection.
CSE 373: Data Structures and Algorithms
An Introduction to Sorting Chapter 9. 2 Chapter Contents Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection.
Simple Sorting Algorithms
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Selection Sort -Reading p Reading p
Sorting Algorithms Selection, Bubble, Insertion and Radix Sort.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
An Introduction to Sorting Chapter 8. 2 Chapter Contents Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
Lecture 16 Arrays: Part 2 COMP1681 / SE15 Introduction to Programming.
Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS
1 Week 9 l Array Basics l Arrays in Classes and Methods l Programming with Arrays and Classes l Sorting Arrays l Multidimensional Arrays Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
Building Java Programs Chapter 13 Searching reading: 13.3.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
CSE 373 Data Structures and Algorithms
Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 1 Class 14 - Review: Sorting & searching r What are sorting and searching? r Simple sorting algorithms.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Simple algorithms on an array - compute sum and min.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CS 162 Intro to Programming II Sorting Introduction & Selection Sort 1.
CS212: Data Structures and Algorithms
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
COP 3503 FALL 2012 Shayan Javed Lecture 16
Chapter 7 Arrays Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 7 Arrays Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Arrays Chapter 6 Array Basics Arrays in Classes and Methods
Searching and Sorting Linear Search Binary Search ; Reading p
CSS161: Fundamentals of Computing
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
Selection sort Given an array of length n,
Sorting Algorithms.
CS 2430 Object Oriented Programming and Data Structures I
AP Java Warm-up Boolean Array.
Expressions and Assignment Statements
CS 1430: Programming in C++.
Why did the programmer quit his job?
Arrays Lecture 11.
Simple Sorting Algorithms
CSS161: Fundamentals of Computing
Module 8 – Searching & Sorting Algorithms
slides adapted from Marty Stepp
Sorting Algorithms.
Simple Sorting Algorithms
Workshop for CS-AP Teachers
Chapter 19 Searching, Sorting and Big O
Simple Sorting Algorithms
Insertion Sort and Shell Sort
CSS161: Fundamentals of Computing
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
Module 8 – Searching & Sorting Algorithms
CSS161: Fundamentals of Computing
CS 165: Project in Algorithms and Data Structures Michael T. Goodrich
Stacks, Queues, ListNodes
Sorting Algorithms.
Programming with Arrays 1
Presentation transcript:

Programming with Arrays 2 This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing

Sorting an Array – Selection Sort Key algorithm for (int index = 0; index < count; index++) Place the indexth smallest element in a[index] 8 6 11 17 3 15 5 19 28 12 3 6 11 17 8 15 5 19 28 12 3 5 11 17 8 15 6 19 28 12 3 5 6 17 8 15 11 19 28 12 3 5 6 8 17 15 11 19 28 12 3 5 6 8 11 15 17 19 28 12 3 5 6 8 12 12 17 19 28 15 3 5 6 8 12 11 15 19 28 17 3 5 6 8 12 11 15 17 28 19 CSS161: Fundamentals of Computing 3 5 6 8 12 11 15 17 19 28

First Swapping in Selection Sort CSS161: Fundamentals of Computing

Second Swapping in Selection Sort CSS161: Fundamentals of Computing

Code for Selection Sort (1 of 3) public class SelectionSort { /** Precondition: numberUsed <= a.length; The first numberUsed indexed variables have values. Action: Sorts a so that a[0] <= a[1] <= ... <= a[numberUsed - 1]. */ public static void sort(double[] a, int numberUsed) int index, indexOfNextSmallest; for (index = 0; index < numberUsed - 1; index++) {//Place the correct value in a[index]: indexOfNextSmallest = indexOfSmallest(index, a, numberUsed); interchange(index,indexOfNextSmallest, a); //a[0] <= a[1] <=...<= a[index] and these are the smallest //of the original array elements. The remaining positions //contain the rest of the original array elements. } CSS161: Fundamentals of Computing

Code Code for Selection Sort (2 of 3) /** Returns the index of the smallest value among a[startIndex], a[startIndex+1], ... a[numberUsed - 1] */ private static int indexOfSmallest(int startIndex, double[] a, int numberUsed) { double min = a[startIndex]; int indexOfMin = startIndex; int index; for (index = startIndex + 1; index < numberUsed; index++) if (a[index] < min) min = a[index]; indexOfMin = index; //min is smallest of a[startIndex] through a[index] } return indexOfMin; CSS161: Fundamentals of Computing

Code for Selection Sort (3 of 3) /** Precondition: i and j are legal indices for the array a. Postcondition: Values of a[i] and a[j] have been interchanged. */ private static void interchange(int i, int j, double[] a) { double temp; temp = a[i]; a[i] = a[j]; a[j] = temp; //original value of a[i] } CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on textbook p378’s exercises 20 ~ 21. Trace Selection Sort with the following set of number: int a[9] = {4, 7, 8, 5, 6, 0, 1, 2, 3}; CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Enumerated Types Syntax enum Type_Name {VALUE_1, VALUE_2 …, VALUE_N}; Example enum WorkDay {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY}; Variables WorkDay meetingDay = null; meetingDay = WorkDay.THURSDAY; if ( meetingDay == WorkDay.MONDAY ) System.out.println( “A blue Monday” ); CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing An Enumerated Type CSS161: Fundamentals of Computing

Methods Included with Enumerated Type (1of 3) CSS161: Fundamentals of Computing

Methods Included with Enumerated Type (2 of 3) CSS161: Fundamentals of Computing

Methods Included with Enumerated Type (3 of 3) CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing The values( ) Method CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing The values( ) Method CSS161: Fundamentals of Computing

Enumerated Type in a switch Statement (1 of 3) CSS161: Fundamentals of Computing

Enumerated Type in a switch Statement (2 of 3) CSS161: Fundamentals of Computing

Enumerated Type in a switch Statement (3 of 3) CSS161: Fundamentals of Computing