External vs Internal Representation of Data External ● Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ● Sign/Magnitude Notation: + or , immediately to the next of digits ● Decimal Notation: Digits with a decimal point ● Letters and Symbols: A > Z, a > z, @, $, %, &, *, etc ● Colors: Blue, Green, Orange, Purple, Red, etc ● Audio Internal  Binary ● 0 > Logical False / Off ● 1 > Logical True / On ● A Binary number stored in the memory of a computer takes on meaning only because it is used in a certain way Vocabulary Decimal (dec.): These are decimal systems that use zero to nine. The prefix dec means ten Digit: Each number in the Decimal Number System, zero to nine Binary (bin. ): basetwo numeral system that uses zero and one. The smallest unit of information a computer can process Byte: Eight bits. Each cell or address in Main Memory contains one byte of data Octal (oct. ): Baseeight numeral system that uses zero to seven Hexadecimal (hex. ): Base 16 numeral system that uses zero to nine and A to F Nybble: Half of one byte or four bits Storage ● The number of different values that can be stored using n bits is 2n ● Because a byte is an exceedingly small number of memory, we use larger units ● When going from x bit sequence to y bit sequence, memory is changed 2xy times ● Eg: When upgrading from 8 bit to 12 bit, you get 2128 = 24 = 16 times more memory Units Actual # of Bytes Actual # of Bytes Approximate # of (Exponent) (Decimal) Bytes Bytes 20 1 One KiloBytes (KB) 210 1024 One Thousand MegaBytes (MB) 220 1,048,576 One Million GigaBytes 230 1,073,741,824 One Billion TeraBytes 240 1,099,511,627,776 One Trillion Converting Numbers Example(s) 1. Express the Base10 number 23,745 as a sum of the Base10 numeral system 2 3,7 4 5 104 103 102 101 100 2(10,000) + 3(1000) + 7(100) + 4(10) +5(1) 20000 + 3000 + 700 + 40 + 5 Binary to Decimal Example(s) 1. Convert 101101BIN to a Decimal number 1 0 1 1 0 1 25 24 23 22 21 20 32 16 8 4 2 1 32 + 8 + 4 + 1 45 2. Convert 1110BIN to a Decimal number 1 1 1 0 23 22 21 20 8 4 2 1 8+4+2 14 Converting Binary to Decimal Example(s) 1. Convert 102DEC as a Binary number Decimal Base Quotient Remainder 102 2 51 0 51 2 25 1 25 2 12 1 12 2 6 0 6 2 3 0 3 2 1 1 1 2 0 1 Read the remainders from the bottom to top 1100110 2. Convert 78DEC as a Binary number Decimal Base Quotient Remainder 78 2 39 0 39 2 19 1 19 2 9 1 9 2 4 1 4 2 2 0 2 2 1 0 1 2 0 1 1001110 Overflow: Attempts to represent a value that exceeds the maximum allowed values Java Integers are 4 Bytes, allowing integers in the range 2,147,483,648 to 2,147,483,647. Because most computed numbers are within this range and making it infinite would be impractical for most users Examples: 1. Convert 102DEC to a 4Bit Binary Number 1100110BIN, 7 bits Overflow Maximum possible values = 24 = 16 possible values (0  15) 2. Convert 102DEC to a 12Bit Binary Digits 1100110BIN, 12  7 = 5 Add 5 extra bits (only zeroes) in the front of the number so that the original value doesn’t change 000001100110 0110101 Hexadecimal Numbers: ● Hexadecimal (Base16) is a common number system in Computer Science. Since there are only 10 digits (0  9, the first 6 letters (A  F) are used to represent the remaining six characters ● Each character in HEX represents four Bits (nybble). To represent a full byte, two hexadecimal characters are used. These range from 00 (0) to FF (255) ● Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Hexadecimal Decimal Binary 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111 Hexadecimal to Binary: ● To convert hexadecimal to binary, look at each nybble individually Example(s) 1. Convert D3BHEX to a Binary Number D 3 B 13 3 11 110100111011 2. Convert 40FHEX to a Binary Number 4 0 F 4 0 15 010000001111 Convert hexadecimal to decimal: Example(s): 1. Convert 40FHEX to a Decimal number 4 0 F 4 0 15 162 161 160 4(256) + 0(16) + 15(1) 1039 Convert Binary to Hexadecimal: Example(s): 1. Convert 100111000001BIN to a Hexadecimal Number 1001 1100 0001 9 12 1 9 C 1 9C1HEX 2. Convert 011000111110BIN to a Hexadecimal Number 0110 0011 1110 6 3 14 6 3 E 63EHEX Converting Decimal to Hexadecimal ● To convert Decimal to Hexadecimal, we will follow the same process as converting Decimal to Binary, except we will set our base 16 rather than 2 1. Convert 5236DEC to a Hexadecimal Number Decimal Base Quotient Remainder 5236 16 327 4 327 16 20 7 20 16 1 4 1 16 0 1 1474HEX Convert Octal to Decimal Base 8 or 23 To convert to binary, each octal digit becomes 3 binary digits Example(s): 1. Convert 174OCT to a Decimal Number 1 7 4 001 111 100 4(80) + 7(81) + 1(82) 4 + 56 + 64 124DEX Convert from Decimal to Octal ● To convert Decimal to Octal, follow the same process as converting Decimal to Binary or Decimal to Hexadecimal. Except, use 8 as your base ● Students should be able to convert numbers from any base to another base Adding Binary Numbers Example(s): 0+0=0 0+1=1 1+0=1 1 + 1 = 10 (write 0 and carry over 1) 1. Find the sum of 111BIN and 1110BIN 1 1 1 0 0 1 1 1 0 1 1 1 0 1 0 1 0 1 2. Find the sum of 0110101BIN and 0011101BIN 1 1 1 1 1 0 1 1 0 1 0 1 0 0 1 1 1 0 1 1 0 1 0 0 1 0 Sign/Magnitude Notation ● Sign/Notation allows the representation of both positive and negative numbers ● Sign/Magnitude Notation requires one additional bit where ● Binary 0 > Positive ● Binary 1 > Negative ● The leftmost bit is used to represent the sign Example(s) 1. Convert 5DEC to a Binary number using Sign/Magnitude Notation 101BIN > 0101BIN 2. Convert 5DEC to an 8Bit Binary Number using Sign/Magnitude Notation 101BIN > 10000101BIN Two’s Complement Representation ● Two’s Complement Representation is one of the most used signed integer representation 3Bit Two’s Complement Representation Binary Integer 000 0 001 1 010 2 011 3 100 4 101 3 110 2 111 1 If the int is positive, it is the same as the binary representation If the int is negative, convert the positive to binary, invert all 0s and 1s, add 1 to the new number Unlike sign/magnitude notation, arithmetic works in Two’s Complement Representation Example(s) 010(1) + 110(2) = 000 Fractional Numbers ● Fractional Numbers can be represented in Binary by converting the number to Scientific Notation and using a SignedInteger Technique ● We will use the following criteria when representing fractional numbers in Binary  16 Bits  Sign/Magnitude Notation  Scientific Notation: ±M x BE ● When the binary is stored in a computer, there are no decimals ● 1 as a fractional number is 1100000000000000 1Bit 9Bits 1Bit 5Bits Sign of M Mantissa Sign of E Exponent 1 100000000 0 00000 Negative Binary1 Positive Binary 0 Example(s) 1. 5.75 5 . 7 5 100 101 102 5 + 7(1/10) + 5(1/100) = 5 75/100 = 5.75 2. 11.101 1 1 . 1 0 1 21 20 21 22 23 2 + 1 + ½ + 0(¼) + ⅛ = 3.625 Example(s) 1. Convert 5.75 to a binary number 5.75 5DEC = 101BIN 0.75 x 2 = 1.5 0.5 x 2 = 1 0.75DEC = 0.11BIN 5DEC + 0.75DEC = 101BIN + 0.11BIN = 101.11BIN 101.11 to scientific notation 3 places before decimal point 3DEC = 11BIN Scientific notation = 0.10111 * 211 (11 in Binary, not 11 in Decimal) Sign of Mantissa Mantissa Sign of Exponent Exponent 0 (positive) 101110000 0 (positive) 00011 In mantissa, filler zeroes go at the end In exponent, filler zeroes go at the front Final answer: 0101110000000011 2. Convert 17.625 to a Binary Number 17DEC = 10001 0.625 x 2 = 1.25 0.25 x 2 = 0.5 0.5 x 2 = 1 0.625DEC = 0.101BIN 17.625DEC = 10001.101BIN 5 places before decimal Scientific notation: 0.10001101 x 2101 Sign of Mantissa Mantissa Sign of Exponent Exponent 1 (negative) 100011010 0 (positive) 00101 Final answer = 1100011010000101 3. Convert 1110001110000101 to a decimal number Sign of Mantissa Mantissa Sign of Exponent Exponent 1 (negative) 110001110 0 (positive) 00101 Exponent = 101BIN = 5DEC 0.110001110 x 25 11000.1110 11000 = 24 0.111 = (1(1) + 1(2) + 1(4))/23 = ⅞ = 0.875 Final answer: 24.875 RoundOff Errors: Occurs because of the way numbers with decimal points are stored in the computer. This imprecise nature can cause rounding errors and possibly inaccurate results in your program Textual Information ● Computers can only understand numbers, so letters and symbols must be converted to numbers Code Mapping: Assigns each printable letter, symbol, emoji in our system a unique number ● ASCII (American Standard Code for Information Interchange is an 8 Bit mapping. One is a parity bit for error checking. Thus, ASCII is identified as a 7Bit mapping ● Unicode is a 16Bit mapping with a 32Bit variant ● For example, the ASCII representation of Dog is 01000100 01101111 01100111 Example(s) 1. How much storage is required for a Novel containing 100,000 words using an ASCII (8Bit) Character Mapping? Assume the average word length is 5 and ignore spaces and special characters Total bits = 100,000 x 5 x 8 = 4,000,000 Bits 4,000,000 bits = 500,000 bytes 1 Kilobyte = 1024 Bytes 500,000 Bytes = 500,000/1024 = 488.28 KB Final answers must be rounded to 2 Decimal places Do not round off until reaching the final answer Sound ● Sound is an analog sound ● Sound is digitized using a technique called sampling Sampling: At fixed time intervals, the amplitude of the signal is measured and stored as an integer value ● Accuracy of original sound depends on two key parameters:  Sampling Rate: Times per second the amplitude was sampled  Bit Depth: Number of Bits used to encode each sample Example(s) 1. How much storage is required for a oneminute MP3 file using a Bit Depth of 16 Bits per Second? MP3 uses 44,100 samples per second Total bits = 44,100 * 16 * 60 = 42, 336, 000 bits 42, 336, 000 Bits/8 = 5,292,000 Bytes 5/292,000/1024 = 5167.96875 KB 5167.96875 Kilobytes / 1024 = 5.05 MB Color ● The human eye primarily detects red, green, and blue. Other colors are a combination of these three colors in different amounts ● The early color representation used a Byte (8Bits) for using three bits of red, three for green, and two for blue. RRRGGGBB. Only 128 Colors could be represented using 1 Bit ● Today, computers use 1 Byte for each color RRRRRRRRGGGGGGGGBBBBBBBB ● We can create  256 shades of Red  256 shades of Green  256 shades of Blue  16, 777,216 total colors ● The binary representation of these colors would be very long, so this is an example where hexadecimal equivalents are commonly used Pixel: Short for picture element, the pixel is the basic unit of color on a computer display ● The size of pixels on a screen varies depending on the display’s resolution ● Each pixel is represented as binary numbers ● Large pixels can make an image look blocky, a phenomenon known as pixelation Boolean Logic: Area of mathematics that deals with rules for manipulating the two logical values, true or false. ● Boolean expressions were introduced in the mid1800s by George Boole ● Boolean logic has become prevalent in digital electronics and programming ● Boolean Logic is used to construct circuits that perform operations  true  Binary 1  false  Binary 0 ● There are three basic operations in Boolean Logic AND Conjunction & && • OR Disjunction   + NOT Negation ~ ! ¬ ● We will also use a fourth operator, exclusive or, or symbolized as XOR AND  Binary Operator (Requires two operands) ● When an expression uses an AND operator, then both sides of the expression must be true for the entire expression to evaluate to true ● The expression a AND b is true only if both a and b are true Example(s): To be the President of the United States, one must be at least 35 years old AND be a naturalborn citizen OR  Binary Operator (Requires two operands) ● When the OR operation is used between two expressions, then only one thing has to be true for the entire expression to evaluate to true. ● If both things are true then the expression satisfies the requirement for at least one thing be true, so it evaluates to true ● The only way an expression containing OR operator can evaluate to false is if all included statements are false ● In the expression a OR b, if a is true, the entire expression is true regardless of what b is. It is also true if b is also true, regardless of a is true or false Example(s): The requirements to see an Rrated movie are that you must be 17 years old OR be accompanied by someone who is 21 years old or older NOT  Unary Operator (Requires one operand) ● The NOT operation simply reverses the associated expression ● If the expression evaluates to true, it becomes false, and if it is false, it becomes true XOR  Binary Operand (Requires two operands) ● An XOR operator is made up of AND & OR gates ● If two expressions are separated by an XOR operator, then exactly only one of the two things has to be true. If neither is true or both are true, the whole expression is false Example(s): Taking the bus (X)OR driving to school Truth/Logic Tables Truth/logic tables: Shows possible values for inputs on a true or false, value for the overall expression’s truth a b a AND b false false false false false false true true false true true true a b a OR b false false false false false true true true true true true true a NOT a false true true false a b a XOR b false false false false true true true false true true true false Relational operators > Greater than >= Greater than or < Less than <= Lesser than or Equal to == Equal != Not Equal To Example(s): Let x = 2, y = 5 and z = 8. Determine the value of the expression 1. (x == 2) AND (y == 7) 2 == 2 AND 5 == 7 TRUE AND FALSE FALSE or 0 2. (x < 4) AND (y >= 5) AND (z == 8) (2 < 4) AND (5 >= 5) AND (8 == 8) TRUE AND TRUE AND TRUE TRUE AND TRUE TRUE or 1 3. (x == 2) AND (y == 6) OR NOT ((x + y) < z) (2 == 2) AND (5 == 6) OR NOT ((2 + 5) < 8) TRUE AND FALSE OR NOT TRUE FALSE OR NOT TRUE FALSE OR FALSE FALSE or Binary 0 Writing Boolean Expressions Interpretation of common phases a is # a=# a is at most # a <= # a is at least # a >= # a is between # and # a >= # AND a <= # Example(s) 1. Write a Boolean Expression that is true when the value of x is between 0 and 50 inclusive. x >= 0 AND x <= 50 2. Write a Boolean expression that is true if the value of x is 2 or atleast 12 x == 2 OR x >= 12 If asked to write a expression that is false, write an expression that is true, then use the NOT operator Ex: NOT(x == 0 AND x == 50) Example(s) 1. Create a truth table for (NOT a AND b) or c a b c (NOT a AND b) OR c 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 0 1 0 1 1 1 1 0 0 1 1 1 1 Applying NOT Operator NOT(x >= 0 AND X == 50) x < 0 OR X != 50 DeMorgan’s Law NOT(a OR b) = NOT a AND NOT b NOT(a AND b) = NOT a OR NOT b Simplifying Boolean Expressions with Relational Operator NOT(a >= b) a<b NOT(a > b) a <= b NOT(a <= b) a>b NOT(a < b) a >= b Example(s) 1. Write a Boolean expression that is false if the value of x is not 5 and the value of y is at most 30 NOT(x != 5 AND y <= 30) x == 5 OR y > 30 Short Circuiting; An expression is stopped being evaluated as soon as the outcome is determined For AND statement, it stops evaluating when there is a false FOR OR statement, it stops evaluating when there is a true Logic Gates Logic gates: A physical device that can carry and read out logical operations by taking one or more Boolean values as input and producing one Boolean value as output ● When talking about computers and electronic devices, Boolean values are 0 and 1  0  No current is running through the wire  1  Current running through the wire Circuit: A collection of logic gates that transforms a set of binary inputs into a set of binary outputs AND OR NOT XOR Example(s) Create a circuit for the given Boolean expression 1. a AND b AND c OR NOT d 2. NOT a OR b AND NOT c Example(s) Determine the value of the given circuit 1. Let A = 1, B = 0, C = 1, D = 1 2. Let A = 1, B = 1, C = 1, D = 1 Data and Information: ● Data and information is one of those very large, general topics that can be multiple courses, an entire degree, or a field of study ● Data and information are not synonyms:  Data by itself does not yet have much meaning  Data needs to be arranged, sorted, analyzed, and then conclusions are formed  After this analysis stage, information is available ● People generate significant amounts of digital data daily  Always on devices are collecting geographical data constantly  Social media sites are collecting premium data based on your usage  Every purchase you make, those you return, websites you visit, all involve data that businesses collect  Google searches ● Raw data: Data not processed for use  Collected data are just values and descriptions until we make some sense of it ● A major role of a computer is data processing  By itself, data is a gigantic pile of unrelated, seemingly useless, communication  With sophisticated data processing and enormous computer power, much useful information can be extracted  Gathering insight from this valuable data involves the combination of statistics, mathematics, programming, and problem solving  Large datasets may be analyzed computationally to reveal patterns, trends, and associations Big Data ● Big data: Vast amounts of data ● Big Data sets have so many records, they are too large to fit in the memory of our computers or even servers. These files need multiple servers to hold and process the data ● Server Farm: Many large computers in one place for the purpose of processing data ● Tools such as spreadsheets, word processors, accounting software, and databases can sort and filter data to gain insight and knowledge Data Processing ● Data Processing is a series of operations on data, especially by a computer, to retrieve, transform, or classify information ● Computers make processing huge amounts of data possible so that people can make sense of it ● Computers process data iteratively and interactively when processing digital information to gain insight and knowledge  Iterative means that computers can go through all data in large data sets to filter, clean, and convert unsorted data into sorted data  Interactive means that people can gain insight and knowledge from translating and transforming digitally represented information  This process is repeated for the life of the program ● Data Cleaning: Removing corrupt data, removing or repairing incorrect data and verifying ranges or dates  Removing pr flagging invalid data is extremely useful ● Data Filtering: Different datasets can be identified and extracted to help people make meaning of the data ● Data Classifying: Grouping data with common features and values  Grouping or classification are based on criteria provided by those working with the data ● Current computing allows the people to collaborate by sharing data  Collaborations when processing information can help one gain insight and knowledge by applying multiple perspectives, experiences and skillsets Metadata ● Metadata is a set of data that describes and gives information about other data. In other words, it is data about data ● Examples:  A digital photograph  The picture itself is the data  The information (filename, location, time, author, etc) is the metadata ● Examples of metadata  Date  Time  Author/Owner  File Size  File Type Data Privacy ● Privacy concerns arise through the mass collection of data ● Anything done online is likely to lead to an invasion of piracy ● This tradeoff for convenience is the risk of violating privacy ● Data is stored and may be sold with or without an individual’s knowledge ● Some businesses claim to aggregate data to protect privacy by summarizing, removing aspects, and/ro masking the data Data Security ● Data Security deals with the ability to prevent unauthorized individuals to gain accessibility to it and preventing who can view your data from changing it ● Strong passwords help block those to gain unauthorized access ● Strong passwords  Must contain a capital letter  Must contain a number  Must contain a special character  Must be a specified minimum length  Cannot be the same or almost the same as another or previous password  Cannot be the same as the user ID ● “Read” access allows a user to view but not modify or delete data ● Encrypting data before it is transmitted ensures data remains secure if it is intercepted during transmissions. The receiving location would decrypt the data Data Compression ● There are two types of data compression  Lossless Compression: Compresses data without any loss of data  Lossy Compression: Compresses data with loss of data ● A loss of quality is accepted when other factors may be more important when dealing with an image, video, audio, etc ● A loss of quality is not accepted if you are dealing with FBI’s fingerprint database ● The larger the file, the greater the opportunity for larger savings of data Data Storage ● Data Storages are used to store and retrieve data ● Writing data is also known as storing data ● Reading data is when you retrieve stored data ● Writing data has a higher storage requirement because it requires the entire files to be saved in memory as the hard drive’s copy is being rewritten so the information is not lost ● With the rise of big data, storage concerns have grown. There are many tradeoffs when deciding where to save your data ● Data Storage Sizes in increasing order:  Kilobyte  Megabyte  Gigabyte  Terabyte  Petabyte  Exabyte  Zettabyte  Yottabyte Scalability ● Ability to increase the capacity of a resource without having to go on a completely new solution and for that resource to continue to operate at acceptable levels Example(s) ● The “cloud”  As traffic or demand for resources increases, the cloud server manages the demand by producing additional resources such as servers ● Networks  As more devices are added to the network, network managers increases access points and other devices to accommodate additional network devices and traffic Predicting Algorithms ● Predicting algorithms can have an effect on daily life as data is analyzed Example(s) ● Social media can target advertising based on viewing habits ● An online store can suggest new products based on prior purchases ● An application may recommend a particular movie based on interest ● Crimes can be prevented based on the identification of hotspots Intro to Algorithms Algorithm: A set of instructions designed to perform a specific task Algorithm Attributes: ● Correctness: The algorithm performs the tasks without error ● Ease of Understanding: The algorithm can be easily read and understood ● Elegance: The algorithm is as short as possible ● Efficiency: The algorithm takes little time and/or space, as necessary ● There is more than one correct algorithm to solve a problem in hand. Different algorithms have different levels of understanding, elegance, and efficiency Benchmarking: Timing an algorithm on standard data sets Examples of Algorithms: ● Recipe ● Map Directions ● Calculate Grades ● Searching ● Sorting Languages for Algorithms: ● Natural Language: Our native speaking and writing language ● Programming Language: Strict syntax for the computer to execute ● Pseudocode: A combination of Natural Language and Programming Language Searching and Sorting Algorithms: Searching: Task of finding a specific value in a list of values or deciding it’s not there Target: The value we are searching for Sorting: Task of putting a list of values into numeric or alphabetical order Sequential / Linear Search ● A sequential/linear search sequentially checks each element of the list until a match is found or the whole list has been searched ● It is not necessary for a list to be sorted for a sequential/linear search to be implemented Example(s) 1. Given a list of N items, find the Best, Average, and Worst case scenario of a Sequential/Linear Search Best Case  1 comparison Average Case  N/2 comparisons Worst Case  N comparisons 2. Using Sequential/Linear Search on 2,500 items, what is the Best, Average, and Worst case scenario of the algorithms Best Case  1 comparison Average Case  2500/2 = 1,250 comparisons Worst Case  2500 comparisons Binary Search ● A binary search works by repeatedly dividing in half the portion of the list that could contain the item. It is considered a divide and conquer algorithm ● A Binary Search requires the list to be in order ● Best Case Scenario of a Binary Search is 1 ● Average & Worst Case Scenario of a Binary Search is log2N for N items Binary Search Steps: Let m = midpoint index, l = low index, h = high index, t = target value, v = value at m 1. Set m = (l + h)/2 2. a) If t = v, end the search, item is found b) If t > v, eliminate all values less than or equal to m. Set l = m + 1 c) If t < v, eliminate all values greater than or equal to m. Set h = m  1 3. If h < l, end the search. The item was not found. Otherwise, go to Step 1 Example(s) 1. Use the Binary Search to find the target 21 in the given list. Identify the value(s), in order, in which the target was compared 10 12 15 21 30 35 37 32 50 51 1 2 3 4 5 6 7 8 9 10 l=1 h = 10 m = (1 + 10)/2 = 11/2 = 5.5 = 5 Element 5 is 30 21 < 30 Eliminate right list 10 12 15 21 30 35 37 32 50 51 l = 1, h = 4 m = (1 + 4)/2 = 5/2 = 2.5 = 2 Element 2 is 12 21 > 12 Eliminate left list 10 12 15 21 l = 3, h = 4 m = (3 + 4)/2 = 3.5/3 = 3 Element 3 is 15 21 > 15 Eliminate element 15 l = 4, h = 4 m = (4 + 4)/2 = 4 Element 4 is 21 21 = 21 Values Compared: 30, 12, 15, 21 Best Case: 1 Worst Case: log210 = 4 Worst case comparison 2. Use the Binary Search Algorithm to find the target 14 in the given list. Identify the value(s) in order, in which the target was compared 2 4 6 8 10 12 14 16 18 20 22 24 1 2 3 4 5 6 7 8 9 10 11 12 Values compared: 12, 18, 14 Best Case: 1 Worst case: log212 = 4 Selection Sort ● Selection Sort is an inplace comparison algorithm ● For N items in a List:  Number of Passes: N  1  Number of Swaps: N  1  Number of Comparisons: N(N  1)/2 Selection Sort Steps: ● Find the largest value of the unsorted part of the list ● Swap the largest value with the last value ● Repeat the above steps until there is one value remaining in the unsorted list Example(s) 1. Use the Selection Sort Algorithm to sort the given list. Show each pass of the Algorithm and identify the Number of Passes and Number of Comparisons 3 7 5 8 4 N=5 Number of Passes = 5  1 = 4 Number of Swaps = 4 Number of Comparisons = 5(5  1)/2 = 10 Largest number: 8 i) Swap 8 and 4 3 7 5 4 8 ii) Sort 7 and 4 3 4 5 7 8 It is already sorted, but a computer does not know that is sorted iii) Sort 5 with 5 (itself) 3 4 5 7 8 iv) Sort 4 with 4 (itself) 3 4 5 7 8 Since there is only one number left in the unsorted side, the sorting is complete 2. Use the Selection Sort Algorithm to sort the given list. Show each pass of the Algorithm and identify the Number of Passes and the Number of Comparisons dog bird cat lizard ant flamingo fish N=7 Number of Passes: 6 Number of Swaps: 6 Number of Comparisons: 7(6)/2 = 21 i) Swap lizard and fish dog bird cat fish ant flamingo lizard ii) Swap flamingo with flamingo (itself) dog bird cat fish ant flamingo lizard iii) Swap fish with ant dog bird cat ant fish flamingo lizard iv) Swap dog with ant ant bird cat dog fish flamingo lizard It is already sorted, but the computer doesn’t know it is sorted v) Swap cat with cat (itself) ant bird cat dog fish flamingo lizard vi) Swap bird with bird (itself) ant bird cat dog fish flamingo lizard Insertion Sort ● Insertion Sort is a single sorting algorithm that builds the final list one list at a time ● Insertion Sort is not efficient on large lists ● For N items in a list:  Number of Passes = N  1 Insertion Sort Steps: 1. Let the first item be a sorted subset 2. Add the next item and insert the item in its appropriate position 3. Repeat Step 2 until all items have been moved to the subset Example(s) 1. Use the Insertion Sort Algorithm to sort the given list. Show each pass of the Algorithm and identify the number of Passes 20 12 35 21 25 43 5 Number of Passes: 6 20 12 35 21 25 43 5 1. Place 12 at the beginning of the sorted list 12 20 35 21 25 43 5 2. Place 35 at the end of the sorted list 12 20 35 21 25 43 5 3. Place 21 between 20 and 35 in the sorted list 12 20 21 35 25 43 5 4. Place 25 between 20 and 35 in the sorted list 12 20 21 25 35 43 5 5. Place 43 at the end of the sorted list 12 20 21 25 35 43 5 6. Place 5 at the beginning of the sorted list 5 12 20 21 25 35 43 2. Use the Insertion Sort Algorithm to sort the given list. Show each pass of the Algorithm and identify the number of Passes octagon rhombus carrot bunny triangle arc Number of Passes: 5 1. Place rhombus at the end of the sorted list octagon rhombus carrot bunny triangle arc 2. Place carrot at the beginning of the sorted list carrot octagon rhombus bunny triangle arc 3. Place bunny at the beginning of the sorted list bunny carrot octagon rhombus triangle arc 4. Place triangle at the end of the sorted list bunny carrot octagon rhombus triangle arc 5. Place arc at the end of the sorted list arc bunny carrot octagon rhombus triangle Additional Sorting Algorithm ● There are many other sorting algorithms. Other noteworthy Sorting Algorithms are: Bubble Sort, Merge Sort, Quick Sort, and Heap Sort Other Algorithm Information ● The algorithms covered in this section are all Polynomial Bounded Algorithms. This means there exists at least one efficient algorithm to solve the problem ● An Intractable Problem has no Polynomial Bounded Algorithm. This means there exists no efficient algorithm to solve the problem. The only option is the inefficient Brute Force. Algorithm: BruteForce algorithms try every problem. The time and storage necessary to perform a BruteForce algorithm makes it useless ● It has not been proven that a Polynomial Bounded Algorithm does not exist for Intractable Problem ● Examples of Intractable Problems:  GPS  Finding the best route  Bin/Box Packing  Maximizing the volume of a bin/box  Chess  Finding the best move ● Data Cleanup Algorithm removes unwanted values ● One of the key features of Algorithms is that once created, you can use them over and over, combine them for more complex problem solving, of modify them for a new user ● The efficiency of Algorithms becomes important especially with extremely large datasets Variables ● Variables are placeholders for values a program needs to use ● Programs can assign values to variables as well as update the value to be a new one ● Variables can be used in expressions ● An important aspect of variables is naming them well. A good name for a variable is one that is descriptive ● Variables are data abstractions because we do not know the details of how and where the values are stored in memory ● A variable can hold a single value or a collection of values if it is a list Assignment Statements ● To assign a value to a variable, the assignment operator is used. ● In many programming languages, a single equal sign, =, is used as the assignment operator ● For this course, the leftfacing arrow ← is used ● The variable name is always on the left side of the ← operator ● The programming language will evaluate the right hand side of the assignment operator and then place the value ito the variable on the left hand side of it ● The format to assign a value to a variable for text and block coding for this course is Text: variable ← expression Block: variable ← expression Example: 1. Assign the value 79 to the variable count num ← 79 2. Assign the value 18 to the variable count. Then, change the value of count to 25 count ← 18 count ← 25 Data Types ● The computer stores everything as 0s and 1s. Data types are the way computers assign some meaning to those binary digits ● There are four main data types: String, Integer, Fractional, and Boolean Strings ● Strings are a series of characters and are denoted with quotation marks Example: phrase ← “Hello World!” ● Any character, number, or symbol can be part of a string ● When numbers are part of a string, they will be considered text. In this case, you cannot use them in calculations because they are text fields, not numbers ● Concatenating strings means adding them together Integer x ← 5 String x ← “5” Integer y ← 3 String y ← “3” z← x+y→8 String z ← x + y → “53” Example: 1, Assign the value “Hello “ to the variable phrase1 and and the value “World!” to the variable phrase2. Concatenate phrase1 and phrase2 and store the result in phrase3 phrase1 ← “Hello “ phrase2 ← “World!” phrase3 ← “Hello World!” ● Each character in a String has an index representing its position in the String Character H e l l o W o r l d ! Index 1 2 3 4 5 6 7 8 9 10 11 12 ● Notice the space between “Hello” and “World!” gets its own index. A space is a valid character Integers ● Integers are whole numbers. Integers can be used in mathematical operations and expressions, whereas Strings cannot. ● Integers are assigned without quotation marks Example: num ← 20 Fractional Numbers ● Fractional Numbers contain a decimal point. These numbers are also used in mathematical operations and expressions ● Fractional numbers are assigned without quotation marks Example: num1 ← 2.5 num2 ← 32.0 Booleans ● Booleans store the value true or false ● Booleans are assigned without quotation marks Example: result ← false ● Relational & Boolean operators are used with Booleans: =. ≠, >, <, ≥, ≤, AND, OR, NOT Mathematical Operators ● Most programming languages include mathematical operators. For this course, you need to understand the operators provided in the table below Operator Meaning Example + Addition 3 + 8 = 11  Subtraction 52=3 * Multiplication 4 * 5 = 20 / Division 5 / 2 = 2.5 MOD Modulus 7 MOD 2 = 1 ● Modulus uses the symbol MOD and returns only the remainder after dividing. Most programming languages use the symbol % to represent Modulus ● The order of operations, PEMDAS, used in math is followed by programming languages. MOD has the same precedence as multiplication and division 1. Parentheses 2. MOD, *, / 3. +,  ● When operators have the same precedence, the expression should be evaluated left to right Example(s) What is the value of a after the expression is evaluated? a ← 10 MOD 3 10/3 = 3 REM 1 a←1 a ← 27 MOD 4 27/4 = 6 REM 3 a←3 a ← 12 MOD 2 12/2 = 6 REM 0 a←0 a ← 4 MOD 6 4/6 = 4 REM 6 a←4 Display Operators ● The DISPLAY operator displays the value of an expression, followed by a space Text: DISPLAY (expression) Block: DISPLAY expression Example(s) 1. What will the following program display a←5+4*2 b ← 4 MOD 5 DISPLAY (a + b) a = 5 + 8 = 13 b=4 17 a ← 4 * (3 + 6) b←4*3+6 DISPLAY (a ≤ b) a = 4 * 9 = 36 b = 12 + 6 = 18 false Input Operators ● The INPUT operator accepts a value from the user and returns the input value Text: INPUT () Block: INPUT Example(s) 1. What will the following program display if the INPUT function reads an even number such as 4 a ← INPUT () a ← a MOD 2 DISPLAY (a) 0 2. What will the following program display if the INPUT function reads an odd number such as 5 a ← INPUT () a ← a MOD 2 DISPLAY (a) 1 Numeric Procedures ● RANDOM generates and returns a random integer from ato b, including a and b. Each result is equally likely to occur Text: RANDOM (a, b) Block: RANDOM a, b Example(s) 1. What is the percentage of times a would display false a ← RANDOM (1, 10) DISPLAY (a ≤ 3) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 70% 2. What is the percentage of times a would display true a ← RANDOM (1, 10) DISPLAY (a ≤ 3) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 30% 3. What is the percentage of times a would display false a ← RANDOM (1, 2) DISPLAY (a = 1) 1, 2 50%
Enter the password to open this PDF file:











