lecture 12: pointers to functions...
TRANSCRIPT
![Page 1: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/1.jpg)
Hank Childs, University of OregonNovember 7, 2019
Lecture 12:Pointers to Functions
Complexity
__. __ .__ __. __ ___ ___ ______ ___ .__ __. _______ _______ ___ .___________. ___| | | | | \ | | | | \ \ / / / | / \ | \ | | | \ | \ / \ | | / \| | | | | \| | | | \ V / | ,----' / ^ \ | \| | | .--. | | .--. | / ^ \ `---| |----` / ^ \| | | | | . ` | | | > < | | / /_\ \ | . ` | | | | | | | | | / /_\ \ | | / /_\ \| `--' | | |\ | | | / . \ __ | `----.__ / _____ \ | |\ | | '--' | | '--' | / _____ \ | | / _____ \\______/ |__| \__| |__| /__/ \__\ (_ ) \______(_ ) /__/ \__\ |__| \__| |_______/ |_______/ /__/ \__\ |__| /__/ \__\
_______.___________..______ __ __ ______ .___________. __ __ .______ _______ _______. / | || _ \ | | | | / || || | | | | _ \ | ____| / | | (----`---| |----`| |_) | | | | | | ,----'`---| |----`| | | | | |_) | | |__ | (----` \ \ | | | / | | | | | | | | | | | | | / | __| \ \
.----) | | | | |\ \----.| `--' | | `----. | | | `--' | | |\ \----.| |____.----) ||_______/ |__| | _| `._____| \______/ \______| |__| \______/ | _| `._____||_______|_______/
![Page 2: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/2.jpg)
TalkaboutQ1frommidterm
![Page 3: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/3.jpg)
Project2G
![Page 4: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/4.jpg)
QuizonTuesday,Nov12
• TherewillbeaquizTuesdayNov12.• Itwillbeworth3points.• ItwillbeonfileI/O.• Nonotes.• Expectingyoutoknowfopen/fread/fwrite/etc.
![Page 5: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/5.jpg)
Reading:Chapters5and6
• Iwillbefollowingthetextbookmorecloselythenextfewweeks
![Page 6: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/6.jpg)
Callbyvalue/callbyreference
• Referstohowparametersarepassedtoafunction.– Callbyvalue:sendthevalueofthevariableasafunctionparameter• Sideeffectsinthatfunctiondon’t affectthevariableinthecallingfunction
– Callbyreference:sendareference(pointer)asafunctionparameter• Sideeffectsinthatfunctionaffectthevariableinthecallingfunction
![Page 7: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/7.jpg)
CallbyValue
Cdoesnotcarethatfoohasan
argumentvariablecalled“x”andmainhasan
automaticvariablecalled“x”.TheyhaveNOrelation.
![Page 8: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/8.jpg)
Callbyreference
![Page 9: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/9.jpg)
Preprocessor
• Preprocessor:– takesaninputprogram– producesanotherprogram(whichisthencompiled)
• Chasaseparatelanguageforpreprocessing– DifferentsyntaxthanC– Usesmacros(“#”)
macro(“macroinstruction”):ruleforreplacinginputcharacterswithoutputcharacters
![Page 10: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/10.jpg)
#include
• compilercanonlycompileonefileatafile• takesanotherfileandincludesitinthecurrentfile
• thefileisa“header”file– itcontainsfunctionprototypes– afunctionprototypedeclaresafunctionexists,butnothowitisimplemented
![Page 11: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/11.jpg)
Demonstrate#include
#include<stdio.h>int main(){printf("Helloworld\n");
}
%gcc –Eprintf.c
![Page 12: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/12.jpg)
FunctionPrototype
![Page 13: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/13.jpg)
PreprocessorPhases
• Resolve#includes• Conditionalcompilation(#ifdef)• Macroreplacement• Specialmacros
![Page 14: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/14.jpg)
#definecompilation
Thisisanexampleofmacroreplacement.
![Page 15: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/15.jpg)
#defineviagcc command-lineoption
![Page 16: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/16.jpg)
Conflicting–Dand#define
![Page 17: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/17.jpg)
Conditionalcompilation
![Page 18: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/18.jpg)
Conditionalcompilationcontrolledviacompilerflags
![Page 19: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/19.jpg)
FunctionPointers
• Idea:– Youhaveapointertoafunction– Thispointercanchangebasedoncircumstance–Whenyoucallthefunctionpointer,itislikecallingaknownfunction
![Page 20: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/20.jpg)
FunctionPointerExample
![Page 21: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/21.jpg)
FunctionPointerExample#2
Functionpointer Partoffunctionsignature
Don’tbescaredofextra‘*’s…theyjustcomeaboutbecauseofpointersintheargumentsorreturnvalues.
![Page 22: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/22.jpg)
Simple-to-ExoticFunctionPointerDeclarations
void(*foo)(void);void(*foo)(int **,char***);char**(*foo)(int **,void(*)(int));
Thesesometimescomeuponinterviews.
![Page 23: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/23.jpg)
FunctionPointersvs Conditionals
Whataretheprosandconsofeachapproach?
![Page 24: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/24.jpg)
Callbacks
• Callbacks:functionthatiscalledwhenaconditionismet– Commonlyusedwheninterfacingbetweenmodulesthatweredevelopedseparately.
– …librariesusecallbacksanddeveloperswhousethelibraries“register”callbacks.
![Page 25: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/25.jpg)
Callbackexample
![Page 26: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/26.jpg)
Callbackexample
![Page 27: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/27.jpg)
WhatisaDataStructure?
![Page 28: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/28.jpg)
WhatisaDataStructure?
• Datastructuredefinitions– Textbook:“asystematicwaytoorganizedata”–Wikipedia:“dataorganization,managementandstorageformatthatenablesefficientaccessandmodification”
![Page 29: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/29.jpg)
WeAreAlreadyFamiliarWithSomeDataStructures
• Arrays• FromPython– List– Tuple– Dictionary– Set
![Page 30: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/30.jpg)
KeyConcept
• Itorganizesdata• Itenablesefficientaccess–Whatdoesefficientmean??
![Page 31: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/31.jpg)
Example:DataStructure:ArraysOperation:Search
![Page 32: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/32.jpg)
IsItEfficient?:TwoSub-questions
• 1)Howlongdoesthistaketorun?– (howdowemeasurethis?)
• 2)Couldwedoitwithlessoperations?
![Page 33: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/33.jpg)
IsItEfficient?:TwoSub-questions
• 1)Howlongdoesthistaketorun?– (howdowemeasurethis?)
• 2)Couldwedoitwithlessoperations?
![Page 34: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/34.jpg)
HowLongDoesThisTakeToRun?
• Oneanswer:timeit!
![Page 35: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/35.jpg)
Unixcommand:time
Courtesy:Sventek textbook
![Page 36: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/36.jpg)
Anotheroption:addtimingstoyourprogram!
• (note:nowtakingafewslideaside)
![Page 37: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/37.jpg)
gettimeofday
(therearelotsofUnixsystemcalls,whichdolotsofdifferentthings)
![Page 38: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/38.jpg)
gettimeofday example
![Page 39: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/39.jpg)
gettimeofday example
![Page 40: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/40.jpg)
gettimeofday example
![Page 41: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/41.jpg)
gettimeofday example
![Page 42: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/42.jpg)
HowLongDoesThisTakeToRun?
• Oneanswer:timeit!• Question:whatifIchangethedata?–Whatifwehave8000studentsinsteadof80students?
• Newidea:analyzetheprogram
![Page 43: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/43.jpg)
AnalyzetheProgram
• Howmanyoperationsdoesthisperform?• Assumestrcmp takes50operations• AndnumStudents is80• Then~80*50=4000operation
![Page 44: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/44.jpg)
BigONotation(1/3):ImportantComputerScienceConcept
• Ifinputdatasizeis“N”,thenyoucandescribehowmanyoperationsoccurusingN
• If“numStudents”isN,andstrcmp takes50operations,then50N
![Page 45: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/45.jpg)
BigONotation(2/3):ImportantComputerScienceConcept
• Idea#1behindBigO:don’tworryaboutconstants• Idea#2behindBigO:justsayhowmanyoperationswithrespecttoN(numberofdataelements)
• Answer:O(N)– Thisissosimple,almostnotuseful… neednewexample!
![Page 46: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/46.jpg)
BigONotation(3/3):ImportantComputerScienceConcept
• Thisiscalled“AsymptoticAnalysis”–Why?… whathappenswhenNgets“large”?
![Page 47: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/47.jpg)
Morecomplexexample
• Howmanyoperationsnow?• numStudents*numStudents*50(about)• à O(n2)
![Page 48: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/48.jpg)
IsThisBetter?
• Howmanyoperationsnow?• numStudents*numStudents*25(about)• STILLà O(n2)
![Page 49: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/49.jpg)
ComparingOurTwoImplementations
• Answer:secondversionisabouttwiceasfast– ButbothO(n2),somaybenotahugedifference
![Page 50: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/50.jpg)
Newdatastructure:sortedarray
• Likeanarray,buteverythingissorted• Unsortedarray– int X[6]={4,1,3,7,9,2};
• Sortedarray– int X[6]={1,2,3,4,7,9};
![Page 51: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/51.jpg)
CanWeImproveOnThisOneWithAnUnsortedArray?
• Probablynot,but…
![Page 52: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/52.jpg)
ButWeCanImproveWithaSortedArray…
![Page 53: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/53.jpg)
Howmanyoperationsdidthistake?
• StartwithNelements• After1iteration,N/2elements• After2iterations,N/4elements• After3iterations,N/8elements• After4iterations,N/16elements• Etc…• Afterlog2Niterations,1element• à O(logn)
![Page 54: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/54.jpg)
Buttherewasworktotakeanarrayandmakeasortedarray
• Howlongdoesittaketosort?• Sort:complexityvaries:O(nlogn)toO(n2)– Project2Bexample:O(n2)– Mostrealworld:O(nlogn)
• So:wecoulddoIsStudentInClass onunsortedarrayforO(n)
• Or:sortarray(costO(nlogn))anddoIsStudentInClass forO(logn)
• à weonlywanttodoasortifweplantocallIsStudentInClass abunchoftimes
![Page 55: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/55.jpg)
CanwegetabetterBigOforthisone?
• ‘for’loopovernumStudents:O(n)• Combined:O(nlogn)+O(n)à O(nlogn)– (asymptoticanalysis)
![Page 56: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/56.jpg)
Courtesy:Sventek textbook
![Page 57: Lecture 12: Pointers to Functions Complexityix.cs.uoregon.edu/~Hank/212/Lectures/CIS212_F19_Lec12.pdfBig O Notation (1/3): Important Computer Science Concept •If input data size](https://reader033.vdocument.in/reader033/viewer/2022042212/5eb4b294d461183e56529abd/html5/thumbnails/57.jpg)
IsItEfficient?:TwoSub-questions
• 1)Howlongdoesthistaketorun?– (howdowemeasurethis?)
• 2)Couldwedoitwithlessoperations?
• Unsortedarraysweremoreefficientforsearch,buttherewasworkincreatingthem– Differentdatastructuresareefficientfordifferentoperations
– Hence,wehavealotofthem