Python: Bubble sort - w3resource
文章推薦指數: 80 %
Note : According to Wikipedia "Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through ...
home
FrontEnd
HTML
CSS
JavaScript
HTML5
Schema.org
php.js
TwitterBootstrap
ResponsiveWebDesigntutorial
ZurbFoundation3tutorials
PureCSS
HTML5Canvas
JavaScriptCourse
Icon
Angular
Vue
Jest
Mocha
NPM
Yarn
BackEnd
PHP
Python
Java
Node.js
Ruby
Cprogramming
PHPComposer
Laravel
PHPUnit
Database
SQL(2003standardofANSI)
MySQL
PostgreSQL
SQLite
NoSQL
MongoDB
Oracle
Redis
ApolloGraphQL
API
GooglePlusAPI
YoutubeAPI
GoogleMapsAPI
FlickrAPI
Last.fmAPI
TwitterRESTAPI
DataInterchnage
XML
JSON
Ajax
Exercises
HTMLCSSExercises
JavaScriptExercises
jQueryExercises
jQuery-UIExercises
CoffeeScriptExercises
PHPExercises
PythonExercises
CProgrammingExercises
C#SharpExercises
JavaExercises
SQLExercises
OracleExercises
MySQLExercises
SQLiteExercises
PostgreSQLExercises
MongoDBExercises
TwitterBootstrapExamples
Others
ExcelTutorials
Usefultools
GoogleDocsFormsTemplates
GoogleDocsSlidePresentations
NumberConversions
LinuxTutorials
Quizzes
Articles
Python:Bubblesort
LastupdateonMay28202212:57:29(UTC/GMT+8hours)
PythonSearchandSorting:Exercise-4withSolution
WriteaPythonprogramtosortalistofelementsusingthebubblesortalgorithm.
Note:AccordingtoWikipedia"Bubblesort,sometimesreferredtoassinkingsort,isasimplesortingalgorithmthatrepeatedlystepsthroughthelisttobesorted,compareseachpairofadjacentitemsandswapsthemiftheyareinthewrongorder.Thepassthroughthelistisrepeateduntilnoswapsareneeded,whichindicatesthatthelistissorted.Thealgorithm,whichisacomparisonsort,isnamedforthewaysmallerelements"bubble"tothetopofthelist.Althoughthealgorithmissimple,itistooslowandimpracticalformostproblemsevenwhencomparedtoinsertionsort.Itcanbepracticaliftheinputisusuallyinsortorderbutmayoccasionallyhavesomeout-of-orderelementsnearlyinposition.
Stepbysteppictorialpresentation:
SampleSolution:-
PythonCode:
defbubbleSort(nlist):
forpassnuminrange(len(nlist)-1,0,-1):
foriinrange(passnum):
ifnlist[i]>nlist[i+1]:
temp=nlist[i]
nlist[i]=nlist[i+1]
nlist[i+1]=temp
nlist=[14,46,43,27,57,41,45,21,70]
bubbleSort(nlist)
print(nlist)
SampleOutput:
[14,21,27,41,43,45,46,57,70]
Flowchart:
PythonCodeEditor:
ContributeyourcodeandcommentsthroughDisqus.
Previous:WriteaPythonprogramforbinarysearchforanorderedlist.
Next:WriteaPythonprogramtosortalistofelementsusingtheselectionsortalgorithm.
Whatisthedifficultylevelofthisexercise?
Easy
Medium
Hard
TestyourProgrammingskillswithw3resource'squiz.
Python:TipsoftheDay
CheckifaSequenceIsEmpty:
>>>empty_list=[(),'',[],{},set()]
>>>foriteminempty_list:
... ifnotitem:
... print(f'Dosomethingwiththe{type(item)}')
...
Dosomethingwiththe
延伸文章資訊
- 1[演算法] 泡沫排序(Bubble Sort) - iT 邦幫忙
- 2Python 冒泡排序 - 菜鸟教程
这个算法的名字由来是因为越小的元素会经由交换慢慢"浮"到数列的顶端。 实例. def bubbleSort(arr): n ...
- 3Python Program for Bubble Sort - GeeksforGeeks
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elem...
- 4Python bubble sort 泡沫排序法
本篇ShengYu 介紹排序法中最簡單經典的泡沫排序法bubble sort,並且由Python 來實作泡沫排序法bubble sort。 泡沫排序法bubble sort 基本原理泡沫排序 ...
- 5Bubble Sort in Python - Stack Abuse
In the most inefficient approach, Bubble Sort goes through n-1 iterations, looking at n-1 pairs o...