SlideShare a Scribd company logo
1 of 49
1
Day 13
Bubble sort
Agenda
In this module, you will learn about:
Theory:
 The bubble sort
 Operations on lists | slices
Practicals:
 Sorting a list
 Slices
 Slices - negative indices
 Slices: continued
2
The bubble sort
• Now that you can effectively juggle the elements of lists, it's time to learn how to sort
them. Many sorting algorithms have been invented so far, which differ a lot in speed, as
well as in complexity. We are going to show you a very simple algorithm, easy to
understand, but unfortunately not too efficient, either. It's used very rarely, and certainly
not for large and extensive lists.
• Let's say that a list can be sorted in two ways:
• increasing (or more precisely - non-decreasing) - if in every pair of adjacent elements, the
former element is not greater than the latter;
• decreasing (or more precisely - non-increasing) - if in every pair of adjacent elements, the
former element is not less than the latter.
• In the following sections, we'll sort the list in increasing order, so that the numbers will
be ordered from the smallest to the largest.
3
Here's the list:
8 10 6 2 4
4
We'll try to use the following approach: we'll take the first and the second elements and compare
them; if we determine that they're in the wrong order (i.e., the first is greater than the second),
we'll swap them round; if their order is valid, we'll do nothing. A glance at our list confirms the
latter - the elements 01 and 02 are in the proper order, as in 8 < 10.
Now look at the second and the third elements. They're in the wrong positions. We have to swap
them:
• We'll try to use the following approach: we'll take the first and the second elements
and compare them; if we determine that they're in the wrong order (i.e., the first is
greater than the second), we'll swap them round; if their order is valid, we'll do
nothing. A glance at our list confirms the latter - the elements 01 and 02 are in the
proper order, as in 8 < 10.
• Now look at the second and the third elements. They're in the wrong positions. We
have to swap them:
5
8 6 10 2 4
6
• We go further, and look at the third and the fourth elements. Again, this is not what it's
supposed to be like. We have to swap them:
7
8 6 2 10 4
• Now we check the fourth and the fifth elements. Yes, they too are in the wrong positions.
Another swap occurs:
8
8 6 2 4 10
• The first pass through the list is already finished. We're still far from finishing our
job, but something curious has happened in the meantime. The largest element, 10,
has already gone to the end of the list. Note that this is the desired place for it. All
the remaining elements form a picturesque mess, but this one is already in place.
9
• Now, for a moment, try to imagine the list in a slightly different way -
namely, like this:
10
10
4
2
6
8
• Look - 10 is at the top. We could say that it floated up from the bottom to the
surface, just like the bubble in a glass of champagne. The sorting method derives its
name from the same observation - it's called a bubble sort.
• Now we start with the second pass through the list. We look at the first and second
elements - a swap is necessary:
11
6 8 2 4 10
12
• Time for the second and third elements: we have to swap them too:
13
6 2 8 4 10
• Now the third and fourth elements, and the second pass is finished, as 8 is already in
place:
14
6 2 4 8 10
• We start the next pass immediately. Watch the first and the second elements carefully
- another swap is needed:
15
2 6 4 8 10
• Now 6 needs to go into place. We swap the second and the third elements:
16
2 4 6 8 10
2 4 6 8 10
17
• The list is already sorted. We have nothing more to do. This is exactly what we want.
• As you can see, the essence of this algorithm is simple: we compare the adjacent elements,
and by swapping some of them, we achieve our goal.
• Let's code in Python all the actions performed during a single pass through the list, and then
we'll consider how many passes we actually need to perform it. We haven't explained this so
far, and we'll do that a little later.
18
• Sorting a list
• How many passes do we need to sort the entire list?
• We solve this issue in the following way: we introduce another variable; its task is to observe if
any swap has been done during the pass or not; if there is no swap, then the list is already sorted,
and nothing more has to be done. We create a variable named swapped, and we assign a value of
False to it, to indicate that there are no swaps. Otherwise, it will be assigned True.
myList = [8, 10, 6, 2, 4] # list to sort
for i in range(len(myList) - 1): # we need (5 - 1) comparisons
if myList[i] > myList[i + 1]: # compare adjacent elements
myList[i], myList[i + 1] = myList[i + 1], myList[i] #
19
• You should be able to read and understand this program without any problems:
• myList = [8, 10, 6, 2, 4] # list to sort
• swapped = True # it's a little fake - we need it to enter the while loop
while swapped:
swapped = False # no swaps so far
for i in range(len(myList) - 1):
if myList[i] > myList[i + 1]:
swapped = True # swap occured!
myList[i], myList[i + 1] = myList[i + 1], myList[i]
print(myList)
20
• Run the program and test it.
21
The bubble sort - interactive version
• In the editor you can see a complete program, enriched by a conversation with the user, and
allowing the user to enter and to print elements from the list: The bubble sort - final interactive
version.
• Python, however, has its own sorting mechanisms. No one needs to write their own sorts, as
there is a sufficient number of ready-to-use tools.
• We explained this sorting system to you because it's important to learn how to process a list's
contents, and to show you how real sorting may work.
22
• If you want Python to sort your list, you can do it like this:
myList = [8, 10, 6, 2, 4]
myList.sort()
print(myList)
• It is as simple as that.
• The snippet's output is as follows:
23
myList = []
swapped = True
num = int(input("How many elements do
you want to sort: "))
for i in range(num):
val = float(input("Enter a list element: "))
myList.append(val)
while swapped:
swapped = False
for i in range(len(myList) - 1):
if myList[i] > myList[i + 1]:
swapped = True
myList[i], myList[i + 1] = myList[i + 1],
myList[i]
print("nSorted:")
print(myList)
24
Key takeaways
1. You can use the sort() method to sort elements of a list, e.g.:
lst = [5, 3, 1, 2, 4]
print(lst)
lst.sort()
print(lst) # outputs: [1, 2, 3, 4, 5]
25
• There is also a list method called reverse(), which you can use to reverse the list, e.g.:
lst = [5, 3, 1, 2, 4]
print(lst)
lst.reverse()
print(lst) # outputs: [4, 2, 1, 3, 5]
26
• Exercise 1
• What is the output of the following snippet?
lst = ["D", "F", "A", "Z"]
lst.sort()
print(lst)
27
• Exercise 2
• What is the output of the following snippet?
a = 3
b = 1
c = 2
lst = [a, c, b]
lst.sort()
print(lst)
28
• Exercise 3
• What is the output of the following snippet?
a = "A"
b = "B"
c = "C"
d = " "
lst = [a, b, c, d]
lst.reverse()
print(lst)
29
Operations on lists
• The inner life of lists
• Now we want to show you one important, and very surprising, feature of lists, which strongly
distinguishes them from ordinary variables.
• We want you to memorize it - it may affect your future programs, and cause severe problems if
forgotten or overlooked.
• Take a look at the snippet in the editor.
30
• The program:
• creates a one-element list named list1;
• assigns it to a new list named list2;
• changes the only element of list1;
• prints out list2.
• The surprising part is the fact that the program will output: [2], not [1], which seems to be the
obvious solution.
• Lists (and many other complex Python entities) are stored in different ways than ordinary
(scalar) variables.
31
• You could say that:
• the name of an ordinary variable is the name of its content;
• the name of a list is the name of a memory location where the list is stored.
• Read these two lines once more - the difference is essential for understanding
what we are going to talk about next.
• The assignment: list2 = list1 copies the name of the array, not its contents. In
effect, the two names (list1 and list2) identify the same location in the
computer memory. Modifying one of them affects the other, and vice versa.
• How do you cope with that?
32
Operations on lists | slices
• Powerful slices
• Fortunately, the solution is at your fingertips - its name is the slice.
• A slice is an element of Python syntax that allows you to make a brand new copy of a list, or parts of a
list.
• It actually copies the list's contents, not the list's name.
• This is exactly what you need. Take a look at the snippet below:
list1 = [1]
list2 = list1[:]
list1[0] = 2
print(list2)
• Its output is [1].
33
• This inconspicuous part of the code described as [:] is able to produce a brand new list.
• One of the most general forms of the slice looks as follows:
myList[start:end]
• As you can see, it resembles indexing, but the colon inside makes a big difference.
• A slice of this form makes a new (target) list, taking elements from the source list - the
elements of the indices from start to end - 1.
• Note: not to end but to end - 1. An element with an index equal to end is the first
element which does not take part in the slicing.
34
• Using negative values for both start and end is possible (just like in indexing).
• Take a look at the snippet:
myList = [10, 8, 6, 4, 2]
newList = myList[1:3]
print(newList)
• The newList list will have end - start (3 - 1 = 2) elements - the ones with indices equal to
1 and 2 (but not 3).
• The snippet's output is: [8, 6]
35
# Copying the whole list
list1 = [1]
list2 = list1[:]
list1[0] = 2
print(list2)
# Copying part of the list
myList = [10, 8, 6, 4, 2]
newList = myList[1:3]
print(newList)
36
Slices - negative indices
• Look at the snippet below:
myList[start:end]
• To repeat:
• start is the index of the first element included in the slice.
• end is the index of the first element not included in the slice.
37
• This is how negative indices work with the
slice:
myList = [10, 8, 6, 4, 2]
newList = myList[1:-1]
print(newList)
• The snippet's output is: [8, 6, 4].
• If the start specifies an element lying further
than the one described by the end (from the
list's beginning point of view), the slice will
be empty:
myList = [10, 8, 6, 4, 2]
newList = myList[-1:1]
print(newList)
• The snippet's output is: [].
38
• Slices: continued
• If you omit the start in your slice, it is assumed
that you want to get a slice beginning at the
element with index 0.
• In other words, the slice of this form:
myList[:end]
• is a more compact equivalent of:
myList[0:end]
• Look at the snippet below:
myList = [10, 8, 6, 4, 2]
newList = myList[:3]
print(newList)
• This is why its output is: [10, 8, 6].
39
• Similarly, if you omit the end in your slice, it is assumed that you want the slice to end at the element with the index
len(myList).
• In other words, the slice of this form:
myList[start:]
• is a more compact equivalent of:
myList[start:len(myList)]
• Look at the following snippet:
myList = [10, 8, 6, 4, 2]
newList = myList[3:]
print(newList)
• Its output is therefore: [4, 2].
40
Slices: continued
• As we've said before, omitting both start and end makes a copy of the whole list:
myList = [10, 8, 6, 4, 2]
newList = myList[:]
print(newList)
• The snippet's output is: [10, 8, 6, 4, 2].
41
• The previously described del instruction is able to delete more than just a
list's element at once - it can delete slices too:
myList = [10, 8, 6, 4, 2]
del myList[1:3]
print(myList)
• Note: in this case, the slice doesn't produce any new list!
• The snippet's output is: [10, 4, 2].
42
• Deleting all the elements at once is possible too:
myList = [10, 8, 6, 4, 2]
del myList[:]
print(myList)
• The list becomes empty, and the output is: [].
• Removing the slice from the code changes its meaning dramatically.
• Take a look:
43
myList = [10, 8, 6, 4, 2]
del myList
print(myList)
• The del instruction will delete the list itself, not its content.
• The print() function invocation from the last line of the code will then cause a
runtime error.
44
The in and not in operators
• Python offers two very powerful operators, able to look through the list in order to
check whether a specific value is stored inside the list or not.
• These operators are:
• elem in myList
• elem not in myList
• The first of them (in) checks if a given element (its left argument) is currently stored
somewhere inside the list (the right argument) - the operator returns True in this case
45
• The second (not in) checks if a given element (its left argument) is absent in a list -
the operator returns True in this case.
• Look at the code in the editor. The snippet shows both operators in action. Can you
guess its output? Run the program to check if you were right
46
myList = [0, 3, 12, 8, 2]
print(5 in myList)
print(5 not in myList)
print(12 in myList)
47
Review of the Day
3rd floor Prabhakar Plaza, Station Road, Kolhapur.
Pin -416416
Mobile Number-9225804090

More Related Content

Similar to day 13.pptx

Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxfestockton
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAhmad Bashar Eter
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Introduction of Python-1.pdf
Introduction of Python-1.pdfIntroduction of Python-1.pdf
Introduction of Python-1.pdfSyamsulFattanSSos
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptxASRPANDEY
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.pptSeethaDinesh
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdfharamaya university
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)Neil Soliven
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3smruti sarangi
 
Sorting types and Algorithms
Sorting types and AlgorithmsSorting types and Algorithms
Sorting types and AlgorithmsAli Khan
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
Quick Sort in data structure.pptx
Quick Sort in data structure.pptxQuick Sort in data structure.pptx
Quick Sort in data structure.pptxujjwalmatoliya
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...Tosin Amuda
 
An Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List AlgorithmsAn Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List AlgorithmsBlue Elephant Consulting
 

Similar to day 13.pptx (20)

Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docxAssg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx
 
Acm aleppo cpc training sixth session
Acm aleppo cpc training sixth sessionAcm aleppo cpc training sixth session
Acm aleppo cpc training sixth session
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Introduction of Python-1.pdf
Introduction of Python-1.pdfIntroduction of Python-1.pdf
Introduction of Python-1.pdf
 
MODULE-2.pptx
MODULE-2.pptxMODULE-2.pptx
MODULE-2.pptx
 
unit 5 stack & queue.ppt
unit 5 stack & queue.pptunit 5 stack & queue.ppt
unit 5 stack & queue.ppt
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
tupple.pptx
tupple.pptxtupple.pptx
tupple.pptx
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
Sorting types and Algorithms
Sorting types and AlgorithmsSorting types and Algorithms
Sorting types and Algorithms
 
Data Structure lec#2
Data Structure lec#2Data Structure lec#2
Data Structure lec#2
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Linked list
Linked listLinked list
Linked list
 
Quick Sort in data structure.pptx
Quick Sort in data structure.pptxQuick Sort in data structure.pptx
Quick Sort in data structure.pptx
 
ds bridge.pptx
ds bridge.pptxds bridge.pptx
ds bridge.pptx
 
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
An Experiment to Determine and Compare Practical Efficiency of Insertion Sort...
 
An Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List AlgorithmsAn Introduction To Python - Tables, List Algorithms
An Introduction To Python - Tables, List Algorithms
 
Sorting
SortingSorting
Sorting
 

Recently uploaded

Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 

Recently uploaded (20)

Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 

day 13.pptx

  • 2. Agenda In this module, you will learn about: Theory:  The bubble sort  Operations on lists | slices Practicals:  Sorting a list  Slices  Slices - negative indices  Slices: continued 2
  • 3. The bubble sort • Now that you can effectively juggle the elements of lists, it's time to learn how to sort them. Many sorting algorithms have been invented so far, which differ a lot in speed, as well as in complexity. We are going to show you a very simple algorithm, easy to understand, but unfortunately not too efficient, either. It's used very rarely, and certainly not for large and extensive lists. • Let's say that a list can be sorted in two ways: • increasing (or more precisely - non-decreasing) - if in every pair of adjacent elements, the former element is not greater than the latter; • decreasing (or more precisely - non-increasing) - if in every pair of adjacent elements, the former element is not less than the latter. • In the following sections, we'll sort the list in increasing order, so that the numbers will be ordered from the smallest to the largest. 3
  • 4. Here's the list: 8 10 6 2 4 4 We'll try to use the following approach: we'll take the first and the second elements and compare them; if we determine that they're in the wrong order (i.e., the first is greater than the second), we'll swap them round; if their order is valid, we'll do nothing. A glance at our list confirms the latter - the elements 01 and 02 are in the proper order, as in 8 < 10. Now look at the second and the third elements. They're in the wrong positions. We have to swap them:
  • 5. • We'll try to use the following approach: we'll take the first and the second elements and compare them; if we determine that they're in the wrong order (i.e., the first is greater than the second), we'll swap them round; if their order is valid, we'll do nothing. A glance at our list confirms the latter - the elements 01 and 02 are in the proper order, as in 8 < 10. • Now look at the second and the third elements. They're in the wrong positions. We have to swap them: 5
  • 6. 8 6 10 2 4 6
  • 7. • We go further, and look at the third and the fourth elements. Again, this is not what it's supposed to be like. We have to swap them: 7 8 6 2 10 4
  • 8. • Now we check the fourth and the fifth elements. Yes, they too are in the wrong positions. Another swap occurs: 8 8 6 2 4 10
  • 9. • The first pass through the list is already finished. We're still far from finishing our job, but something curious has happened in the meantime. The largest element, 10, has already gone to the end of the list. Note that this is the desired place for it. All the remaining elements form a picturesque mess, but this one is already in place. 9
  • 10. • Now, for a moment, try to imagine the list in a slightly different way - namely, like this: 10 10 4 2 6 8
  • 11. • Look - 10 is at the top. We could say that it floated up from the bottom to the surface, just like the bubble in a glass of champagne. The sorting method derives its name from the same observation - it's called a bubble sort. • Now we start with the second pass through the list. We look at the first and second elements - a swap is necessary: 11
  • 12. 6 8 2 4 10 12
  • 13. • Time for the second and third elements: we have to swap them too: 13 6 2 8 4 10
  • 14. • Now the third and fourth elements, and the second pass is finished, as 8 is already in place: 14 6 2 4 8 10
  • 15. • We start the next pass immediately. Watch the first and the second elements carefully - another swap is needed: 15 2 6 4 8 10
  • 16. • Now 6 needs to go into place. We swap the second and the third elements: 16 2 4 6 8 10
  • 17. 2 4 6 8 10 17
  • 18. • The list is already sorted. We have nothing more to do. This is exactly what we want. • As you can see, the essence of this algorithm is simple: we compare the adjacent elements, and by swapping some of them, we achieve our goal. • Let's code in Python all the actions performed during a single pass through the list, and then we'll consider how many passes we actually need to perform it. We haven't explained this so far, and we'll do that a little later. 18
  • 19. • Sorting a list • How many passes do we need to sort the entire list? • We solve this issue in the following way: we introduce another variable; its task is to observe if any swap has been done during the pass or not; if there is no swap, then the list is already sorted, and nothing more has to be done. We create a variable named swapped, and we assign a value of False to it, to indicate that there are no swaps. Otherwise, it will be assigned True. myList = [8, 10, 6, 2, 4] # list to sort for i in range(len(myList) - 1): # we need (5 - 1) comparisons if myList[i] > myList[i + 1]: # compare adjacent elements myList[i], myList[i + 1] = myList[i + 1], myList[i] # 19
  • 20. • You should be able to read and understand this program without any problems: • myList = [8, 10, 6, 2, 4] # list to sort • swapped = True # it's a little fake - we need it to enter the while loop while swapped: swapped = False # no swaps so far for i in range(len(myList) - 1): if myList[i] > myList[i + 1]: swapped = True # swap occured! myList[i], myList[i + 1] = myList[i + 1], myList[i] print(myList) 20
  • 21. • Run the program and test it. 21
  • 22. The bubble sort - interactive version • In the editor you can see a complete program, enriched by a conversation with the user, and allowing the user to enter and to print elements from the list: The bubble sort - final interactive version. • Python, however, has its own sorting mechanisms. No one needs to write their own sorts, as there is a sufficient number of ready-to-use tools. • We explained this sorting system to you because it's important to learn how to process a list's contents, and to show you how real sorting may work. 22
  • 23. • If you want Python to sort your list, you can do it like this: myList = [8, 10, 6, 2, 4] myList.sort() print(myList) • It is as simple as that. • The snippet's output is as follows: 23
  • 24. myList = [] swapped = True num = int(input("How many elements do you want to sort: ")) for i in range(num): val = float(input("Enter a list element: ")) myList.append(val) while swapped: swapped = False for i in range(len(myList) - 1): if myList[i] > myList[i + 1]: swapped = True myList[i], myList[i + 1] = myList[i + 1], myList[i] print("nSorted:") print(myList) 24
  • 25. Key takeaways 1. You can use the sort() method to sort elements of a list, e.g.: lst = [5, 3, 1, 2, 4] print(lst) lst.sort() print(lst) # outputs: [1, 2, 3, 4, 5] 25
  • 26. • There is also a list method called reverse(), which you can use to reverse the list, e.g.: lst = [5, 3, 1, 2, 4] print(lst) lst.reverse() print(lst) # outputs: [4, 2, 1, 3, 5] 26
  • 27. • Exercise 1 • What is the output of the following snippet? lst = ["D", "F", "A", "Z"] lst.sort() print(lst) 27
  • 28. • Exercise 2 • What is the output of the following snippet? a = 3 b = 1 c = 2 lst = [a, c, b] lst.sort() print(lst) 28
  • 29. • Exercise 3 • What is the output of the following snippet? a = "A" b = "B" c = "C" d = " " lst = [a, b, c, d] lst.reverse() print(lst) 29
  • 30. Operations on lists • The inner life of lists • Now we want to show you one important, and very surprising, feature of lists, which strongly distinguishes them from ordinary variables. • We want you to memorize it - it may affect your future programs, and cause severe problems if forgotten or overlooked. • Take a look at the snippet in the editor. 30
  • 31. • The program: • creates a one-element list named list1; • assigns it to a new list named list2; • changes the only element of list1; • prints out list2. • The surprising part is the fact that the program will output: [2], not [1], which seems to be the obvious solution. • Lists (and many other complex Python entities) are stored in different ways than ordinary (scalar) variables. 31
  • 32. • You could say that: • the name of an ordinary variable is the name of its content; • the name of a list is the name of a memory location where the list is stored. • Read these two lines once more - the difference is essential for understanding what we are going to talk about next. • The assignment: list2 = list1 copies the name of the array, not its contents. In effect, the two names (list1 and list2) identify the same location in the computer memory. Modifying one of them affects the other, and vice versa. • How do you cope with that? 32
  • 33. Operations on lists | slices • Powerful slices • Fortunately, the solution is at your fingertips - its name is the slice. • A slice is an element of Python syntax that allows you to make a brand new copy of a list, or parts of a list. • It actually copies the list's contents, not the list's name. • This is exactly what you need. Take a look at the snippet below: list1 = [1] list2 = list1[:] list1[0] = 2 print(list2) • Its output is [1]. 33
  • 34. • This inconspicuous part of the code described as [:] is able to produce a brand new list. • One of the most general forms of the slice looks as follows: myList[start:end] • As you can see, it resembles indexing, but the colon inside makes a big difference. • A slice of this form makes a new (target) list, taking elements from the source list - the elements of the indices from start to end - 1. • Note: not to end but to end - 1. An element with an index equal to end is the first element which does not take part in the slicing. 34
  • 35. • Using negative values for both start and end is possible (just like in indexing). • Take a look at the snippet: myList = [10, 8, 6, 4, 2] newList = myList[1:3] print(newList) • The newList list will have end - start (3 - 1 = 2) elements - the ones with indices equal to 1 and 2 (but not 3). • The snippet's output is: [8, 6] 35
  • 36. # Copying the whole list list1 = [1] list2 = list1[:] list1[0] = 2 print(list2) # Copying part of the list myList = [10, 8, 6, 4, 2] newList = myList[1:3] print(newList) 36
  • 37. Slices - negative indices • Look at the snippet below: myList[start:end] • To repeat: • start is the index of the first element included in the slice. • end is the index of the first element not included in the slice. 37
  • 38. • This is how negative indices work with the slice: myList = [10, 8, 6, 4, 2] newList = myList[1:-1] print(newList) • The snippet's output is: [8, 6, 4]. • If the start specifies an element lying further than the one described by the end (from the list's beginning point of view), the slice will be empty: myList = [10, 8, 6, 4, 2] newList = myList[-1:1] print(newList) • The snippet's output is: []. 38
  • 39. • Slices: continued • If you omit the start in your slice, it is assumed that you want to get a slice beginning at the element with index 0. • In other words, the slice of this form: myList[:end] • is a more compact equivalent of: myList[0:end] • Look at the snippet below: myList = [10, 8, 6, 4, 2] newList = myList[:3] print(newList) • This is why its output is: [10, 8, 6]. 39
  • 40. • Similarly, if you omit the end in your slice, it is assumed that you want the slice to end at the element with the index len(myList). • In other words, the slice of this form: myList[start:] • is a more compact equivalent of: myList[start:len(myList)] • Look at the following snippet: myList = [10, 8, 6, 4, 2] newList = myList[3:] print(newList) • Its output is therefore: [4, 2]. 40
  • 41. Slices: continued • As we've said before, omitting both start and end makes a copy of the whole list: myList = [10, 8, 6, 4, 2] newList = myList[:] print(newList) • The snippet's output is: [10, 8, 6, 4, 2]. 41
  • 42. • The previously described del instruction is able to delete more than just a list's element at once - it can delete slices too: myList = [10, 8, 6, 4, 2] del myList[1:3] print(myList) • Note: in this case, the slice doesn't produce any new list! • The snippet's output is: [10, 4, 2]. 42
  • 43. • Deleting all the elements at once is possible too: myList = [10, 8, 6, 4, 2] del myList[:] print(myList) • The list becomes empty, and the output is: []. • Removing the slice from the code changes its meaning dramatically. • Take a look: 43
  • 44. myList = [10, 8, 6, 4, 2] del myList print(myList) • The del instruction will delete the list itself, not its content. • The print() function invocation from the last line of the code will then cause a runtime error. 44
  • 45. The in and not in operators • Python offers two very powerful operators, able to look through the list in order to check whether a specific value is stored inside the list or not. • These operators are: • elem in myList • elem not in myList • The first of them (in) checks if a given element (its left argument) is currently stored somewhere inside the list (the right argument) - the operator returns True in this case 45
  • 46. • The second (not in) checks if a given element (its left argument) is absent in a list - the operator returns True in this case. • Look at the code in the editor. The snippet shows both operators in action. Can you guess its output? Run the program to check if you were right 46
  • 47. myList = [0, 3, 12, 8, 2] print(5 in myList) print(5 not in myList) print(12 in myList) 47
  • 49. 3rd floor Prabhakar Plaza, Station Road, Kolhapur. Pin -416416 Mobile Number-9225804090