How to implement bubble sort algorithm using Python?

WBOY
Release: 2023-09-21 11:03:30
Original
1306 people have browsed it

How to implement bubble sort algorithm using Python?

How to implement the bubble sort algorithm using Python?

Bubble sorting algorithm is a simple but effective sorting algorithm. Its idea is to continuously compare two adjacent elements. If their order is incorrect, swap their positions until the entire sequence is Until sorted. The following will demonstrate how to use Python to implement the bubble sort algorithm through specific code examples.

def bubble_sort(arr): n = len(arr) # 外层循环控制比较的轮数 for i in range(n - 1): # 内层循环控制每轮的比较次数 for j in range(n - i - 1): # 如果相邻的两个元素顺序不正确,则交换它们的位置 if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr # 测试示例 arr = [64, 34, 25, 12, 22, 11, 90] sorted_arr = bubble_sort(arr) print("排序后的数组:", sorted_arr)
Copy after login

In the above code, we define a function namedbubble_sort, which accepts a list as a parameter and returns the sorted list. The core part of bubble sort is a two-level nested loop. The outer loop controls the number of rounds of comparison. Each round of comparison moves the largest element in the unsorted part to the end. The inner loop controls the number of comparisons per round, by comparing two adjacent elements and exchanging their positions if they are not in the correct order. The number of loops and exchanges increases with the size of the sequence to be sorted, so the time complexity of bubble sort is O(n^2).

In the above code, we use a set of test examples to verify the correctness of the sorting algorithm. In this example, we use a list of integers with 7 elements and pass it to thebubble_sortfunction. After running the program, the console will output the sorted list. For the given test example, the output should be[11, 12, 22, 25, 34, 64, 90].

Beyond this simple example, the bubble sort algorithm can be applied to any type of comparable elements. You can use bubble sort to sort integers, floating point numbers, strings, etc. At the same time, we can also optimize the sorting algorithm according to our own needs, such as adding a flag to determine whether the sorting has been completed, which can reduce the number of unnecessary comparisons.

Summary:
Bubble sorting algorithm is a simple but effective sorting algorithm. By comparing adjacent elements and exchanging positions, the largest element is moved to the end step by step, thereby achieving the purpose of sorting. . Through the bubble sort algorithm example written in Python, we can clearly understand the idea and implementation of the algorithm. Whether you are a beginner or an experienced developer, you can improve your understanding and application of algorithms and programming by understanding and practicing the bubble sort algorithm.

The above is the detailed content of How to implement bubble sort algorithm using Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!