Uploaded by Gift Adhikari

Report

advertisement
ISLINGTON COLLEGE
Information System
CC4002NA
Coursework 1
Submitted by:
Submitted to:
Saugat Adhikari
Mr. Sukrit Shakya
Group: N7
Module Leader
Date: 11 December 2018
Information System
CC402NA
INFORMATION SYSTEM
Table of Contents
1.Bubble sort Algorithm ------------------------------------------------------------------------------------ 4
1.1
Introduction: ------------------------------------------------------------------------------------ 4
1.2
Stepwise Explanation of Bubble Sort: --------------------------------------------------------- 5
1.2
Python Code for Bubble Sort: ----------------------------------------------------------------- 7
2.Graphical Representation of Bubble Sort algorithm -------------------------------------------- 10
-------------------------------------------------------------------------------------------------------------------- 10
3. Collection Data Types ----------------------------------------------------------------------------------- 12
3.1 Lists: ----------------------------------------------------------------------------------------------------12
3.2 Tuples: ------------------------------------------------------------------------------------------------15
3.3 Dictionaries: ----------------------------------------------------------------------------------------- 16
3.4 Sets: ----------------------------------------------------------------------------------------------------17
3.5 Data of a book store: ------------------------------------------------------------------------------- 18
4. Learning Reflection: ------------------------------------------------------------------------------------- 21
BIBLIOGRAPHY ------------------------------------------------------------------------- 22
1|PAGE
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
List of Tables
Table 1: List of unsorted numbers. ......................................................................................................... 5
Table 2 : First Pass ................................................................................................................................ 5
Table 3: Second Pass ........................................................................................................................... 5
Table 4: Third Pass ............................................................................................................................... 6
Table 5: Data of a book store.................................................................................................................18
2|PAGE
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
3|PAGE
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
1.BUBBLE SORT ALGORITHM
1.1 Introduction:
The rapid growth in the amount of information stored in this modern
world requires an effectual way in which this data needs to be ordered
for it be accessed easily when required. Many years ago, it was
estimated that half the time on many commercial computers was spent
in sorting the data. (Ms. Jyoti Mundra, September, 2015) Since then, we
have been introduced to different ways of sorting data; such as, Bubble
Sort, Insertion Sort, Merge Sort, etc.
Figure 1: Sorting example. (Karleigh Moore, 2016)
Bubble sort also known as a sinking sort, is a sorting algorithm that
compares the adjacent pairs and swap them if necessary, causing the
items to ‘bubble’ up towards their proper position. (Anon., n.d.) For the
computer to entirely spot the sorting of elements; the swap process
continues until no swaps are necessary.
4|PAGE
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
1.2 Stepwise Explanation of Bubble Sort:
Example:
50
20
15
35
30
Table 1: List of unsorted numbers.
The table shown below depicts the step by step algorithm of Bubble
Sort on the given list of numbers in order to sort them in an ascending
order.
20
50
15
35
30
20
15
50
35
30
20
15
35
50
30
20
15
35
30
50
Table 2 : First Pass
15
20
35
30
50
15
20
35
30
50
15
20
30
35
50
15
20
30
35
50
Table 3: Second Pass
5|PAGE
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
15
20
30
25
50
15
20
30
35
50
15
20
30
35
50
15
20
30
35
50
Table 4: Third Pass
Pass; in a bubble sort is the one complete cycle; where the algorithm
compares and swaps all the adjacent elements throughout the list. A
new pass always continues with the list of values which is derived at the
end of the previous pass and so on. In Table 1: the painted areas
display the swapping of elements in pairs, if they are out of order. In
other words, the algorithm is comparing the two elements and placing
the smallest value to the front position or leaving it unchanged; if the
pair is already in order. This process continues until all the adjacent
numbers are exchanged and by the end of first pass, the largest
number always travels to the last of the list.
Table 2 depicts the similar procedure of swapping and sorting the
numbers in pairs and continuing till essential. In the above example, it is
evident that all the numbers are arranged in the ascending order by the
end of second pass. But the only way computer deems that the array of
numbers is sorted, if there are no swaps seen though an entire pass.
Hence, as there are clearly no swaps in Table 3; the computer
recognizes the numbers are sorted in ascending order.
6|PAGE
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
1.2 Python Code for Bubble Sort:
The python coding implementation of the above example is provided
below:
Figure 2: IDLE Code for bubble sort
Figure 3: Result of the above idle coding
7|PAGE
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
Above figure demonstrates the proper python coding for implementing
the bubble sort algorithm on the given list of numbers; in order to sort
them in ascending order.
1. We input the provided list that requires sorting; i.e.
arr =[50,15,20,35,30]
2. We create a flag variable; which has same value until the
condition is true and then changes as required. i.e. i = 1.
3. We simply use a while loop which is triggered when the value of
our flag variable (i) is smaller or equal to the length of the
list(len(arr)).
4. Then, we create a for loop in order to iterate through the elements
in our list using their indexes. The for loop will have a counter
variable(j) that has a range from 1 to length of the list. So, the
value of j throughout the loop will change 4 times; i.e.1,2,3,4. As
the highest index of any list is a number short that the actual
length of the list, we use the maximum range value as the length
of the list.
5. We already know that bubble sort swaps the two adjacent values
until no swaps are needed. For that, we use an if statement to
compare the value at index [j-1] and value at index [j]. This
statement will only be true when the value at [j-1] is bigger than
value at [j].
8|PAGE
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
6. If the statement is true, we swap those two values by simply
using the temp function. But if the above statement is false or the
preceding value is already smaller than the current value; no
swaps will be done.
7. We had used a counter in while loop, namely (i). For all the
numbers to get sorted, this variable needs to be increased by 1
until the length of the list is met. When the value of i becomes
equal to the length of the list, we get out of the while loop. This
means the required swaps are carried out and the given list is
sorted.
8. Finally, we print the list, which will provide us the above list sorted
in ascending order. i.e. arr = [15,20,30,35,50]
9|PAGE
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
2.GRAPHICAL REPRESENTATION OF BUBBLE SORT ALGORITHM
Start
Print(arr)
Input
List(arr)
i=1
temp=arr[j-1]
arr[j-1]=arr[j]
arr[j]=temp
i=i+1
True
If arr[j-1]>arr[j]
If i<=len(arr)?
True
True
False
j=1
If
j<len(arr)?
j=j+1
False
STOP
10 | P A G E
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
11 | P A G E
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
3. COLLECTION DATA TYPES
3.1 Lists:
A list is an ordered sequence of information/values which can be
retrieved with the use of index. The values that make up a list are called
its element or items. Lists are like strings, which are ordered collection
of characters, except that the elements of a list can be of any data type.
(Peter Wentworh, 2012)
There are several ways in which a list can be created; the basic
characteristic of list is that it is enclosed within square brackets. For
example:
L_1 = [20,35,47,68,90]
L_2 = [“information”, “system”, “python”]
As we know that the elements in the list are accessed with the help of
special integers know as index. Index permanently start form 0 and end
at a number less than the length of the list. Negative indexing can also
be carried out when the actual length of the list is unknown to us; but it
always starts from (-1). Example:
L_1 = [20, 35, 47, 68, 90]
Index=0
1
2
3
4
12 | P A G E
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
Print(L_1[0]) will print 20; the value at 0th index.
Print(L_1[4]) will print 90; the value at 4th index.
Another vital characteristic of a list which makes it very useful and
applicable in data collection is the fact that lists are mutable. We can
easily manipulate list according to our requirement. Some basic
functions used to mutate lists are provided below:
Length: The inbuilt function for finding the length of any given list is
len(List).
Example:
L_1 = [20,35,47,68,90]
print len(L_1)
13 | P A G E
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
Figure 4: Mutating Lists.
The descriptions of functions carried out in Figure 4 are listed below:
1. Changing elements at a certain index.
2. Adding an element to the end of the list.
3. Adding two lists to form a new list.
4. Extending a list by adding elements at its end.
5. Removing an element.
6. Deleting an element at certain index.
7. Eliminating last element of the list.
14 | P A G E
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
3.2 Tuples:
A tuple is used to group any number of values/elements into a single
compound value. Syntactically, a tuple is comma – separated sequence
of values enclosed with parentheses.
By nature, Tuples are immutable and can only be used to store constant
values. So, Tuples are useful for representing records – some related
information that belong together. A tuple lest us “chunk” together related
information and use it as a single thing.
Example:
Bond = (“Dr. No”, 1963, “From Russia With Love”, 1962)
The above example shows the use of tuple in collecting the James
Bond movies and their respective year of release. Institutions generally
use tuples for storing student records; includes Student name, Student
Grade, Student Attendance, etc. Alike lists, the length of tuple can be
required by using the len(Tuple) function as shown below:
len(Bond) = 4
Following syntax shows the code for swapping values within the tuple;
operated on above example:
(1963, 1962) = (1962, 1963)
This prints the above tuple as:
Bond = (1962, “Dr. No”, 1962, “From Russia With Love”, 1963)
15 | P A G E
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
3.3 Dictionaries:
Dictionaries are yet another kind of compound data type, which serves
as a mapping type in Python. The map keys, that can be any immutable
type; to their corresponding values, that can be heterogenous in nature.
Other computing languages even refer dictionaries as associative
arrays since they associate key with a value. (Peter Wentworh, 2012, p.
267)
Dictionary is created denoted by { } and can be created by adding key :
value pairs.
Example:
Dec = {‘’messi’:33, ’dembele’:21, ‘Countinho’:26}
As dictionary is a compound data type without sequence, we cannot
index or slice a dictionary. But we can access the values in dictionary by
using its custom key. Values in a dictionary can be duplicate but keys
must always be unique; as they are immutable.
Dec = {‘Messi’: 33, ’Dembele’: 21, ‘Coutinho’: 26}
Key1 Value1 Key2 Value2
Key 3
Value3
Furthermore, a dictionary can also contain another dictionary inside it in
the form of value.
Example:
D = {4: {1:0, 3:2}
16 | P A G E
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
3.4 Sets:
Set in python is an unordered collection of elements, that is mutable in
nature. Since, the values inside a set is unordered; there is no concept
of indexing. Sets generally contain unique elements because adding
duplicates can be safe but is pointless.
Sets can be defined by naming all its values inside {} brackets. The only
exception is empty set, which can be created using set(). If set(…) has
a list, a string or a tuple as a parameter, it will return a set composed of
its elements. (Denis Kirienko, n.d.)
Figure 5: Set operations
17 | P A G E
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
3.5 Data of a book store:
Book ID
Name
Price
Quantity
B001
Harry Potter(JK Rowling)
$2
30
B002
Start with Why(Simon Sinek)
$1.5
10
B003
Programming with Python(John Smith)
$1.5
20
Table 5: Data of a book store
We have already discoursed various collection data types used for
storing information in python above in the report. 2-D list is my choice
for storing the provided data, in the terms of effectiveness and
convenience. Let’s suppose, we are not aware of the number of values
a user is going to input; a 2D array with a maximum limiting size is the
best solution to this situation. Some real life uses of 2D lists are; used in
image processing as matrices, filters that are used to remove noises
generated in a recording are also 2D arrays. The above table is the
most common way for storing data in real life; so, 2D lists are used to
imitate such tables in python.
The above data can be represented in python as:
Book_id = [‘B001’, ‘B002’,’B003’]
Book_name = [‘Harry Potter’, ‘Start with Why’, ‘Programming with
Python’]
18 | P A G E
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
Price = [‘$2’, ‘$1.5’, ‘$1.5’]
Quantity = [30,10,20]
After using 2D lists, the above lists can be coded as:
Choice = [ [‘Book_id’, ‘Book_name’, ‘Price’, ‘Quantity’]
[‘B001’, ‘Harry Potter’, ‘$2’, 30]
[‘B002’, ‘Start with Why’, ‘$1.5’, 10]
[‘B003’, ‘Programming with Python’, ‘$1.5’, 20] ]
Storing the above data in Tuple would be illogical, since we won’t be
able mutate a Tuple in order to add or remove any element. Similarly,
dictionaries and sets could have been used, but they seem to have
unordered collection of elements. Hence, 2 dimensional lists or arrays is
the best option for the above data; as it serves as a matrix which is
easily accessible with indexes and the data/elements can be easily
manipulated as per requirement.
We can iterate through all the elements in the above 2D list by using for
loop:
19 | P A G E
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
Figure 6: Iterating book store data in 2D list.
Figure 7: Printing result of book store data.
20 | P A G E
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
4. LEARNING REFLECTION:
I was very intrigued when I found out that I was going to partake in the study of
Information System in this course. For a student new to the Information Technology
field, Information System is a must, as it teaches us the basics of collecting, organizing,
storing and comparing information. A small business to large organizations relies on
Information System to sustain their operation and compete in the global market. Our
module till date mainly consisted of introduction to Information System, Python
Programming language, various data types and its collection and manipulation.
Coming from Accounting and Management field, I was familiar with data assembly
and management in the form of Tables, Statistics, Graphs, etc. But I was skeptic that I
would be able to completely grasp Python Programming and Coding; which was an
uncharted territory for me. Honestly speaking, I am content with my understanding as
of now; as I feel like I have exceled the basics of Python and all the concepts we have
learned till date. Of course, the understanding of any subject or concept rest on the
learning capabilities and preparation undergone by the Student. But personally, I
wouldn’t have been able to reach this level of understanding of the unit without prior
history in programming language; if it wasn’t for the excellent teaching ability and
dedication show to their craft by my lecturer and tutor of this module. During the
early weeks, I had some difficulty with providing proper indentation and overall
presentation of my coding in python; but Mr. Sarun Dahal who has maintained such
strong rapport with all his students, personally helped me in overcoming my loopholes
in the coding and hence, making it more professional and accurate.
The current course structure for the first year starts the study of two programming
languages at once; namely Java and Python. So, I have found this as my biggest
drawback; as I seem to be jumbling the codes/syntaxes of the two programming
languages randomly and getting more disordered. But, with proper guidance and
rigorous practice, I am optimistic about fixing this problem and being assured in
Python Coding and concepts in the coming weeks. Other than the mentioned
problem, I am enjoying the learnings of this module and am looking forward to further
learnings about this language between user and computer in Information System.
21 | P A G E
SAUGAT ADHIKARI
CC402NA
INFORMATION SYSTEM
Bibliography
Anon., n.d. Chegg.com. [Online]
Available at: https://www.chegg.com/homework-help/definitions/bubble-sort-3
[Accessed 8 December 2018].
Denis Kirienko, V. P., n.d. Snakify. [Online]
Available at: https://snakify.org/en/lessons/sets/
[Accessed 11 December 2018].
Karleigh Moore, G. P., 2016. Sorting Algorithms. [Online]
Available at: https://brilliant.org/wiki/sorting-algorithms/
[Accessed 8 December 2018].
Ms. Jyoti Mundra, M. B., September, 2015. Minimizing Execution Time of Bubble Sort
Algorithm. Internation Journal of Computer Science and Mobile Computing, 4(9), p. 173.
Peter Wentworh, J. E., 2012. Lists. In: How to Think Like a Computer Scientist: Learning
with Python. s.l.:dreamtech, pp. 145, 267.
22 | P A G E
SAUGAT ADHIKARI
Download