Sorting Algorithms: Insertion and Selection Sort

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is VSL an abbreviation for?

  • Value of Standard Living
  • Value of Serene Life
  • Value of Sustained Living
  • Value of Statistical Life (correct)

The human capital approach is based on what?

  • The cost of healthcare
  • The value of leisure time
  • Lost earnings over a lifetime (correct)
  • The price of education

What does WTA stand for in cost-benefit analysis?

  • Way To Analyze
  • Willingness To Accept (correct)
  • Way To Account
  • Willingness To Acquire

What is hedonic pricing used for?

<p>Inferring the 'price' of a good by analyzing the prices of related goods. (C)</p> Signup and view all the answers

What three types of value sum up total value?

<p>Use value, Option value, Existence value (C)</p> Signup and view all the answers

What is a stated preference method?

<p>Techniques that measure values for environmental or other goods by asking questions regarding people’s WTP or WTA. (C)</p> Signup and view all the answers

What is the name of the standard that Pareto improvements are difficult to find in the wild in real life?

<p>Pareto improvements (B)</p> Signup and view all the answers

What is the effect of Pigouvian taxes?

<p>Taxes targeted at negative externalities (A)</p> Signup and view all the answers

What occurs if positive externalities exist?

<p>Market undersupplies (A)</p> Signup and view all the answers

What does the Coase Theorem state regarding pollution?

<p>Private negotiation can lead to efficient outcomes if property rights are assigned. (B)</p> Signup and view all the answers

What is a key advantage of Pareto efficiency?

<p>Clear and easy to understand (B)</p> Signup and view all the answers

What is an example of a negative externality?

<p>Pollution (C)</p> Signup and view all the answers

According to the notes, what is the effect of EPA utilities pollution monitoring?

<p>They will produce less on days they are monitored. (A)</p> Signup and view all the answers

Who posited that markets are a good way to organize economic activity?

<p>Adam Smith (D)</p> Signup and view all the answers

What does demand describe?

<p>The consumer's ability and willingness to pay a price for a specific good or service (B)</p> Signup and view all the answers

Flashcards

Pollution regulation

Trade-off between health benefits and additional burden on businesses (jobs go down, higher prices).

Preservation regulation

Trade-off between natural beauty/biodiversity and affordable housing.

Why are tradeoffs important?

Understanding your exact trade-off is useful for decision making.

Opportunity cost

The cost of something what you give up to get it.

Signup and view all the flashcards

Marginal (utility) benefit

Additional happiness from consuming 1 additional unit.

Signup and view all the flashcards

Marginal cost

Additional money + opportunity cost of consuming/creating 1 additional unit.

Signup and view all the flashcards

Marginal revenue

Additional revenue from creating/selling 1 additional unit.

Signup and view all the flashcards

Response to pollution monitoring?

They will produce less on days they are monitored.

Signup and view all the flashcards

Wealth of Nations

Markets are usually a good way to organize economic activity.

Signup and view all the flashcards

Externalities

Exists when there is a cost or benefit of a transaction not borne by buyer or seller.

Signup and view all the flashcards

Negative externality

Impose harm to someone who is not a buyer or seller. (air/water/ noise Pollution ).

Signup and view all the flashcards

Pigouvian taxes

Tax equal to externalities internalizes the externality - makes SMC = PMC.

Signup and view all the flashcards

Coase Theorem

If polluters and victims can easily bargain then private negotiation arrive at efficient outcome, whoever has initial right to pollute/prevent pollution and let the interested parties negotiate to reach the optimal outcome at first.

Signup and view all the flashcards

Pareto improvement :

Pareto improvement exists if one person can be made better off without making anyone else worse off.

Signup and view all the flashcards

Efficiency Standard

An allocation of resources is efficient if the net benefits (benefits minus costs) from the use of those resources is highest under that allocation.

Signup and view all the flashcards

Study Notes

Algorithms for Sorting

Insertion Sort

  • Takes elements one by one and inserts them into their correct position within the already sorted portion of the list.
def tri_insertion(liste):
  """Sorts a list using the insertion sort algorithm."""
  n = len(liste)
  for i in range(1, n):
    element_a_inserer = liste[i]
    j = i
    # Move elements of liste[0..i-1], that are greater than
    # element_a_inserer, to one position ahead
    while j > 0 and liste[j-1] > element_a_inserer:
      liste[j] = liste[j-1]
      j -= 1
    liste[j] = element_a_inserer
  return liste
  • Complexity: $O(n^2)$
  • Considered efficient for small or nearly sorted lists.

Selection Sort

  • Finds the minimum element in the list and places it in the first position.
  • Repeats this process for the rest of the list, placing the next smallest element in the second position, and so on.
def tri_selection(liste):
  """Sorts a list using the selection sort algorithm."""
  n = len(liste)
  for i in range(n):
    # Find the index of the minimum in the remaining unsorted list
    min_idx = i
    for j in range(i+1, n):
      if liste[j] < liste[min_idx]:
        min_idx = j
    # Swap the found minimum element with the first unsorted element
    liste[i], liste[min_idx] = liste[min_idx], liste[i]
  return liste
  • Complexity: $O(n^2)$
  • Maintains the same time complexity regardless of the initial order of the list.

Bubble Sort

  • Compares adjacent elements and swaps them if they are in the wrong order.
  • This process is repeated until no more swaps are needed, indicating that the list is sorted.
def tri_bulles(liste):
  """Sorts a list using the bubble sort algorithm."""
  n = len(liste)
  for i in range(n):
    # Last i elements are already in place
    for j in range(0, n-i-1):
      # Traverse the list from 0 to n-i-1
      # Swap if the element found is greater than the next element
      if liste[j] > liste[j+1] :
        liste[j], liste[j+1] = liste[j+1], liste[j]
  return liste
  • Complexity: $O(n^2)$
  • Generally less efficient in practice, except for small lists.

Quicksort

  • Chooses a pivot element and partitions the list into two sub-lists: elements less than the pivot and elements greater than the pivot.
  • Recursively sorts the two sub-lists.
def partition(liste, low, high):
  """Partitions the list around a pivot."""
  i = ( low-1 )         # index of smaller element
  pivot = liste[high]     # pivot
  for j in range(low , high):
    # If current element is smaller than or

Bernoulli's Principle

  • States that an increase in the speed of a fluid occurs simultaneously with a decrease in pressure or a decrease in the fluid's potential energy.

How Planes Fly

  • Airplane wings are designed to make air travel faster over the top of the wing than underneath.
Faster air = lower pressure
  • Faster moving air has lower pressure, and the wing is "sucked" upward.

Lecture 17: October 24

Plan

  • Finish discussing $P$ vs $NP$
  • $NP$-completeness: definitions and examples

Reductions (recap)

Definition
  • Language $L_1$ reduces to language $L_2$ (denoted $L_1 \le_p L_2$) if:
    • There is a polynomial time function $f$ such that $x \in L_1 \iff f(x) \in L_2$.
Example
  • $IndependentSet \le_p Clique$
    • Transformation: $G' = \overline{G}$, $k' = k$
    • An independent set of size $k$ in $G$ $\iff$ A clique of size $k$ in $\overline{G}$

$NP$-completeness

Definition
  • $L$ is $NP$-complete if:
    • $L \in NP$
    • Every $L' \in NP$ reduces to $L$ (i.e., $L' \le_p L$ for every $L' \in NP$)
Significance
  • If we can show that $L$ is solvable in polynomial time, then $P = NP$.
Cook-Levin Theorem
  • $SAT$ is $NP$-complete

How to prove that $L$ is $NP$-complete

  1. Show $L \in NP$
  2. Choose an $NP$-complete problem $L'$
  3. Prove $L' \le _p L$
Prove that 3-SAT is $NP$-Complete
  1. $3-SAT \in NP$
  2. $SAT \le_p 3-SAT$
    • For each clause, convert it into a conjunction of 3-CNF clauses
    • $(x_1 \lor x_2)$ becomes $(x_1 \lor x_2 \lor y) \land (x_1 \lor x_2 \lor \overline{y})$
    • $(x_1 \lor x_2 \lor x_3 \lor x_4)$ becomes $(x_1 \lor x_2 \lor y) \land (\overline{y} \lor x_3 \lor x_4)$

More Examples of $NP$-complete Problems

Clique
  1. Clique $\in NP$
  2. $3-SAT \le_p Clique$
    • Given: $\phi = (x_1 \lor x_2 \lor \overline{x_3}) \land (\overline{x_1} \lor x_3 \lor x_4) \land... \land (x_2 \lor \overline{x_3} \lor \overline{x_4})$
    • Create a graph $G = (V, E)$
      • $V$: all literals in $\phi$
      • E
        • add edge between $l_i$ in $C_i$ and $l_j$ in $C_j$ if
          • $i \ne j$
          • $l_i$ is not the negation of $l_j$
    • Let $k$ = # of clauses
Example
  • $\phi = (x_1 \lor x_2 \lor \overline{x_3}) \land (\overline{x_1} \lor x_3 \lor x_4)$
  • $k = 2$
Graph
  • If $\phi$ is satisfiable, then there is a clique of size $k$ in $G$.
  • If there is a clique of size $k$ in $G$, then $\phi$ is satisfiable.
Independent Set
  1. Independent Set $\in NP$
  2. Clique $\le_p$ Independent Set
    • $G' = \overline{G}$
    • Same $k$

Chapter 3: Data Representation

3.1 Introduction
  • Computers store, processes, and transmit information electronically.
  • Data must be represented in a way understandable to computers.
  • Data representation schemes define how data is stored and processed.
  • Data representation impacts program efficiency and accuracy.
  • This chapter explores data representation methods used in computing.
3.2 Number Systems
  • A number system defines how numbers are represented.
Key Characteristics
- Base or radix: The number of unique digits.
- Digits: Symbols used to represent numbers.
- Place value: The value of a digit depends on its position.
Common Number Systems
- Decimal (base 10)
- Binary (base 2)
- Octal (base 8)
- Hexadecimal (base 16)
3.2.1 Decimal Number System
  • Base 10 number system
Digits
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  • Each position is a power of 10.
Example
- $325 = (3 \times 10^2) + (2 \times 10^1) + (5 \times 10^0)$
- $325 = 300 + 20 + 5$
3.2.2 Binary Number Systems
  • Base 2 number system
Digits
- 0, 1
  • Each position is a power of 2.
  • Used extensively in computers due to electronic circuits representing 0 or 1 (off or on).
Example

$1011 = (1 \times 2^3) + (0 \times 2^2) + (1 \times 2^1) + (1 \times 2^0)$ $1011 = 8 + 0 + 2 + 1 = 11$ (in decimal)

Binary to Decimal Conversion
  • Multiply each binary digit by its corresponding power of 2.
  • Add the results.
  • Ex: $11010 = (1 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)$
  • $11010 = 16 + 8 + 0 + 2 + 0 = 26$ (in decimal)
Decimal to Binary Conversion
  • Repeatedly divide the decimal number by 2.
  • Record the remainders.
  • The binary number is formed by the remainders written in reverse order Ex: Convert 25 to binary
Division Quotient Remainder
25 / 2 12 1
12 / 2 6 0
6 / 2 3 0
3 / 2 1 1
1 / 2 0 1
  • $25_{10} = 11001_2$
3.2.3 Octal Number System
  • Base 8 number system
Digits
- 0, 1, 2, 3, 4, 5, 6, 7
  • Each position represents a power of 8.
  • Used as shorthand for binary in some applications
Example
- $472_8 = (4 \times 8^2) + (7 \times 8^1) + (2 \times 8^0) $
- $472_8 = 256 + 56 + 2 = 314_{10}$
Octal to Decimal Conversion
  • Multiply each octal digit by its corresponding power of 8.
  • Add the results.
  • Ex: $237_8 = (2 \times 8^2) + (3 \times 8^1) + (7 \times 8^0)$
  • $237_8 = 128 + 24 + 7 = 159_{10}$
Decimal to Octal Conversion
  • Repeatedly divide the decimal number by 8.
  • Record the remainders.
  • The octal number is formed by the remainders written in reverse order.
Ex: Convert 159 to octal.
Division Quotient Remainder
159 / 8 19 7
19 / 8 2 3
2 / 8 0 2
  • $159_{10} = 237_8$
Octal to Binary Conversion
  • Convert each octal digit to its 3-bit binary equivalent
    • 0 = 000, 1 = 001, 2 = 010, 3=011
    • 4=100, 5= 101, 6=110, 7=111
  • Concatenate the binary equivalents
Example
- $472_8 = 100 \ 111 \ 010 = 100111010_2$
Binary to Octal Conversion
  • Group the binary digits into sets of 3, starting from the right
  • If necessary, add leading zeros to complete the leftmost group
  • Convert each group of 3 binary digits to its octal equivalent
Example

$101101110_2 = 101 \ 101 \ 110 = 556_8$

3.2.4 Hexadecimal Number System
  • Base 16 number system
Digits
  • 0-9, A, B, C, D, E, F, A = 10, B=15, C=12, D=13, E=14, F=15
  • Each position represents a power of 16.
  • Commonly used to represent memory addresses and colors
Example
  • $2AF_{16} = (2 \times 16^2) + (10 \times 16^1) + (15 \times 16^0) $
  • $2AF_{16} = 512 + 160 + 15 = 687_{10}$
Conversions
Hexadecimal to Decimal Conversion
  • Multiply each hexadecimal digit by its corresponding power of 16.
  • Add the results.
Example
  • $3B2_{16} = (3 \times 16^2) + (11 \times 16^1) + (2 \times 16^0)$
  • $3B2_{16} = 768 + 176 + 2 = 946_{10}$
Decimal to Hexadecimal Conversion
  • Repeatedly divide the decimal number by 16.
  • Record the remainders (in hexadecimal).
  • The hexadecimal number is formed by the remainders written in reverse order.
Example
  • Convert 946 to hexadecimal.
Division Quotient Remainder
946 / 16 59 2
59 / 16 3 11 (B)
3 / 16 0 3
  • $946_{10} = 3B2_{16}$
Hexadecimal to Binary Conversion

Convert each hexadecimal digit to its 4-bit binary equivalent

  • 0-7: 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001
  • A-F:1010, 1011, 1100, 1101, 1110, 1111
  • Concatenate the binary equivalents.
Example

$2AF_{16} = 0010 \ 1010 \ 1111 = 001010101111_2$

Binary to Hexadecimal Conversion
  • Group the binary digits into sets of 4, starting from the right
  • If necessary, add leading zeros to complete the leftmost group.
  • Convert each group of 4 binary digits to its hexadecimal equivalent
Example

$1011011110_2 = 0010 \ 1101 \ 1110 = 2DE_{16}$

3.2.5 Number System Conversions Table
Number System Base Digits
Binary 2 0, 1
Octal 8 0, 1, 2, 3, 4, 5, 6, 7
Decimal 10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Hexadecimal 16 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
3.3 Data Types
  • A data type defines the type of value a variable can hold
  • Different data types require different amounts of storage space
  • Selecting the appropriate data type is important for efficient and accurate
Common Data types
- Integer
- Floating-point
- Character
- Boolean
3.3.1 Integer Data Type
  • Represents whole numbers (positive, negative, and zero)
  • Common sizes: 8-bit, 16-bit, 32-bit, 64-bit
Examples
  • -10, 0, 255, 1024
Different Integer Types
  • int - Signed integer
  • unsigned int: Unsigned integer (non negative)
  • short int: Short integer
  • long int: Long integer
3.3.2 Floating-Point Data Type
  • Represents real numbers with fractional parts
  • Stored using scientific notation
  • Typical sizes: 32-bit, 64-bit
  • Examples: -3.14, 0.0, 2.718, 1.0
Common Floating Point types
  • float - Single-precision floating point
  • double - Double-precision floating point
3.3.3 Character Data Type
  • Represents individual characters (letters, digits, symbols)
  • Typically 8 bit or 16 bit
  • Examples: A,7,$, Represented using character encoding schemes like ASCII and Unicode.
  • Type = char
3.3.4 Boolean Data Type
  • Represents truth values (true or false)
  • Examples: true, false
  • Type: bool
  • Used in logical operations and control flow statements
3.3.5 Other Data Types
  • String - Sequence of characters
  • Array: Collection of elements of the same data type
  • Structure - Collection
  • Pointer = variable that stores the memory address of another variable.
3.4 Character Encoding Schemes
  • Character encoding schemes define how characters are represented as numeric codes.
Common Schemes
  • ASCII
  • Unicode
  • UTF-8
3.4.1 ASCII
  • American Standard Code for Information Interchange.
  • 7-bit encoding scheme.
Represents 128 characters
- Uppercase letters
- Lowercase letters
- Digits
- Punctuation marks
- Control characters.
3.4.2 Unicode

Universal character encoding standard

  • Provides a unique numeric code for every character.
  • Supports much larger character set with ASCII
Different Unicode encoding Forms
  • UTF-8
  • UTF-16
  • UTF-32
3.4.3 UTF-8
  • Unicode Transformation Format -8 bit Variable encoding scheme.
  • Represents characters using 1 to four bytes
  • Compatible with ASCII( ASCII characters are represented by one byte)Most widely used encoding scheme dor web pages and other text based formats

3.5 Representing Sound and Images

Computers can also represent sound and images using numbers.

3.5.1 Representing Sound
  • Sound is represented as a sequence of samples.
  • Sampling rate: 44.1kHz for CD quality audio. Number of samples taken per second
  • Sample size: number of bits used to represent each sample
Audio Formats
  • WAV
  • MP3
  • ACC
3.5.2 Representing Images
  • Images are presented as a grid of elements.
  • Each element has a color value
  • Color depth: Number of bits represents each pixel's color.
Image formats
  • BMP
  • JPEG
  • PNG
  • GIF

3.6 Summary

  • Data presentation if crucial for computers to store process and information
  • Number systems like binary, octal, decimal and hexa decimal are used to present numeric data
  • Sound and images can be represented by using sample and pixel, respectively.

Fluid Mechanics

Fluid properties
Density
  • Mass per unit volume.
  • Rho = m/v or Density = mass/volume
    Units
    • kg/m^3
    • slugs/ft^3
Specific Volume
Definition
  • Volume of unit mass
  • The equation is v= 1/Rho
Units


Units

Specific weight
Definition
  • Weight per unit volume
  • The equation is gamma= W/V( volume ) =p(rho)g
  • w = weight and g(acceleration due to gravity(9.81))
Units
  • n/m3
  • Ib/ft3
Relative Density
Equation

-SG ratio = p/ pH2O or =p/ pair

NOTE
Viscosity
Dynamic (absolute) viscosity)
Definition
  • Measure of a fluid's resistance to flow under an applied force Torr=u/ (du dy) and where Torr ( shear steress and use( dynamic vescosity du:dy(veslocity gradient
Units

Pa.s or n.5/m2 Ib.s/ft2

Kinematic
Equation

v= nu/p(rho)

Units

m2/s ft2s

Surface tension

Surface tension
Definition

F force acting. perpendicular to a line of unit length on the surface The equation is sigma= f/l were f= force and l= length

Units
Units

N/m CApillay rice

  • h= 2 (sigma) coe/ yv
Vapor Pressure
  • The pressure at which a liquid boils and is in equilibrium with its own vapor.
  • Boiling occurs when the fluid pressure equals the vapor.
Compressibility
Bulk Modulus or Elasticity

dp/dv/u

Units

Pa Ib/in2

Compressibility
  • C= 1/eu

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Like This

Use Quizgecko on...
Browser
Browser