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
延伸文章資訊
- 1C/C++ bubble sort 泡沫排序法
本篇ShengYu 介紹C/C++ 中最簡單經典的泡沫排序法bubble sort,並且由C/C++ 來實作泡沫排序法bubble sort。 如果不想自己刻一個排序法可以使用現成C ...
- 2[ C語言生活記事] Sorting algorithm - (1) Bubble sort | 阿鐵的碼 ...
排序演算法(1) - Bubble sort用兩個迴圈來實現,程式複雜度O( n^2 )
- 3C 語言排序演算法實作整理:泡沫排序、快速排序等 - GT Wang
若要對一連串的元素(陣列)做排序的話,有很多種實作方式,常見的排序方法有:泡沫排序法(bubble sort)、插入排序法(insertion sort)、快速排序 ...
- 4[C++] 氣泡排序法(Bubble sort)
[C++] 氣泡排序法(Bubble sort). 簡單記錄一下自己的理解. 氣泡排序的意思,wiki 裡面是這麼說明: 又稱為泡沫排序,是一種簡單的排序演算法。
- 5C 語言實作氣泡排序法(Bubble Sort) - Justin
C 語言實作氣泡排序法(Bubble Sort). 排序在演算法裡面算是一個很基本的問題,雖然現在各種強大的程式語言都會自帶排序演算法的函 ...