CompSci 100e 12.1 Sorting: In 2 Slides l Why do people study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm analysis.

Slides:



Advertisements
Similar presentations
Chapter 12 Lists and Iterators. List Its an abstract concept not a vector, array, or linked structure. Those are implementations of a List. A list is a.
Advertisements

C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Chapter 1 Writing a Program Fall Class Overview Course Information –On the web page and Blackboard –
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
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.
CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
INTRODUCTION T.Najah Al_Subaie Kingdom of Saudi Arabia Prince Norah bint Abdul Rahman University College of Computer Since and Information System CS240.
Sorting CS221 – 3/2/09. Recursion Recap Use recursion to improve code clarity Make sure the performance trade-off is worth it Every recursive method must.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
Computer Science A 1: 3/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Discussion Section: HW1 and Programming Tips GS540.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Java and C++, The Difference An introduction Unit - 00.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Compsci 100, Fall ’.1 Views of programming l Writing code from the method/function view is pretty similar across languages  Organizing methods.
Instructor: Tina Tian. About me Office: RLC 203A Office Hours: Wednesday 1:30 - 4:30 PM or .
By – Tanvir Alam.  This tutorial offers several things.  You’ll see some neat features of the language.  You’ll learn the right things to google. 
Introduction. 2COMPSCI Computer Science Fundamentals.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
 200 Total Points ◦ 74 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 36 Points Short Answer ◦ 30 Points Multiple Choice.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Compsci 06/101, Spring What is Computing? Informatics? l What is computer science, what is its potential?  What can we do with computers in.
CompSci 100e 11.1 Sorting: From Theory to Practice l Why do we study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
CompSci 100e Program Design and Analysis II April 26, 2011 Prof. Rodger CompSci 100e, Spring20111.
Pointers OVERVIEW.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Software Design 8.1 A Rose by any other name…C or Java? l Why do we use Java in our courses (royal we?)  Object oriented  Large collection of libraries.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Arrays and Vectors Sequential (One-Dimensional) Containers Chapter 12 1.
2011-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, and Peter Andreae, VUW.
Compsci 06/101, Spring What is Computing? Informatics? l What is computer science, what is its potential?  What can we do with computers in.
Compsci 100, Spring What’s left to talk about? l Transforms  Making Huffman compress more  Understanding what transforms do Conceptual understanding,
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CPS 100, Fall What is Computing? Informatics? l What is computer science, what is its potential?  What can we do with computers in our lives?
CompSci 100e 11.1 Sorting: From Theory to Practice l Why do we study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm.
CompSci Problem: finding subsets l See CodeBloat APT, requires finding sums of all subsets  Given {72, 33, 41, 57, 25} what is sum closest (not.
 200 Total Points ◦ 75 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 35 Points Short Answer ◦ 30 Points Multiple Choice.
CompSci Sorting: From Theory to Practice  Why do we study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Peyman Dodangeh Sharif University of Technology Spring 2014.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Compsci 101, Fall LWoC l Review Recommender, dictionaries, files  How to create recommendations in order? food.txt  Toward a Duke eatery-recommender.
Compsci 06/101, Fall What is Computing? Informatics? l What is computer science, what is its potential?  What can we do with computers in our.
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Compsci 101.2, Fall Plan for LDO101 l Ethical webpage scraping  Illustrate power of regular expressions  Python makes trying things relatively.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
CMPT 201 Computer Science II for Engineers
Bottom up meets Top down
Eugene Hsu.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Views of programming Writing code from the method/function view is pretty similar across languages Organizing methods is different, organizing code is.
CS 2308 Final Exam Review.
CS 2308 Exam I Review.
Brought to you by C++ Tutorial Brought to you by
CSCE156: Introduction to Computer Science II
From bit to byte to char to int to long
Presentation transcript:

CompSci 100e 12.1 Sorting: In 2 Slides l Why do people study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm analysis in a simple, useful setting l There are n sorting algorithms, how many should we study?  O(n), O(log n), …  Why do we study more than one algorithm? Some are good, some are bad, some are very, very sad Paradigms of trade-offs and algorithmic design  Which sorting algorithm is best?  Which sort should you call from code you write?

CompSci 100e 12.2 Sorting out sorts l Simple, O(n 2 ) sorts --- for sorting n elements  Selection sort --- n 2 comparisons, n swaps, easy to code  Insertion sort --- n 2 comparisons, n 2 moves, stable, fast  Bubble sort --- n 2 everything, slow, slower, and ugly l Divide and conquer faster sorts: O(n log n) for n elements  Quick sort: fast in practice, O(n 2 ) worst case  Merge sort: good worst case, great for linked lists, uses extra storage for vectors/arrays l Other sorts:  Heap sort, basically priority queue sorting, Big-Oh?  Radix sort: doesn’t compare keys, uses digits/characters O( dn + kd )  Shell sort: quasi-insertion, fast in practice, non-recursive O( n 1.5 )

CompSci 100e 12.3 A Rose by any other name…C or Java? l Why do we use Java in our courses (royal we?)  Object oriented, large collection of standard libraries  Good for advanced programming and beginners l Why don't we use C++ (or C)?  Standard libraries weak or non-existent (comparatively)  No GUIs, complicated compilation model  How about Perl, Python, PHP, Ruby, Scheme, ML,… ?  Can we do something different in one language?

CompSci 100e 12.4 C++ on two slides l Classes are similar to Java, compilation model is different  Classes have public and private sections/areas  Typically declaration in.h file and implementation in.cpp Separate interface from actual implementation Good in theory, hard to get right in practice  One.cpp file compiles to one.o file To create an executable, we link.o files with libraries Hopefully someone else takes care of the details l We #include rather than import, this is a preprocessing step  Literally sucks in an entire header file, can take a while for standard libraries like iostream, string, etc.  No abbreviation similar to java.util.*;

CompSci 100e 12.5 C++ on a second slide l We don't have to call new to create objects, they can be created "on the stack"  Using new creates memory "on the heap"  In C++ we need to do our own garbage collection, or avoid and run out of memory (is this an issue?) l Vectors are similar to ArrayLists, pointers are similar to arrays  Unfortunately, C/C++ equate array with memory allocation  To access via a pointer, we don't use. we use -> l Streams are used for IO, iterators are used to access begin/end of collection  ifstream, cout correspond to Readers and System.out

CompSci 100e 12.6 How do we read a file in C++ and Java? Scanner s = new Scanner(new File(“data.txt”)); TreeSet set = new TreeSet (); while (s.hasNext()){ String str = s.next(); set.add(str); } myWordsAsList = new ArrayList (set); string word; set unique; ifstream input(“data.txt”); while (input >> word){ unique.insert(word); } myWordsAsVector = vector (unique.begin(), unique.end()); l What are similarities? Differences?

CompSci 100e 12.7 How do we read a file in C? FILE * file = fopen("/u/ola/data/poe.txt","r"); char buf[1024]; char ** words = (char **) malloc(5000*sizeof(char **)); int count = 0; int k; while (fscanf(file,"%s",buf) != EOF){ int found = 0; // look for word just read for(k=0; k < count; k++){ if (strcmp(buf,words[k]) == 0){ found = 1; break; } if (!found){ // not found, add to list words[count] = (char *) malloc(strlen(buf)+1); strcpy(words[count],buf); count++; } l What if more than 5000 words? What if string length > 1024? What if?  What is complexity of this code?

CompSci 100e 12.8 Why do we learn other languages? l Perl, Python, PHP, Ruby, C, C++, Java, Scheme, ML,  Can we do something different in one language? Depends on what different means. In theory: no; in practice: yes  What languages do you know? All of them.  In what languages are you fluent? None of them l In later courses why do we use C or C++?  Closer to the machine, we want to understand the machine at many levels, from the abstract to the ridiculous Or at all levels of hardware and software  Some problems are better suited to one language What about writing an operating system? Linux?

CompSci 100e 12.9 Unique words in Java import java.util.*; import java.io.*; public class Unique { public static void main(String[] args) throws IOException{ Scanner scan = new Scanner(new File("/data/melville.txt")); TreeSet set = new TreeSet (); while (scan.hasNext()){ String str = scan.next(); set.add(str); } for(String s : set){ System.out.println(s); }

CompSci 100e Bjarne Stroustrup, Designer of C++ l Numerous awards, engineering and science  ACM Grace Hopper l Formerly at Bell Labs  Now Texas A&M l “There's an old story about the person who wished his computer was as easy to use as his telephone. That wish has come true, since I no longer know how to use my telephone.” Bjarne Stroustrup

CompSci 100e Unique words in C++ #include using namespace std; int main(){ ifstream input("/data/melville.txt"); set unique; string word; while (input >> word){ unique.insert(word); } set ::iterator it = unique.begin(); for(; it != unique.end(); it++){ cout << *it << endl; } return 0; }

CompSci 100e PHP, Rasmus Lerdorf and Others l Rasmus Lerdorf  Qeqertarsuaq, Greenland  1995 started PHP, now part of it  l Personal Home Page  No longer an acronym l “When the world becomes standard, I will start caring about standards.” Rasmus Lerdorf

CompSci 100e Unique words in PHP <?php $wholething = file_get_contents("file:///data/melville.txt"); $wholething = trim($wholething); $array = preg_split("/\s+/",$wholething); $uni = array_unique($array); sort($uni); foreach ($uni as $word){ echo $word." "; } ?>

CompSci 100e Guido van Rossum l BDFL for Python development  Benevolent Dictator For Life  Late 80’s began development l Python is multi-paradigm  OO, Functional, Structured, … l We're looking forward to a future where every computer user will be able to "open the hood" of their computer and make improvements to the applications inside. We believe that this will eventually change the nature of software and software development tools fundamentally. Guido van Rossum, 1999!

CompSci 100e Unique Words in Python #! /usr/bin/env python import sys import re def main(): f = open('/data/melville.txt', 'r') words = re.split('\s+',f.read().strip()) allWords = set() for w in words: allWords.add(w) for word in sorted(allWords): print "%s" % word if __name__ == "__main__": main()

CompSci 100e Kernighan and Ritchie l First C book, 1978 l First ‘hello world’ l Ritchie: Unix too!  Turing award 1983 l Kernighan: tools  Strunk and White l Everyone knows that debugging is twice as hard as writing a program in the first place. So if you are as clever as you can be when you write it, how will you ever debug it? Brian Kernighan

CompSci 100e How do we read a file in C? #include int strcompare(const void * a, const void * b){ char ** stra = (char **) a; char ** strb = (char **) b; return strcmp(*stra, *strb); } int main(){ FILE * file = fopen("/data/melville.txt","r"); char buf[1024]; char ** words = (char **) malloc(5000*sizeof(char **)); int count = 0; int k;

CompSci 100e Storing words read when reading in C while (fscanf(file,"%s",buf) != EOF){ int found = 0; // look for word just read for(k=0; k < count; k++){ if (strcmp(buf,words[k]) == 0){ found = 1; break; } if (!found){ // not found, add to list words[count] = (char *) malloc(strlen(buf)+1); strcpy(words[count],buf); count++; } l Complexity of reading/storing? Allocation of memory?

CompSci 100e Sorting, Printing, Freeing in C qsort(words,count,sizeof(char *), strcompare); for(k=0; k < count; k++) { printf("%s\n",words[k]); } for(k=0; k < count; k++){ free(words[k]); } free(words); } l Sorting, printing, and freeing  How to sort? What’s analgous to comparator?  Why do we call free? Necessary in this program? Why?

CompSci 100e The exam l Friday, May 7, 7pm-10 in classroom l Open book/open note l ~40% multiple choice/short answer l Cumulative l By Monday, May 3:  All grades up (except huff)  Test solutions out  Grade problems: Submit assignment issues l Final grades up Monday, May 10 l Available by appointment throughout reading period and exam week