CC 204 Module
CC 204 Module
Apply your knowledge. In this part, you will practice what you have
learned.
Assess your knowledge. You will be tested here and you will be able to
know the gaps in your understanding in this lesson. If you are not satisfied
with the feedback, you may then go back to some point that you may have
missed.
References. List down the resources and links from which the content of
the lesson was based from. These may take the form of books, internet
sites, blogs, videos, photographs, animation, PowerPoint Presentations,
icons, etc.
Directions are found inside each lesson that will tell you how long you are going to
work on this module. All formative activities must be answered and counter-checked
with the feedback attached. Honesty is a school policy. Be serious about the learning
activities you are working on. It will define who you are and what you will become in the
future. Pre-test and post-test must be completed as well. At the end of the semester or
as instructed otherwise, you are to submit this module to your subject professor.
Inquiries on some points not fully understood will be made online via Google Classroom
in a scheduled encounter. This module is a self-contained learning kit with instructions
that will guide you to the end.
The following are my personal contact information just in case you needed to
contact me immediately:
Mobile Number: 0921-718-4865
E-mail Address: [email protected]
You are now ready to begin. Happy learning and happy coding.
In programming, a data type is a set of data with predefined value. We also have a
System-defined data types or primitive data types and user defined data types.
Data Structures
Based on the discussion above, once we have data in variables, we need some
mechanism for manipulating that data to solve problems. Data structure is a particular
way of storing and organizing data in a computer so that it can be used efficiently. A
data structure is a special format for organizing and storing data. General data structure
types include arrays, files, linked lists, stacks, queues, trees, graphs and so on.
Depending on the organization of the elements, data structures are classified into two
types:
1) Linear data structures: Elements are accessed in a sequential order but it is not
compulsory to store all elements sequentially. Examples: Linked Lists, Stacks and
Queues.
2) Non – linear data structures: Elements of this data structure are stored/accessed in a
non-linear order. Examples: Trees and graphs.
Before defining abstract data types, let us consider the different view of system-defined
data types. We all know that, by default, all primitive data types (int, float, etc.) support
basic operations such as addition and subtraction. The system provides the
implementations for the primitive data types. For user-defined data types we also need
to define operations. The implementation for these operations can be done when we
want to actually use them. That means, in general, user defined data types are defined
along with their operations.
To simplify the process of solving problems, we combine the data structures with their
operations and we call this Abstract Data Types (ADTs). An ADT consists of two parts:
1. Declaration of data
2. Declaration of operations
Commonly used ADTs include: Linked Lists, Stacks, Queues, Priority Queues, Binary
Trees, Dictionaries, Disjoint Sets (Union and Find), Hash Tables, Graphs, and many
others. For example, stack uses LIFO (Last-In-First-Out) mechanism while storing the
data in data structures. The last element inserted into the stack is the first element that
gets deleted. Common operations of it are: creating the stack, pushing an element onto
the stack, popping an element from stack, finding the current top of the stack, finding
number of elements in the stack, etc.
While defining the ADTs do not worry about the implementation details. They come into
the picture only when we want to use them. Different kinds of ADTs are suited to
different kinds of applications, and some are highly specialized to specific tasks.
What we are doing is, for a given problem (preparing an omelette), we are providing a
step-by- step procedure for solving it. The formal definition of an algorithm can be
stated as:
An algorithm is the step-by-step unambiguous instructions to solve a given
problem.
In the traditional study of algorithms, there are two main criteria for judging the merits
of algorithms: correctness (does the algorithm give solution to the problem in a finite
number of steps?) and efficiency (how much resources (in terms of memory and time)
does it take to execute the steps).
To go from city “A” to city “B”, there can be many ways of accomplishing this: by flight,
by bus, by train and also by bicycle. Depending on the availability and convenience, we
choose the one that suits us. Similarly, in computer science, multiple algorithms are
available for solving the same problem (for example, a sorting problem has many
algorithms, like insertion sort, selection sort, quick sort and many more). Algorithm
analysis helps us to determine which algorithm is most efficient in terms of time and
space consumed.
The goal of the analysis of algorithms is to compare algorithms (or solutions) mainly in
terms of running time but also in terms of other factors (e.g., memory, developer
effort, etc.)
Running Time Analysis is the process of determining how processing time increases
as the size of the problem (input size) increases. Input size is the number of elements in
the input, and depending on the problem type, the input may be of different types. The
following are the common types of inputs.
Size of an array
Polynomial degree
Number of elements in a matrix
Number of bits in the binary representation of the input
Vertices and edges in a graph.
Types of Analysis
To analyze the given algorithm, we need to know with which inputs the algorithm takes
less time (performing wel1) and with which inputs the algorithm takes a long time. We
have already seen that an algorithm can be represented in the form of an expression.
That means we represent the algorithm with multiple expressions: one for the case
where it takes less time and another for the case where it takes more time.
In general, the first case is called the best case and the second case is called the worst
case for the algorithm. To analyze an algorithm we need some kind of syntax, and that
forms the base for asymptotic analysis/notation. There are three types of analysis:
Worst case
Defines the input for which the algorithm takes a long time (slowest time to
complete).
Input is the one for which the algorithm runs the slowest.
Best case
Defines the input for which the algorithm takes the least time (fastest time to
complete).
Input is the one for which the algorithm runs the fastest.
Average case
Provides a prediction about the running time of the algorithm.
Run the algorithm many times, using many different inputs that come from
some distribution that generates these inputs, compute the total running time
(by adding the individual times), and divide by the number of trials.
Assumes that the input is random.
Apply your knowledge. In this part, you will practice what you have
learned.
Assess your knowledge. You will be tested here and you will be able to
know the gaps in your understanding in this lesson. If you are not satisfied
with the feedback, you may then go back to some point that you may have
missed.
Unit 2: Linked Lists, Stacks and Queues
Objectives:
At the end of the unit, you must have:
c.
A linked list is a data structure used for storing collections of data. A linked list has the
following properties.
Successive elements are connected by pointers
The last element points to NULL
Can grow or shrink in size during execution of a program
Can be made just as long as required (until systems memory exhausts)
Does not waste memory space (but takes some extra memory for pointers). It
allocates memory as list grows.