0% found this document useful (0 votes)
78 views

Bubble Sort Algorithm - Algorithm - Flowchart - Data Structures

This document discusses the bubble sort algorithm, provides a graphic representation through a flowchart, and describes various data structures in Python including lists, tuples, dictionaries, sets, and strings. It explains how the bubble sort algorithm works by comparing adjacent elements and swapping them if they are in the wrong order. The flowchart depicts the steps of the bubble sort algorithm. It also defines common Python data structures, provides examples of how to initialize and access elements in each structure, and explains their properties such as mutability.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Bubble Sort Algorithm - Algorithm - Flowchart - Data Structures

This document discusses the bubble sort algorithm, provides a graphic representation through a flowchart, and describes various data structures in Python including lists, tuples, dictionaries, sets, and strings. It explains how the bubble sort algorithm works by comparing adjacent elements and swapping them if they are in the wrong order. The flowchart depicts the steps of the bubble sort algorithm. It also defines common Python data structures, provides examples of how to initialize and access elements in each structure, and explains their properties such as mutability.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/343601648

Bubble Sort Algorithm | Algorithm | Flowchart | Data Structures

Research · December 2018

CITATIONS READS
0 3,234

1 author:

Dipak Kandel
London Metropolitan University
3 PUBLICATIONS   0 CITATIONS   

SEE PROFILE

All content following this page was uploaded by Dipak Kandel on 12 August 2020.

The user has requested enhancement of the downloaded file.


Table of Contents
1. The Bubble sort algorithm .................................................................................................... 1

1.1. Explanation of the algorithm ................................................................................................ 1


2. Graphic representation of the bubble sort algorithm .................................................... 2

2.1. Algorithm ................................................................................................................................... 2


2.2. Flowchart ................................................................................................................................... 3
3. Data Structures ........................................................................................................................ 4

3.1. Lists ............................................................................................................................................ 4


3.2. Tuples ......................................................................................................................................... 5
3.3. Dictionary .................................................................................................................................. 6
3.4. Sets ............................................................................................................................................. 6
3.5. Strings ........................................................................................................................................ 7
3.6. Proper Data Structures and Its Justification ................................................................... 7
Table of figures:
Figure 1 Example of Bubble sort ..................................................................................... 1
Figure 2 Bubble Sort Flowchart ....................................................................................... 3
List of Tables

Table 1 List of Books in Book Store .............................................................. 7


1. The Bubble sort algorithm
Bubble sort algorithm is a simple algorithm which is used to sort the elements like
integers of an array in an order either ascending or descending. Bubble sort compares
each element to the next one by one and sorts them based on their values. Bubble sort
algorithm works by comparing first two elements i.e. element in index 0 and 1, and
swapping them according to the need of program whether it is for ascending or
descending.

Let us take an example to visualize this algorithm better.

Random list of arrays: {2, 7, 5, 11, 4, 14, 18, 3}

After using Bubble sort algorithm: {2, 3, 4, 5, 7, 11, 14, 18}

1.1. Explanation of the algorithm


In bubble sort algorithm, we first enter an
unordered list of an array. The algorithm
compares the first two elements I.e.
elements of index 0 and 1 and swaps them if
they are not in order i.e. in greater value is in
index 0 and smaller value is in index 1 or vice
versa according to the program whether it is
Figure 1 Example of Bubble sort
done to sort in ascending order or in descending order. After first and second elements
are compared and swapped, the second and third elements are taken, compared and
Figure 2 Example of Bubble sort
then swapped. This process continues till the last group of elements. Then again
comparing process goes on the same way. Until there come no swap in a complete
iteration, the comparing and swapping process continues. When there are no swaps,
then the program knows the list/array is sorted and the process stops. Then as output
sorted list of an array is obtained. The algorithm performs the operations like taking
elements from particular index, comparing the elements, swapping the elements, and
iterating over and over through the list or an array. (Sehgal, 2017)

Dipak Kandel 1|Page


2. Graphic representation of the bubble sort algorithm

2.1. Algorithm
Before going to the flowchart, let’s understand the algorithm of the bubble sort. So
the step by step algorithm of the bubble sort algorithm is given below:

Step 1: Start BubbleSort


Step 2: input a List
Step 3: sorted = False
Step 5: integer swaps = 0
Step 6: for I =1 to list. Count - 1
Step 7: if list [i-1]>list[i] go to step number 8 else to step 6
Initialize temp=list [i-1]
List [i-1] = list[i]
List[i] = temp

Step 8: swaps = swaps + 1


Step 9: if swaps == 0 go to step 15 else go to step 16
Step 10: Update variable sorted with Boolean value “True”
Step 11: Variable Sorted = False
Step 12: Output List
Step 13: End BubbleSort

Dipak Kandel 2|Page


2.2. Flowchart

Figure 3 Bubble Sort Flowchart

Dipak Kandel 3|Page


3. Data Structures
Data Structures is a specialized format or a way of organizing and storing data so that
the data could be easily accessed, iterated and managed. A data structure is designed
to organize specific purpose so that it can be accessed and worked in easier and
appropriate way. Lists, Tuples, Dictionaries, Strings and Sets etc. are some of the
examples of Data Structures which are built-in in Python. In programming Data Structures
are selected to store data for the purpose of working on it with various algorithms.
(Cokelaer, 2014)

The Collection Data Types are given and described below:

3.1. Lists
List is the ordered collection data type with ordered sequence of data information
which are accessed according to their index value. List can have any numbers and any
type of items. Index value starts from 0 to list length – 1. Lists are mutable data structures
so that the list values can be added, removed, or simply changed even after the list is
defined and created. List can contain arbitrary objects, lists can be nested and are
Dynamic. Indexing in list can be run negatively i.e. the last element inside the list can be
accessed by the index value -1. List use Square Brackets [] to initialize. Following are
some examples of functions in lists. (Sturtz, 2018)

List = [1, 2, 3, 4, 5] #Initialized a list

Index value - 0, 1, 2, 3, 4 Positive Indexing

-5, -4, -3, -2, -1 Negative Indexing

 Print(list[0])
o Output = 1
 Print(list[-1])
o Output = 5
 list.append (6) #add element to the end of

Dipak Kandel 4|Page


the list now, list = [1, 2, 3, 4, 5, 6]

 list.remove(4) #delete an element 4 from the list

 del(list[2]) #delete an element 3 from the list when the 2 index is


deleted

3.2. Tuples
Tuples are quiet identical to lists in python. Tuples are defined by enclosing the
elements in parenthesis () instead of square brackets. Tuples are immutable data types
in python. Tuples are ordered, can contain arbitrary objects. Tuples can be indexed sliced
and can be nested as in lists. Tuples can be created with zero items represented by blank
(). Following are some examples of tuples and some functions on it. (Sturtz, 2018)

Tuple1 = (“ram”, “35”, “Monday”, “2.5”) #Declaring a tuple and initializing elements.

print (tuple1 [0]) print (tuple1 [3])

Output = ram Output = 2.5

Dipak Kandel 5|Page


3.3. Dictionary
Python dictionary is an unordered collection of items. Dictionary has a key: value pair
whereas whether data structures have only one value as element. Dictionary is created by placing
key value pair inside curly braces {} separated by a comma. In key value pair, Keys are immutable
type like string, number or tuple whereas values can be of any data type. The name of item is key
and corresponding defined item is value and as a whole, it is key value pair. Some of the examples
of dictionaries and its functions are given below: (Sturtz, 2018)

Dictionary1= {‘name’: “tommy”, ‘class’:4} #initialized a new dictionary named


Dictionary1

Key : value1 key : value 2

Print (dictionary1 [‘name’])

Output = tommy

3.4. Sets
A set is an unordered collection of items. Sets are used to perform mathematical
operations like union, intersection, Symmetric difference, etc. To create a set, we use curly braces
{}. Elements are placed inside curly braces and separate by a comma. Set can be of mixed data
types. Set does not support changing of an element of set by using indexing or slicing. Some of
the examples of sets are given below. (Anon., n.d.)

Set1 = {12,15,14,18,16 } #Initialized a set named set1

Set2 = {12,16,58,14,27 } #Initialized a set named set2

Print (set1.intersection(set2))

Output = {12, 16, 14 }

Dipak Kandel 6|Page


3.5. Strings
String is a Sequence of characters. In python string is a sequence of Unicode Characters.
Strings can be created by enclosing character inside a single or double quote. String is immutable,
once defined, characters can’t be added or removed from the string. Following are the examples
of string data types. (GeeksforGeeks, 2018)

String1 = “hello_World”

print (String1)

Output = hello_World

3.6. Proper Data Structures and Its Justification

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 1 List of Books in Book Store


Above table consists of some data related to a book store. The table includes data
types like integers and strings. So in the python, the suitable data type for above data is
Dictionary. Dictionary brings all the data types in single program. Dictionary makes data
management easy by providing quick report on the data and resource that the objects are
using. Dictionaries have many key value pairs so we can customize the data’s, we can
easily add or remove specific data and key value even after it is assigned. So that I prefer
using dictionary data type for the given data table.

Dipak Kandel 7|Page

View publication stats

You might also like