起泡排序算法
When working with large databases, it is necessary to add the functionality to search for values or find the highest or lowest values from a range of items. The Bubble Sort Algorithm is an algorithm that facilitates just that.
当使用大型数据库时,有必要添加功能以搜索值或从一系列项目中找到最高或最低值。 冒泡排序算法是一种简化算法。
Bubble Sort Algorithm is a sorting technique that assists with sorting the elements in a simple and efficient manner. Using bubble sort, elements can be sorted in ascending/descending order.
气泡排序算法是一种排序技术,可帮助以简单有效的方式对元素进行排序。 使用冒泡排序,可以按升序/降序对元素进行排序。
Bubble sort makes a comparison between the adjacent elements and swaps the elements if they are not in a defined order (either ascending or descending).
冒泡排序会在相邻元素之间进行比较,并在相邻元素未按定义顺序(升序或降序)时进行交换。
冒泡排序算法 (The Bubble Sort Algorithm)
- Initiate with the first element i.e. position = 0, and compare it with the next element of that particular array. 从第一个元素(即position = 0)开始,然后将其与该特定数组的下一个元素进行比较。
- If the element to be compared is greater than the next element, then we need to swap the elements. 如果要比较的元素大于下一个元素,则需要交换元素。
- If the element to be compared is smaller than the next element, move to the next position to check for another element. 如果要比较的元素小于下一个元素,请移至下一个位置以检查另一个元素。
- Repeat Step 1 – 3 until all the elements get sorted in an ascending or descending fashion. 重复步骤1-3 ,直到所有元素都以升序或降序排序。
Logic:
逻辑:
bubble_sort(input_array)
for x <- 1 to index_of_last_element-1
if left_element > right_element
swap left_element and right_element
end bubble_sort
冒泡排序算法的工作 (Working of Bubble Sort Algorithm)
Let’s understand how the Bubble Sort algorithm works with the help of an example.
让我们借助示例了解Bubble Sort算法的工作方式。
Our task is to sor