Contents
Chapter 1: Getting started with algorithms .................................................................................................... 2
Section 1.1: A sample algorithmic problem ................................................................................................................. 2 Section 1.2: Getting Started with Simple Fizz Buzz Algorithm in Swift ...................................................................... 2
Chapter 2: Algorithm Complexity ......................................................................................................................... 5
Section 2.1: Big-Theta notation .................................................................................................................................... 5
Section 2.2: Comparison of the asymptotic notations .............................................................................................. 6
Section 2.3: Big-Omega Notation ................................................................................................................................ 6
Credits ............................................................................................................................................................................ 250 You may also like ...................................................................................................................................................... 252
Chapter 1: Getting started with algorithms
Section 1.1: A sample algorithmic problem
An algorithmic problem is specied by describing the complete set of instances it must work on and of its output after running on one of these instances. This distinction, between a problem and an instance of a problem, is fundamental. The algorithmic problem known as sorting is dened as follows: [Skiena:2008:ADM:1410219]
Problem: Sorting
Input: A sequence of n keys, a_1, a_2, ..., a_n.
Output: The reordering of the input sequence such that a '_1 <= a' _2 <= ... <= a '_{n-1} <= a' _n
An instance of sorting might be an array of strings, such as { Haskell, Emacs } or a sequence of numbers such as { , , 1337 } .
Section 1.2: Getting Started with Simple Fizz Buzz Algorithm in Swift
For those of you that are new to programming in Swift and those of you coming from dierent programming bases, such as Python or Java, this article should be quite helpful. In this post, we will discuss a simple solution for implementing swift algorithms.
Fizz Buzz
You may have seen Fizz Buzz written as Fizz Buzz, FizzBuzz, or Fizz-Buzz; they're all referring to the same thing. That "thing" is the main topic of discussion today. First, what is FizzBuzz?
This is a common question that comes up in job interviews.
Imagine a series of a number from 1 to 10.
1 2 3 4 5 6 7 8 9 10
Fizz and Buzz refer to any number that's a multiple of 3 and 5 respectively. In other words, if a number is divisible by 3, it is substituted with zz; if a number is divisible by 5, it is substituted with buzz. If a number is simultaneously a multiple of 3 AND 5, the number is replaced with "zz buzz." In essence, it emulates the famous children game "zz buzz".
To work on this problem, open up Xcode to create a new playground and initialize an array like below:
// for example
let number = [ ,,,, ] // here 3 is fizz and 5 is buzz
To nd all the zz and buzz, we must iterate through the array and check which numbers are zz and which are buzz. To do this, create a for loop to iterate through the array we have initialised:
for num in number {
// Body and calculation goes here
}
After this, we can simply use the "if else" condition and module operator in swift ie - % to locate the zz and buzz
for num in number {
if num % == {
print ( " \( num) fizz" )
} else {
print ( num )
}
}
Great! You can go to the debug console in Xcode playground to see the output. You will nd that the "zzes" have been sorted out in your array.
For the Buzz part, we will use the same technique. Let's give it a try before scrolling through the article you can check your results against this article once you've nished doing this.
for num in number {
if num % == {
print ( " \( num) fizz" )
} else if num % == { print ( " \( num) buzz" )
} else {
print ( num )
}
}
Check the output!
It's rather straight forward you divided the number by 3, zz and divided the number by 5, buzz. Now, increase the numbers in the array
let number = [ ,,,,,,,,,,,,,, ]
We increased the range of numbers from 1-10 to 1-15 in order to demonstrate the concept of a "zz buzz." Since 15 is a multiple of both 3 and 5, the number should be replaced with "zz buzz." Try for yourself and check the answer!
Here is the solution:
for num in number {
if num % == && num % == { print ( " \( num) fizz buzz" )
} else if num % == {
print ( " \( num) fizz" )
} else if num % == {
print ( " \( num) buzz" )
} else {
print ( num )
}
}
Wait...it's not over though! The whole purpose of the algorithm is to customize the runtime correctly. Imagine if the range increases from 1-15 to 1-100. The compiler will check each number to determine whether it is divisible by 3 or 5. It would then run through the numbers again to check if the numbers are divisible by 3 and 5. The code would essentially have to run through each number in the array twice it would have to runs the numbers by 3 rst and then run it by 5. To speed up the process, we can simply tell our code to divide the numbers by 15 directly.
Here is the nal code:
for num in number {
if num % == {
print ( " \( num) fizz buzz" )
} else if num % == { print ( " \( num) fizz" )
} else if num % == { print ( " \( num) buzz" )
} else {
print ( num )
}
}
As Simple as that, you can use any language of your choice and get started Enjoy Coding
Chapter 2: Algorithm Complexity
Section 2.1: Big-Theta notation
Unlike Big-O notation, which represents only upper bound of the running time for some algorithm, Big-Theta is a tight bound; both upper and lower bound. Tight bound is more precise, but also more dicult to compute.
The Big-Theta notation is symmetric: f ( x ) = ( g ( x )) <=> g ( x ) = ( f ( x ))
An intuitive way to grasp it is that f ( x ) = ( g ( x )) means that the graphs of f(x) and g(x) grow in the same rate, or that the graphs 'behave' similarly for big enough values of x.
The full mathematical expression of the Big-Theta notation is as follows:
(f(x)) = {g: N0 -> R and c1, c2, n0 > 0, where c1 < abs(g(n) / f(n)), for every n > n0 and abs is the absolute value }
An example
If the algorithm for the input n takes 42n ^ + 25n + operations to nish, we say that is O ( n ^ ) , but is also O ( n ^ ) and O ( n ^ ) . However, it is ( n ^ ) and it is not ( n ^ ) , ( n ^ ) etc. Algorithm that is ( f ( n )) is also O ( f ( n )) , but not vice versa!
Formal mathematical denition
( g ( x )) is a set of functions.
( g ( x )) = { f ( x ) such that there exist positive constants c1, c2, N such that <= c1 * g ( x ) <= f ( x ) <= c2 * g ( x ) for all x > N }
Because ( g ( x )) is a set, we could write f ( x ) ( g ( x )) to indicate that f ( x ) is a member of ( g ( x )) . Instead, we will usually write f ( x ) = ( g ( x )) to express the same notion - that's the common way.
Whenever ( g ( x )) appears in a formula, we interpret it as standing for some anonymous function that we do not care to name. For example the equation T ( n ) = T ( n / ) + ( n ) , means T ( n ) = T ( n / ) + f ( n ) where f ( n ) is a function in the set ( n ) .