Download Assignment 2 BTEC Assignment 2 BTEC and more Assignments Data Structures and Algorithms in PDF only on Docsity!
ASSIGNMENT 2 FRONT SHEET
Qualification BTEC Level 5 HND Diploma in Computing
Unit number and title Unit 14: Business Intelligence
Submission date May 8, 2021 Date Received 1st submission
Re-submission Date Date Received 2nd submission
Student Name Lê Hoàng Hiệp Student ID GCS
Class GCS0805A_PPT Assessor name Vũ Thanh Hiền
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.
Student’s signature Hiệp
Grading grid
P4 P5 P6 P7 M4 M5 D3 D
Summative Feedback: Resubmission Feedback:
Grade: Assessor Signature: Date:
Internal Verifier’s Comments:
IV Signature:
Learning Outcomes and Assessment Criteria Pass Merit Distinction LO1 Implement complex data structures and algorithms D3 Critically evaluate the complexity of an implemented ADT/algorithm P4 Implement a complex ADT and algorithm in an executable programming language to solve a well defined problem. P5 Implement error handling and report test results. M4 Demonstrate how the implementation of an ADT/algorithm solves a well- defined problem LO4 Assess the effectiveness of data structures and algorithms D4 Evaluate three benefits of using implementation independent data structures P6 Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm P7 Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer with an example. M5 Interpret what a trade-off is when specifying an ADT using an example to support your answer
Table of Contents
- defined problem................................................................................................................................ P4 Implement a complex ADT and algorithm in an executable programming language to solve a well
- P5 Implement error handling and report test results.........................................................................
- P6 Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm.............
- Asymptotic Analysis – Analysis of Algorithms................................................................................
- 1.1. Big Oh Notation, Ο........................................................................................................................
- 1.2. Omega Notation, Ω.......................................................................................................................
- 1.3. Theta Notation, θ.........................................................................................................................
- 2 The execution Time of Algorithms...............................................................................................
- 3 General Rules for Estimation........................................................................................................
- with an example.............................................................................................................................. P7 Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer
- 1 Measurement of algorithm efficiency..........................................................................................
- Common Growth Rates................................................................................................................
- 3 A Comparison of Growth-Rate Functions.....................................................................................
- REFERENCES....................................................................................................................................
Convert 10 to binary (Implementation)
package ArrayStack; import java.util.Scanner; public class Test2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Nhập vào một số3nguyên: "); int n = sc.nextInt(); ArrayStack stack = new ArrayStack(); while (n > 0 ) { stack.push(n % 2 ); n /= 2 ; } System.out.println("Số3nhị phân tuong ứng: "); while (!stack.isEmpty()) System.out.print(stack.pop()); sc.close(); } }
I use Stack to change the coefficients.
Enter any int.
Put the remainder of k chi for 2 (n% 2) on the stack.
Assign n /= 2
Reverse the stack I get the value to count.
P5 Implement error handling and report test results.
Algorithm – Convert 10 to binary (Use of Stack)
Algorithm – Convert 10 to binary (Implementation)
At the beginning, I give the value divided by 0 (n = n / 0) and Java throws an exception after which the
program is terminated and an error message is displayed informing the user which exception was raised
Result:
After using the try-catch command to skip it and program execution can continue.
For a simple example, the running time of a given data mining query is considered f(n), and its corollary
search operation is calculated as g(n2). So the first operation’s running time increases linearly with the rise
in n, while the running time of the second operation increases exponentially as n is enlarged.
While run-time performance can be calculated with many different functions, the limiting behavior of
the algorithm is expressed graphically using simple notation:
Ο(n): Is the upper bound of an algorithm's running time and measures the worst case scenario of
how long an algorithm can possibly take to complete a given operation.
Ω(n): Is the lower bound of an algorithm's running time and measures the best case scenario of how
long an algorithm can possibly take to complete a given operation.
Θ(n): Is charting both the upper and lower running time boundaries, with the average case scenario
express as the average between each border.
1.1. Big Oh Notation, Ο
The notation Ο(n) is the formal way to express the upper bound of an algorithm's running time. It measures
the worst case time complexity or the longest amount of time an algorithm can possibly take to complete.
For example, for a function f(n)
Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. }
1.2. Omega Notation, Ω
The notation Ω(n) is the formal way to express the lower bound of an algorithm's running time. It measures
the best case time complexity or the best amount of time an algorithm can possibly take to complete.
For example, for a function f(n)
Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }
1.3. Theta Notation, θ
The notation θ(n) is the formal way to express both the lower bound and the upper bound of an
algorithm's running time. It is represented as follows –
θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. }
2. The execution Time of Algorithms
Each operation in an algorithm (or a program) has a cost.
Each operation takes a certain of time.
count = count + 1;
take a certain amount of time, but it is constant
A sequence of operations:
count = count + 1; Cost: c
sum = sum + count; Cost: c
Total Cost = c1 + c
boolean sorted = false; int temp; while(!sorted) { sorted = true; for (int i = 0 ; i < array.length - 1 ; i++) { if (a[i] > a[i+ 1 ]) { temp = a[i]; a[i] = a[i+ 1 ]; a[i+ 1 ] = temp; sorted = false; } } } }
Example: Insertion Sort
public static void insertionSort(int[] array) { for (int i = 1 ; i < array.length; i++) { int current = array[i]; int j = i - 1 ; while(j >= 0 && current < array[j]) { array[j+ 1 ] = array[j]; j--; } // at this point we've exited, so j is either - // or it's at the first element where current >= a[j] array[j+ 1 ] = current; } }
Example: Selection Sort
public static void selectionSort(int[] array) { for (int i = 0 ; i < array.length; i++) { int min = array[i]; int minId = i; for (int j = i+ 1 ; j < array.length; j++) { if (array[j] < min) { min = array[j]; minId = j; } } // swapping int temp = array[i]; array[i] = min; array[minId] = temp; } }
3. General Rules for Estimation
Loops : The running time of a loop is at most the running time of the statements inside of that loop
times the number of iterations
Nested Loops : Running time of nested loop containing a statement in the inner most loop is the
running time of statement multiplied by the product of the sized of all loops
Consecutive Statement : Just add the running times of those consecutive statements
If/Else : Never more than the running time of the test plus the larger of running times of S1 and S
P7 Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer with an example.
1. Measurement of algorithm efficiency
Some algorithms are more efficient than others. We would prefer to chose an efficient algorithm, so it
would be nice to have metrics for comparing algorithm efficiency.
The complexity of an algorithm is a function describing the efficiency of the algorithm in terms of the
amount of data the algorithm must process. Usually there are natural units for the domain and range of
this function. There are two main complexity measures of the efficiency of an algorithm:
Time complexity is a function describing the amount of time an algorithm takes in terms of the
amount of input to the algorithm. "Time" can mean the number of memory accesses performed,
the number of comparisons between integers, the number of times some inner loop is executed, or
some other natural unit related to the amount of real time the algorithm will take. We try to keep
this idea of time separate from "wall clock" time, since many factors unrelated to the algorithm
itself can affect the real time (like the language used, type of computing hardware, proficiency of
the programmer, optimization in the compiler, etc.). It turns out that, if we chose the units wisely,
all of the other stuff doesn't matter and we can get an independent measure of the efficiency of the
algorithm.
Space complexity is a function describing the amount of memory (space) an algorithm takes in
terms of the amount of input to the algorithm. We often speak of "extra" memory needed, not
counting the memory needed to store the input itself. Again, we use natural (but fixed-length) units
public static void printFirstItem(int[] items) { System.out.println(items[ 0 ]); }
This method runs in O(1)O(1) time (or "constant time") relative to its input. The input array could be 1 item
or 1,000 items, but this method would still just require one "step."
public static void printAllItems(int[] items) { for (int item : items) { System.out.println(item); } }
This method runs in O(n)O(n) time (or "linear time"), where nn is the number of items in the array. If the
array has 10 items, we have to print 10 times. If it has 1,000 items, we have to print 1,000 times.
public static void printAllPossibleOrderedPairs(int[] items) { for (int firstItem : items) { for (int secondItem : items) { System.out.println(firstItem + ", " + secondItem); } } }
Here we're nesting two loops. If our array has nn items, our outer loop runs nn times and our inner loop
runs nn times for each iteration of the outer loop, giving us n^2 total prints. Thus this method runs in
O(n^2) time (or "quadratic time"). If the array has 10 items, we have to print 100 times. If it has 1,
items, we have to print 1,000,000 times.
2. Common Growth Rates:
Running times for small inputs
Running times for moderate inputs
kn^2 > n^2–3n+10 for all n ≥ n.
The algorithm is order n^2 (In fact, k is 3 and n0 is 2)
3n^2 > n^2–3n+10 for all n ≥ 2.
Thus, the algorithm requires no more than k*n^2 time units for n ≥ n0. So it is O(n^2)
3. A Comparison of Growth-Rate Functions