Uploaded by Vo Vo Min

assingnment

advertisement
LO1 Define basic algorithms to carry out an operation and
outline the process of programming an application
Algorithms
An algorithm is a procedure that can be used to solve a problem. In either
hardware-based or software-based routines, they function as a detailed sequence of
instructions that carry out predetermined operations sequentially. All aspects of
information technology employ algorithms extensively. A simple technique that
resolves a recurring issues is typically referred to as an algorithm in mathematics and
computer science. For example, checking a number is even or odd is algorithm. By
this algorithms, we need to get input value first. And then, we can divide this number
with 2. If the reminder is 0 , this is even. If the remainder is not 0 , this is odd. This is
an example of algorithms. This means that we can use algorithm step by step to get
our solutions. The most popular algorithms are sorting and searching algorithms. For
example, we can search one contact in phone contacts by using searching algorithms.
And then, we can use compression algorithm to compress the folders to zip files. As
the advantages, we can get the solution in the effective way. And then, if we chose the
right algorithms for problems, we can reduce time and space for this program.
Sorting Algorithms
A sorting algorithm is the arranging the things into a specific order like
alphabetically, from shortest to longest distance and from highest to lowest value.
This algorithm uses the input data, take operations on these data and output the
ordered array. There are numerous uses for sorting algorithms, including pricing
products on a retail website and ranking websites on a search engine results page.
There are many sorting algorithms.
1. The bubble sort algorithm
2. The insertion sort algorithm
3. The shell sort algorithm
4. The quick sort algorithm
Bubble Sort
Bubble sort is the simplest sorting algorithm. Each pair of adjacent elements
in this comparison-based sorting algorithm is compared to each other, and if they are
not in the correct order, the elements are swapped. As this algorithm's average and
worst-case complexity are both O(n2), where n is the number of items, it is not
appropriate for handling huge data sets.
Our example uses an unsorted array. Because bubble sort requires O(n2) time, we're
making it succinct and direct.
The first two items in a bubble sort are compared to see which one is greater in the
beginning.
Value 33 is bigger than 14, therefore it is already in sorted places in this instance. We
then contrast 33 with 27.
Since 27 is less than 33, these two numbers must be switched.
The new array should seem as follows:
We then contrast 33 and 35. Both are already in sorted positions, as we discover.
Next, we go to the next two values: 35 and 10.
So, we know that 10 is smaller than 35. They are not sorted as a result.
These values are switched. We discover that the array’s end has been reached. The
array should seem as follows after one iterations:
We are specifically demonstrating the state of an array following each iteration right
now. After the second revision, it ought to resemble this:
Keep in mind that at least one value shifts at the end of each cycle.
Additionally, bubble sorts discovers that an array is fully sorted when no swap is
necessary.
The list is taken to be an array with n entries. We also assume that the swap function
switches the values of the elements in the supplied array.
Begin BubbleSort(list)
1. For all elements of list
2. if list[i] > list[i+1]
3. swap(list[i], list[i+1]
4. end if
5. end for
6. return list
End BubbleSort
Searching Algorithms
The methodical process used to locate particular data within a collection of
data is known as a search algorithm. It is regarded as a basic computing operation. In
computer science, using the right search algorithm can make the difference between a
fast application and a slow one while looking for data. There are many searching
algorithm. They are
1. Linear Search
2. Binary Search
3. Jump Search
4. Interpolation Search
5. Exponential Search
Binary Search
A quick search algorithm with run-time complexity of O(log n) is binary
search . Divide and conquer is the guiding philosophy behind this search algorithm.
The data collection must be in sorted form for this algorithm to function
correctly.Binary search compares the collection's middle item in an effort to find a
specific item. If there is a match, the item's index is returned. The item is searched in
the sub-array to the left of the middle item if the middle item is greater than the item.
If not, the sub-array to the right of the middle item is searched for the item. Up until
the subarray's size is reduced to zero, this process is repeated on the subarray as well.
The target array must be sorted in order for a binary search to function. We will use a
visual illustration to teach us how binary search works. Let's say that we need to use
binary search to find the value 31 in the following sorted array.
First, we'll use this algorithm to find the first half of the array.
mid = low + (high - low) / 2
As you can see, 0 + (9 - 0) / 2 = 4. (integer value of 4.5). So, the middle of the array is
number 4.
Now we contrast the value that is being searched, which is 31, with the value that is
stored at location 4. The value at position 4 is found to be 27, which does not match.
We also know that the target value must be in the top part of the array because the
value is larger than 27, and the array is sorted.
We adjust our low to mid + 1, then recalculate the new mid value.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target
value 31.
The value kept at location 7 does not match what we are looking for; rather, it is
greater. Therefore, from this place, the value must be in the lower section.
We calculate the mid again. This time it is 5.
We contrast our desired value with the value kept at location 5. We discover that it
matches.
We come to the conclusion that the goal value, 31, is kept at position 5.
Binary search minimizes the number of comparisons that must be conducted to
extremely few numbers by halving the number of searched objects.
Program Development Life Cycle
Understanding the Problem
Understanding and identifying the problem is the first step of program
development life cycle. This is the responsibility of the system analyst in large
software projects, and they deliver their findings to the programmers in the form of a
program design. The data used in the program, the processing that must occur while
looking for a solution, the output format, and the user interface are all defined in the
program specification.
Designing the program
In this step programmers can design the steps of solution. This step is the most
important step of the program development life cycle. We can design the program in
two manner: top-bottom program design or modular programming. Finding the main
routine, which is one of the program’s core activities, is the first stage. From there,
programmers attempt to split the various aspects of the main procedure into units
known as modules. Programmers create conceptual plans for each module using the
proper program design tool to show how the module will carry out its assigned task.
There are many program designs tools for designing the programs. They are
1. Algorithms
2. Flowcharts
3. Decision tables
4. Pseudocode
Coding the Program
In this step, we need to code the program by using programming languages
such as java, C++, Python and Golang . Programmers need to use the algorithm from
the previous step for coding the programs. In this step, programmers need to expert in
one programming languages and must follow the rules of this programming language.
Programmers can get the error on this step but they need to clean this error before the
next step.
Testing and debugging the program
After coding the program, programmers need to test and debug this program.
Although the programmers cleaned the errors, the program’s outputs can be incorrect.
So, they need to test the program. They might make the logical errors on the previous
step. The logical errors can not be detected by the translators because it is the mistake
that they made while planning the program. So, the programmers need to test the
programs by using the Test data. Together, syntax and logical errors are referred to as
bugs. Debugging is the process of locating errors and removing them.
Documenting the Program
The software project is completely finished now. So, the programmer need to
write the documents including algorithms, flowcharts, pseudo codes and decision
tables for those involved in the software project. Writing a manual that includes an
overview of the program's functioning, beginner tutorials, detailed explanations of
key program features, reference documentation of all program commands, and an indepth explanation of the error messages the software generates marks the end of this
phase.
LO2
Programming Paradigm
A programming paradigm is a manner of conceptualizing and organizing the
functionality of a program. Programming paradigms decide how the program's
execution and control are defined and what components the programs consist of.
Thinking a solution for a problem depends on paradigm. For example, we can think a
problem as an object in object oriented paradigm. The most programming languages
support multiple paradigm. For example, Java programming language supports not
only object oriented paradigm but also event driven paradigm. There are different type
of programming paradigm.
1.Object Oriented Programming Paradigm
2.Procedural Programming Paradigm
3.Event-driven programming paradigm
4.Functional programming paradigm
Procedural Programming Paradigm
In procedural programming paradigm, codes are executed line by line. In
addition, the larger program is divided into smaller programs called procedures. These
procedures can be defined as methods, functions or subprograms. The data that are
used by the program is in the form of variables. This data is used by the functions or
sub programs. These functions consist the computational statement to solve the
problem. This is the first paradigm for the new developer. And then, it is so good for
general-purpose programming. In addition, this paradigm can reduce the code
duplication by means of sub programs or functions. We can also track the program
flow easily. By using this paradigm, the memory requirement also slashes.
Figure :
Characteristics of Procedural Programming Paradigm
Local Variable: Local variables are declared in the scopes. These variables can not be
used from the other scope. The local variables can only be used in the method or
function it is defined in.
Local variable
Can’t access from another scope
Global Variable: Global variables are declared outside of all scopes. These variables
can be used from anywhere of the program unlike the local variable.
Global variable
Can access from other every scope
Can access from other every scope
Modularity: When two different systems with two distinct tasks at hand are brought
together to finish a bigger task first, this is known as modularity.The individual tasks
within each group of systems would subsequently be finished one by one until all
tasks were finished. For example, we can divide three modules such as booking, user
management and logging for a ticket booking application.
Sub program
Sub program
Sub program
Predefined Library : Typically, a predefined function is an instruction with a name.
Predefined functions are typically included in higher-level programming languages,
but they come from libraries or registries rather than programs. For example, we need
to import “Scanner” library to our program to build scanner object.
Import scanner class
Parameter Passing :The values can pass the methods,functions or sub routines by
their allowed parameter. The terms "pass by value," "pass by reference," "pass by
result," "pass by value-result," and "pass by the name" are all used to pass parameters.
Parameter passing to method
Object-oriented Programming Paradigm
In Object-oriented paradigm, the problems are defined as the real world
objects. This paradigm relies on the objects and the blue prints or classes for these
objects. The object-oriented paradigm is used to develop the code that is reusable and
simple. In addition, data is treated as the element in the program development and
bind the data securely than allowing it to move freely inside the program. There are
many programming languages that use the object-oriented paradigm such as Java,
Python.
Characteristics of Object-oriented Programming Paradigm
Class
The class is the abstract design or blueprint to build the specialized objects aka
particular data structures. We can create our own abstract datatype by using class. The
classes can define the properties of real objects. The classes include the fields and
behaviors. The fields are attributes and the behaviors are method. For example, a dog
class have the “name”, “age” attributes as the fields and bark() method as the
behaviors. But the class is not object and it is just the template to build the real
objects. We can create many dog objects by using “Dog” template or class.
Creating dog objects by using dog class
Dog class
Objects
Download