Memory Layout for a Matrix class Matrix { double ** contents; int numRows, numCols; … } to initialize: contents = new double * [numRows]; for(int i=0;i<numRows;i++)

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void.
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
An intro to programming concepts with Scratch Session 8 of 10 sessions Working with lists; sorting a list.
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.
The University of Adelaide, School of Computer Science
Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : 1.Start at the beginning of the data set. 2.Compare the first two elements,
The University of Adelaide, School of Computer Science
Bubble Sort Algorithm It is so named because numbers (or letters) which are in the wrong place “bubble-up” to their correct positions (like fizzy lemonade)
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
CS 171: Introduction to Computer Science II Simple Sorting Ymir Vigfusson.
CSE 373: Data Structures and Algorithms
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Multiple-Subscripted Array
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
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.
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
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.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
Chapter 6- Arrays. Overview n What are arrays? n Declaring/initializing arrays. n Using arrays. n Arrays details. n Using arrays with classes/ in classes.
CSE 373 Data Structures and Algorithms
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Decision Maths 1 Sorting Algorithm Shuttle Sort A V Ali : 1.Compare items 1 and 2; swap them if necessary 2.Compare 2 and 3; swap.
Arrays. 2 Till now we are able to declare and initialize few variables Reality: need to compute on a large amount of data Arrays are data structures that.
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
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.
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
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.
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.
Vectors, Lists, Sequences. Vectors Linear sequence s of n elements e rank – number of elements before e in s Vector supports access to elements via their.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Recursion Method calls itself iteratively until a base case is met and usually containing the following: if-else for base case with return value increment/decrement.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
String as Arrays, Array Sorting C++ Programming Technologies.
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
In the first pass, the first two numbers are compared. The shuttle sort compares the numbers in pairs from left to right exchanging when necessary.
Objects with Functions and Arrays. Objects can be Passed Class defines type – Can use as type of function or parameter.
Multidimensional Arrays As Parameter void fun ( int matrix [] [10] ) {…} void main ( ) { int mat[5][10]; … fun(mat); } void fun (float *mat_ptr, int num_rows,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Dr. Sajib Datta Feb 14,  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for.
Sorting Slowly. Swap (used in bubble sort and selectSort) public static void swap(int[] array, int a, int b ) { //save a int temp = array[a]; //copy b.
Bubble sort. Quite slow, but simple Principles: Compare 2 numbers next to each other (lets call it current and the one next to it) If the current number.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Introduction to Programming
Sorting Algorithms.
Shuttle Sort Example 1st pass Comparisons: 1
CS Two Basic Sorting Algorithms Review Exchange Sorting Merge Sorting
Shaker.
Introduction to Sorting Algorithms
Shuttle Sort Example 1st pass Comparisons: 1
Agenda About the homework EvensAndOdds Array class Array Sorting
ㅎㅎ Fourth step for Learning C++ Programming Call by value
Presentation transcript:

Memory Layout for a Matrix class Matrix { double ** contents; int numRows, numCols; … } to initialize: contents = new double * [numRows]; for(int i=0;i<numRows;i++) contents[i] = new double[numCols] contents numRows numCols contents[0] contents[1] contents[0][0]contents[1][2] double **double *double

Naïve Vector Copy class Vector { double * contents; int length; … } contents contents[0] double * 0xFF Vector f() { Vector v(3); // … return v; } Vector v 0xFF2 Copy of Vector v

Copy Constructor class Vector { double * contents; int length; … } contents contents[0] double * 0xF Vector::Vector(const Vector & init) { length = init.length; contents = new double[length]; assert(contents != 0); for (int i = 0; i < length; i++) contents[i] = init.contents[i]; } Vector v 0xFF6 Copy of Vector v

Bubble Sort (1 st Pass) Original Array, Sort Ascending Compare Compare & swap Compare Compare & swap Compare & swap Compare & swap End of 1st pass

Bubble Sort (2 nd Pass) Compare & swap Compare Compare & swap Compare & swap Compare Compare End of 2 nd pass Make up to n passes, or until no swap in a pass.