Python: Bubble sort - w3resource

文章推薦指數: 80 %
投票人數:10人

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 Dosomethingwiththe Dosomethingwiththe Dosomethingwiththe Dosomethingwiththe   Loading... Exercises:WeeklyTop12MostPopularTopics SQLExercises,Practice,Solution-SUBQUERIES CProgrammingExercises,Practice,Solution:ForLoop PythonExercises,Practice,Solution PythonDataType:List-Exercises,Practice,Solution C++Basic:Exercises,Practice,Solution SQLExercises,Practice,Solution-exercisesonEmployeeDatabase SQLExercises,Practice,Solution-exercisesonMovieDatabase SQLExercises,Practice,Solution-exercisesonSoccerDatabase CProgrammingExercises,Practice,Solution:Recursion Python:ArrayExercises,Practice,Solution C#SharpBasicAlgorithm:Exercises,Practice,Solution C++programmingExercisespracticewithsolution



請為這篇文章評分?