Reading from a file, Sorting, and a little Searching Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics,

Slides:



Advertisements
Similar presentations
Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.
Advertisements

Sorting I Chapter 8 Kruse and Ryba. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores –Words of.
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
J. Michael Moore Searching & Sorting CSCE 110. J. Michael Moore Searching with Linear Search Many times, it is necessary to search an array to find a.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small 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.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
Applications of Arrays (Searching and Sorting) and Strings
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
February 4, 2005 Searching and Sorting Arrays. Searching.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
General Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Binary Trees Priority Queues, and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer.
Maps and Dictionaries Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Review and Prepare for Test 1 Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
Review and Prepare for Test 3 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Big Oh Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
C++ and Ubuntu Linux Review and Practice CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Chapter 14: Searching and Sorting
CS1101: Programming Methodology
Bubble Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Priority Queues, Trees, and Huffman Encoding CS 244 This presentation requires Audio Enabled Brent M. Dingle, Ph.D. Game Design and Development Program.
HelloWorld – GameOver CS 244 – Data Structures and Algorithms Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout.
Makefiles, Geany CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Hanoi Towers Big Oh Recursion Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
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.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
Priority Queues and Sorting CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Searching with Trees CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
References and Pointers CS 244 Connect Speakers for this Presentation Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics,
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
Quicksort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Dynamic Arrays Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Take the Test Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Recursion CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
More on Merge Sort CS 244 This presentation is not given in class
Topic 14 Searching and Simple Sorts
Search,Sort,Recursion.
Review and Prepare for Test 2
Topic 14 Searching and Simple Sorts
Search,Sort,Recursion.
Data Structures and Algorithms CS 244
Presentation transcript:

Reading from a file, Sorting, and a little Searching Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount) Some content from Data Structures Using C++ (D.S. Malik)

Points of Note All things to date should now be graded. Test 1 grades are posted Test was handed back and gone over last class Come see me during office hours if you have any remaining questions Homework 7 was Due March 27 moved to March 30 to allow the use of the weekend Homework 8 is due April 3

Previously Test 1 Spring Break You are now back in class Recall Searching methods Linear (sequential) Search Binary Search Recall Sorting Methods Insertion Sort Selection Sort

For Today Reading data from a file Review: Sorting Data Insertion Selection Motivation: Recall binary search required data to be sorted So might want to be able to sort data

Marker Slide Any General Questions ? Next up Reading Data from a file Sorting Binary Search

Reading Data from a File A common programming task is to read data from a file. This can be accomplished in C++ as shown below // Program to demonstrate a way to read data from a file #include using namespace std; int main(int argc, char * argv[]) { // open the data.txt file for input std::fstream myfile("./data.txt", std::ios_base::in); float number; while (myfile >> number) { cout << number << " "; } cout << "\nProgram ends.\n"; myfile.close(); return 0; } Sample data.txt fileSample output

To Open a User Specified File To allow a user to specify a file to read from the command line you need to check that 2 arguments were provided and use the second one as the name of the data file argv[1] int main(int argc, char * argv[]) { if (argc >= 2) { cout << "Reading data from file: [" << argv[1] << "]\n"; std::fstream myfile(argv[1], std::ios_base::in); :

Marker Slide Questions on: Reading Data from a file Next up Sorting Selection Sort Insertion Sort Binary Search

Selection Sort Not so long ago we saw a selection sort algorithm It looked a lot like that to the right: void selectionSort(int data[], int n) { int smallestIndex; int smallest, saved; for (int i=0; i < n-1; i++) { // Find smallest element smallest = data[i]; smallestIndex = i; for (int j=i+1; j < n; j++) { if (data[j] < smallest) { smallest = data[j]; smallestIndex = j; } // Swap with smallest element saved = data[i]; data[i] = data[smallestIndex]; data[smallestIndex] = saved; }

Setup for Class Activity Let data[] be defined to hold: 22, 15, 44, 10, 3, 9, 13, 25 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] plus one line before entry 8 lines of output void selectionSort(int data[], int n) { int smallestIndex; int smallest, saved; for (int i=0; i < n-1; i++) { // Find smallest element smallest = data[i]; smallestIndex = i; for (int j=i+1; j < n; j++) { if (data[j] < smallest) { smallest = data[j]; smallestIndex = j; } // Swap with smallest element saved = data[i]; data[i] = data[smallestIndex]; data[smallestIndex] = saved; }

Class Participation Activity Let data[] be: 22, 15, 44, 10, 3, 9, 13, 25 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output void selectionSort(int data[], int n) { int smallestIndex; int smallest, saved; for (int i=0; i < n-1; i++) { // Find smallest element smallest = data[i]; smallestIndex = i; for (int j=i+1; j < n; j++) { if (data[j] < smallest) { smallest = data[j]; smallestIndex = j; } // Swap with smallest element saved = data[i]; data[i] = data[smallestIndex]; data[smallestIndex] = saved; } For each iteration of the outer for-loop write the contents of data[] plus one line before entry 8 lines of output Take 5 minutes to finish this out

Class Activity Let data[] be: 22, 15, 44, 10, 3, 9, 13, 25 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output void selectionSort(int data[], int n) { int smallestIndex; int smallest, saved; for (int i=0; i < n-1; i++) { // Find smallest element smallest = data[i]; smallestIndex = i; for (int j=i+1; j < n; j++) { if (data[j] < smallest) { smallest = data[j]; smallestIndex = j; } // Swap with smallest element saved = data[i]; data[i] = data[smallestIndex]; data[smallestIndex] = saved; } enhanced version

Class Activity Let data[] be: 22, 15, 44, 10, 3, 9, 13, 25 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output void selectionSort(int data[], int n) { int smallestIndex; int smallest, saved; for (int i=0; i < n-1; i++) { // Find smallest element smallest = data[i]; smallestIndex = i; for (int j=i+1; j < n; j++) { if (data[j] < smallest) { smallest = data[j]; smallestIndex = j; } // Swap with smallest element saved = data[i]; data[i] = data[smallestIndex]; data[smallestIndex] = saved; } enhanced version

Class Activity Let data[] be: 22, 15, 44, 10, 3, 9, 13, 25 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output void selectionSort(int data[], int n) { int smallestIndex; int smallest, saved; for (int i=0; i < n-1; i++) { // Find smallest element smallest = data[i]; smallestIndex = i; for (int j=i+1; j < n; j++) { if (data[j] < smallest) { smallest = data[j]; smallestIndex = j; } // Swap with smallest element saved = data[i]; data[i] = data[smallestIndex]; data[smallestIndex] = saved; } enhanced version

Class Activity Let data[] be: 22, 15, 44, 10, 3, 9, 13, 25 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output void selectionSort(int data[], int n) { int smallestIndex; int smallest, saved; for (int i=0; i < n-1; i++) { // Find smallest element smallest = data[i]; smallestIndex = i; for (int j=i+1; j < n; j++) { if (data[j] < smallest) { smallest = data[j]; smallestIndex = j; } // Swap with smallest element saved = data[i]; data[i] = data[smallestIndex]; data[smallestIndex] = saved; } enhanced version

Class Activity Let data[] be: 22, 15, 44, 10, 3, 9, 13, 25 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output void selectionSort(int data[], int n) { int smallestIndex; int smallest, saved; for (int i=0; i < n-1; i++) { // Find smallest element smallest = data[i]; smallestIndex = i; for (int j=i+1; j < n; j++) { if (data[j] < smallest) { smallest = data[j]; smallestIndex = j; } // Swap with smallest element saved = data[i]; data[i] = data[smallestIndex]; data[smallestIndex] = saved; } enhanced version

Class Activity Let data[] be: 22, 15, 44, 10, 3, 9, 13, 25 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output void selectionSort(int data[], int n) { int smallestIndex; int smallest, saved; for (int i=0; i < n-1; i++) { // Find smallest element smallest = data[i]; smallestIndex = i; for (int j=i+1; j < n; j++) { if (data[j] < smallest) { smallest = data[j]; smallestIndex = j; } // Swap with smallest element saved = data[i]; data[i] = data[smallestIndex]; data[smallestIndex] = saved; } enhanced version

Class Activity Let data[] be: 22, 15, 44, 10, 3, 9, 13, 25 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output void selectionSort(int data[], int n) { int smallestIndex; int smallest, saved; for (int i=0; i < n-1; i++) { // Find smallest element smallest = data[i]; smallestIndex = i; for (int j=i+1; j < n; j++) { if (data[j] < smallest) { smallest = data[j]; smallestIndex = j; } // Swap with smallest element saved = data[i]; data[i] = data[smallestIndex]; data[smallestIndex] = saved; } and there it is. done. enhanced version

Why Is It Named Selection Sort It repeatedly selects the least element void selectionSort(int data[], int n) { int smallestIndex; int smallest, saved; for (int i=0; i < n-1; i++) { // Find smallest element smallest = data[i]; smallestIndex = i; for (int j=i+1; j < n; j++) { if (data[j] < smallest) { smallest = data[j]; smallestIndex = j; } // Swap with smallest element saved = data[i]; data[i] = data[smallestIndex]; data[smallestIndex] = saved; }

Selection Sort Selection Sort repeatedly selects the element of least value that has not previously been selected | | | | | | | | ================================================

Selection Sort | | | | | | | | Observing the “selection” process can be visually illustrated So X out the red numbers 1 at a time – use line 1 ================================================

Selection Sort | | | | | | | | ================================================ Observing the “selection” process can be visually illustrated So X out the red numbers 1 at a time – use line 1

Selection Sort | | | | | | | | ================================================ Observing the “selection” process can be visually illustrated So X out the red numbers 1 at a time – use line 1

Selection Sort | | | | | | | | ================================================ Observing the “selection” process can be visually illustrated So X out the red numbers 1 at a time – use line 1

Selection Sort | | | | | | | | ================================================ Observing the “selection” process can be visually illustrated So X out the red numbers 1 at a time – use line 1

Selection Sort | | | | | | | | ================================================ Observing the “selection” process can be visually illustrated So X out the red numbers 1 at a time – use line 1

Selection Sort | | | | | | | | ================================================ Observing the “selection” process can be visually illustrated So X out the red numbers 1 at a time – use line 1

Selection Sort | | | | | | | | ================================================ Observing the “selection” process can be visually illustrated So X out the red numbers 1 at a time – use line 1

Marker Slide Questions on: Reading Data from a file Sorting Selection Sort Next up Sorting Insertion Sort Binary Search

Insertion Sort Not long ago we saw an insertion sort algorithm It looked a lot like that to the right Watch the animation See how it inserts the current number into the correct position void insertion_sort(int x[],int N) { int key,i; for(int j=1; j < N; j++) { key = x[j]; i = j - 1; while ( (x[i] > key) && (i >= 0) ) { x[i+1] = x[i]; i--; } x[i+1] = key; }

Setup for Class Activity Let data[] be defined to hold: 6, 5, 3, 1, 8, 7, 2, 4 And thus N = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] plus one line before entry 8 lines of output void insertion_sort(int x[],int N) { int key,i; for(int j=1; j < N; j++) { key = x[j]; i = j - 1; while ( (x[i] > key) && (i >= 0) ) { x[i+1] = x[i]; i--; } x[i+1] = key; }

Class Activity Let data[] be: 6, 5, 3, 1, 8, 7, 2, 4 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output void insertion_sort(int x[],int N) { int key,i; for(int j=1; j < N; j++) { key = x[j]; i = j - 1; while ( (x[i] > key) && (i >= 0) ) { x[i+1] = x[i]; i--; } x[i+1] = key; }

Class Activity Let data[] be: 6, 5, 3, 1, 8, 7, 2, 4 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output void insertion_sort(int x[],int N) { int key,i; for(int j=1; j < N; j++) { key = x[j]; i = j - 1; while ( (x[i] > key) && (i >= 0) ) { x[i+1] = x[i]; i--; } x[i+1] = key; } For each iteration of the outer for-loop write the contents of data[] plus one line before entry 8 lines of output Take 5 minutes to finish this out

Class Activity Let data[] be: 6, 5, 3, 1, 8, 7, 2, 4 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output enhanced version void insertion_sort(int x[],int N) { int key,i; for(int j=1; j < N; j++) { key = x[j]; i = j - 1; while ( (x[i] > key) && (i >= 0) ) { x[i+1] = x[i]; i--; } x[i+1] = key; }

Class Activity Let data[] be: 6, 5, 3, 1, 8, 7, 2, 4 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output enhanced version void insertion_sort(int x[],int N) { int key,i; for(int j=1; j < N; j++) { key = x[j]; i = j - 1; while ( (x[i] > key) && (i >= 0) ) { x[i+1] = x[i]; i--; } x[i+1] = key; }

Class Activity Let data[] be: 6, 5, 3, 1, 8, 7, 2, 4 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output enhanced version void insertion_sort(int x[],int N) { int key,i; for(int j=1; j < N; j++) { key = x[j]; i = j - 1; while ( (x[i] > key) && (i >= 0) ) { x[i+1] = x[i]; i--; } x[i+1] = key; }

Class Activity Let data[] be: 6, 5, 3, 1, 8, 7, 2, 4 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output enhanced version void insertion_sort(int x[],int N) { int key,i; for(int j=1; j < N; j++) { key = x[j]; i = j - 1; while ( (x[i] > key) && (i >= 0) ) { x[i+1] = x[i]; i--; } x[i+1] = key; }

Class Activity Let data[] be: 6, 5, 3, 1, 8, 7, 2, 4 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output enhanced version void insertion_sort(int x[],int N) { int key,i; for(int j=1; j < N; j++) { key = x[j]; i = j - 1; while ( (x[i] > key) && (i >= 0) ) { x[i+1] = x[i]; i--; } x[i+1] = key; } Notice we are really INSERTING the 4 where it belongs in the list We just shifted stuff to make a place for the 4 to be inserted

Class Activity Let data[] be: 6, 5, 3, 1, 8, 7, 2, 4 And thus n = 8 Execute the function on that input data For each iteration of the outer for-loop write the contents of data[] 8 lines of output enhanced version void insertion_sort(int x[],int N) { int key,i; for(int j=1; j < N; j++) { key = x[j]; i = j - 1; while ( (x[i] > key) && (i >= 0) ) { x[i+1] = x[i]; i--; } x[i+1] = key; } and there it is. done.

Marker Slide Questions on: Reading Data from a file Sorting Selection Sort Insertion Sort Next up Binary Search

Uses a divide and conquer methodology Requires the data to be sorted Runs in O(lg n) time Human World Example of divide and conquer Looking up a word in a dictionary How do we implement it? Let’s assume we have a sorted array of data Perhaps read from a file… [next slide]

Code for Binary Search Assuming theArray is sorted and size is correct This code will perform a binary search looking for the value of findMe The cout is mostly for debugging and illustration // bool BinSearchFor(int findMe, int* theArray, int size) { int left = 0; int right = size - 1; bool found = false; int i; while ((!found) && (left != right)) { i = left + (right - left)/2; // truncates decimal part cout << "checking index = " << i << endl; if(findMe == theArray[i]) { found = true; } else if (findMe < theArray[i]) { right = i; } else { left = i+1; } } return found; }

Graded In-Class Assignment: BinarySearch Download the files: ICA201_BinarySearch.cpp ICA201_data.txt Modify/Correct the CPP file so it reads integer values from a file, sorts them, and then can do a search for a hard coded value Test your results Compile it, Run it When satisfied with it (or out of time) upload the resulting FIXED code to the appropriate D2L dropbox You can work in groups BUT each individual must submit something to D2L

The End (of this part) Of this part There may be more