气泡图 网页可视化
What you need:
您需要什么:
1) Unsorted array
1)未排序的数组
2) for loop i - number of loop is based on the number of elements in the array. Each loop of i would reset loop of j to index zero.
2)for循环i-循环数基于数组中元素的数量。 i的每个循环会将j的循环重置为索引零。
3) for loop j - number of loop is based on number of loop i minus - 1 for every loop of j. Why? we are already sure that the last element of each loop is sorted and doesn't needed to be compared in the next loop (hence -1).
3)对于循环j-循环数基于循环数i减去j的每个循环-1。 为什么? 我们已经确定每个循环的最后一个元素已排序,并且不需要在下一个循环中进行比较(因此为-1)。
4) a variable to switch number. you don't need this in Python.
4)切换数字的变量。 您在Python中不需要这个。
Visualization:
可视化:
If you are wondering how i made this, i used Numbers App in my MacBook.
如果您想知道我是怎么做到的,我在MacBook中使用了Numbers App。
用于实现冒泡排序的Python程序 (Python program for implementation of Bubble Sort)
def bubbleSort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
上面要测试的驱动程序代码 (Driver code to test above)
arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
print ("Sorted array is:")
for i in range(len(arr)):
print ("%d" %arr[i]),
Source for code: https://www.geeksforgeeks.org/bubble-sort/
代码源: https : //www.geeksforgeeks.org/bubble-sort/
翻译自: https://www.freecodecamp.org/news/cjn-bubble-sort-visualized/
气泡图 网页可视化