Bubble Sort (With Code in Python/C++/Java/C) - Programiz
文章推薦指數: 80 %
In this tutorial, you will learn about the bubble sort algorithm and its implementation in Python, Java, C, and C++. Bubble sort is a sorting algorithm that ...
CourseIndex
ExploreProgramiz
Python
JavaScript
SQL
C
C++
Java
Kotlin
Swift
C#
DSA
PopularTutorials
QuicksortAlgorithm
MergeSortAlgorithm
LinkedListDataStructure
HashTableDataStructure
DynamicProgramming
StartLearningDSA
LearningPaths
Challenges
LearnPythonInteractively
TryforFree
Courses
BecomeaPythonMaster
BecomeaCMaster
BecomeaJavaMaster
ViewallCourses
Python
JavaScript
SQL
C
C++
Java
Kotlin
Swift
C#
DSA
PopularTutorials
QuicksortAlgorithm
MergeSortAlgorithm
LinkedListDataStructure
HashTableDataStructure
DynamicProgramming
StartLearningDSA
AllDSATutorials
Python
JavaScript
C
C++
Java
Kotlin
PopularExamples
Addtwonumbers
Checkprimenumber
Findthefactorialofanumber
PrinttheFibonaccisequence
Checkleapyear
AllPythonExamples
DSAIntroduction
Whatisanalgorithm?
DataStructureandTypes
WhylearnDSA?
AsymptoticNotations
MasterTheorem
DivideandConquerAlgorithm
DataStructures(I)
Stack
Queue
TypesofQueue
CircularQueue
PriorityQueue
Deque
DataStructures(II)
LinkedList
LinkedListOperations
TypesofLinkedList
HashTable
HeapDataStructure
FibonacciHeap
DecreaseKeyandDeleteNodeOperationsonaFibonacciHeap
TreebasedDSA(I)
TreeDataStructure
TreeTraversal
BinaryTree
FullBinaryTree
PerfectBinaryTree
CompleteBinaryTree
BalancedBinaryTree
BinarySearchTree
AVLTree
TreebasedDSA(II)
BTree
InsertioninaB-tree
DeletionfromaB-tree
B+Tree
InsertiononaB+Tree
DeletionfromaB+Tree
Red-BlackTree
Red-BlackTreeInsertion
Red-BlackTreeDeletion
GraphbasedDSA
GraphDataStructure
SpanningTree
StronglyConnectedComponents
AdjacencyMatrix
AdjacencyList
DFSAlgorithm
Breadth-firstSearch
BellmanFord'sAlgorithm
SortingandSearchingAlgorithms
BubbleSort
SelectionSort
InsertionSort
MergeSort
Quicksort
CountingSort
RadixSort
BucketSort
HeapSort
ShellSort
LinearSearch
BinarySearch
GreedyAlgorithms
GreedyAlgorithm
Ford-FulkersonAlgorithm
Dijkstra'sAlgorithm
Kruskal'sAlgorithm
Prim'sAlgorithm
HuffmanCoding
DynamicProgramming
DynamicProgramming
Floyd-WarshallAlgorithm
LongestCommonSequence
OtherAlgorithms
BacktrackingAlgorithm
Rabin-KarpAlgorithm
RelatedTopics
SelectionSortAlgorithm
InsertionSortAlgorithm
ShellSortAlgorithm
CountingSortAlgorithm
RadixSortAlgorithm
QuicksortAlgorithm
BubbleSort
Inthistutorial,youwilllearnaboutthebubblesortalgorithmanditsimplementationinPython,Java,C,andC++.
Bubblesortisasortingalgorithmthatcomparestwoadjacentelementsandswapsthemuntiltheyarenotintheintendedorder.
Justlikethemovementofairbubblesinthewaterthatriseuptothesurface,eachelementofthearraymovetotheendineachiteration.Therefore,itiscalledabubblesort.
WorkingofBubbleSort
Supposewearetryingtosorttheelementsinascendingorder.
1.FirstIteration(CompareandSwap)
Startingfromthefirstindex,comparethefirstandthesecondelements.
Ifthefirstelementisgreaterthanthesecondelement,theyareswapped.
Now,comparethesecondandthethirdelements.Swapthemiftheyarenotinorder.
Theaboveprocessgoesonuntilthelastelement.
ComparetheAdjacentElements
2.RemainingIteration
Thesameprocessgoesonfortheremainingiterations.
Aftereachiteration,thelargestelementamongtheunsortedelementsisplacedattheend.
PutthelargestelementattheendIneachiteration,thecomparisontakesplaceuptothelastunsortedelement.
ComparetheadjacentelementsThearrayissortedwhenalltheunsortedelementsareplacedattheircorrectpositions.
ThearrayissortedifallelementsarekeptintherightorderBubbleSortAlgorithm
bubbleSort(array)
forirightElement
swapleftElementandrightElement
endbubbleSort
BubbleSortCodeinPython,JavaandC/C++
Python
Java
C
C++
#BubblesortinPython
defbubbleSort(array):
#looptoaccesseacharrayelement
foriinrange(len(array)):
#looptocomparearrayelements
forjinrange(0,len(array)-i-1):
#comparetwoadjacentelements
#change>to
延伸文章資訊
- 1Bubble Sort Algorithm - GeeksforGeeks
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elem...
- 2[ C語言生活記事] Sorting algorithm - (1) Bubble sort | 阿鐵的碼 ...
排序演算法(1) - Bubble sort用兩個迴圈來實現,程式複雜度O( n^2 )
- 3【Day 23】Bubble sort 範例 - iT 邦幫忙
C 語言的簡單入門系列第23 篇. hebe880123. 9 個月前‧ 540 瀏覽. 1. 不知道大家對於Bubble sort 的程式有沒有甚麼問題呢? 今天,我們就來討論一下程式碼!
- 4C/C++ bubble sort 泡沫排序法
本篇ShengYu 介紹C/C++ 中最簡單經典的泡沫排序法bubble sort,並且由C/C++ 來實作泡沫排序法bubble sort。 如果不想自己刻一個排序法可以使用現成C ...
- 5C 語言實作氣泡排序法(Bubble Sort) - Justin
C 語言實作氣泡排序法(Bubble Sort). 排序在演算法裡面算是一個很基本的問題,雖然現在各種強大的程式語言都會自帶排序演算法的函 ...