CS150 Introduction to Computer Science 1

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Lecture 22: Arrays (cont). 2 Lecture Contents: t Searching in array: –linear search –binary search t Multidimensional arrays t Demo programs t Exercises.
Data Structures Arrays and Structs Chapter The Array Data Type t Array elements have a common name –The array as a whole is referenced through.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Sorting Algorithms Selection, Bubble, Insertion and Radix Sort.
1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
Algorithm Analysis and Big Oh Notation Courtesy of Prof. Ajay Gupta (with updates by Dr. Leszek T. Lilien) CS 1120 – Fall 2006 Department of Computer Science.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Chapter 8 ARRAYS Continued
Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
CS 162 Intro to Programming II Sorting Introduction & Selection Sort 1.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Arrays as Function Parameters. CSCE 1062 Outline  Passing an array argument (section 9.3)  Reading part of an array (section 9.4)  Searching and sorting.
Common Elementary Algorithms Some of the basic but frequently used algorithms for manipulating arrays. These algorithms are so important that: a)Some programming.
Sort Algorithm.
Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort
Searching and Sorting Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Searching and Sorting Arrays
Data Structures I (CPCS-204)
Michele Weigle - COMP 14 - Spr 04 Catie Welsh March 30, 2011
Introduction to Search Algorithms
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 9: Searching, Sorting, and Algorithm Analysis
Statistics in Science.
C++ Programming Lecture 15 Arrays – Part II
Algorithm Analysis and Big Oh Notation
Searching and Sorting Linear Search Binary Search ; Reading p
C++ Programming Lecture 15 Arrays – Part II
Selection Sort – an array sorting algorithm
Quicksort analysis Bubble sort
Sorting Algorithms.
Chapter 8 Arrays Objectives
CS150 Introduction to Computer Science 1
CISC181 Introduction to Computer Science Dr
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Topics discussed in this section:
Intro to Sorting Sorting
24 Searching and Sorting.
Chapter 4.
CS150 Introduction to Computer Science 1
Chapter 9: Data Structures: Arrays
Chapter 8 Arrays Objectives
CS150 Introduction to Computer Science 1
Algorithm Analysis and Big Oh Notation
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Searching and Sorting Arrays
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Sorting Algorithms.
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
Workshop for CS-AP Teachers
Chapter 19 Searching, Sorting and Big O
Introduction to Sorting Algorithms
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
Vectors.
CS Problem Solving and Object Oriented Programming Spring 2019
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

CS150 Introduction to Computer Science 1 Summary Assignment 5 due today Exam 3 on Friday, November 14th, 2003 8/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Write a function to return the index of the smallest element in a subarray. A subarray is a section of an array. The subarray is determined by its starting and ending indexes. The function will have the following arguments: The array, The starting index of the subarray, The ending index of the subarray, The index of the smallest element. 8/2/2018 CS150 Introduction to Computer Science 1

Function findIndexOfMin void findIndexOfMin(const int x[], int startIndex, int endIndex, int& index) { index = startIndex; for(int i=startIndex + 1; i <= endIndex; i++) if(x[i] < x[index]) index = i; } 8/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Sorting Arrays Some programs run more efficiently if their data is sorted in ascending or descending order. Can you think of a way to sort an array? 8/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Selection Sort Algorithm: Starting with the fist item in the array and ending with the next to last item: Set i equal to the index of the first item in the subarray to be processed in the next steps. Find the index of the smallest item in the subarray with indexes ranging from i through n-1. Exchange the smallest item found in the previous step with item i. Write the function that performs the selection sort using the algorithm above. 8/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Selection Sort void selectionSort(int items[], int n) { int minSub; for(int i=0; i<n-1; i++) findIndexOfMin(items, i, n-1, minSub); exchange(items[minSub], items[i]); } 8/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Selection Sort void exchange(int & num1, int & num2) { int temp; temp = num1; num1 = num2; num2 = temp; } 8/2/2018 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Write a program to calculate the mean, median and mode of an array containing 50 numbers. The numbers in the array can only range from 0 - 9 The program should have the following functions: mean: return the average median: sort array and determine value of median element mode: most frequent response selectionSort, exchange, findIndexOfMin printArray: display the array, 10 elements per line 8/2/2018 CS150 Introduction to Computer Science 1