-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python 实现排序算法 #6004
Python 实现排序算法 #6004
Conversation
Python 实现排序算法
校对认领 |
@JalanJiang 好的呢 🍺 |
校对认领 |
@csming1995 妥妥哒 🍻 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fanyijihua @leviding 校对完成
|
||
Sometimes data we store or retrieve in an application can have little or no order. We may have to rearrange the data to correctly process it or efficiently use it. Over the years, computer scientists have created many sorting algorithms to organize data. | ||
有时,我们在应用程序中存储或检索的数据有可能没有顺序。我们可能需要重新排列数据以正确处理或有效地使用它。多年来,计算机科学家创造了许多排序算法来组织数据。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『有时,我们在应用程序中存储或检索的数据有可能没有顺序。』=>『有时,我们在应用程序中存储或检索的数据有可能是乱序的。』
|
||
In this article we'll have a look at popular sorting algorithms, understand how they work and code them in Python. We'll also compare how quickly they sort items in a list. | ||
在本文中,我们将了解一些流行的排序算法,了解它们是如何工作的,并用 Python 对来实现它们。我们还将比较它们对列表排序的速度。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『并用 Python 对来实现它们』=>『并用 Python 来实现它们』
|
||
```python | ||
def bubble_sort(nums): | ||
# We set swapped to True so the loop looks runs at least once | ||
# 我们设置标志值 swapped 值为 True,以便循环能够执行至少一次 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
```python | ||
def bubble_sort(nums): | ||
# We set swapped to True so the loop looks runs at least once | ||
# 我们设置标志值 swapped 值为 True,以便循环能够执行至少一次 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『我们设置标志值 swapped 值为 True』=>『我们将标志值 swapped 设为 True』
nums[i], nums[i + 1] = nums[i + 1], nums[i] | ||
# Set the flag to True so we'll loop again | ||
# 把标志值设为 true 以便我们能再次循环 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『把标志值设为 true 以便我们能再次循环』=>『把标志值设为 True 以便我们能再次循环』
|
||
Sorting algorithms gives us many ways to order our data. We looked at 6 different algorithms - Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Heap Sort, Quick Sort - and their implementations in Python. | ||
排序算法为我们提供了许多排序数据的方法。我们看6到不同的算法——冒泡排序、选择排序、插入排序、归并排序、堆排序、快速排序 —— 在 Python 中的实现。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『在 Python 中的实现。』=>『以及它们在 Python 中的实现。』
|
||
The amount of comparison and swaps the algorithm performs along with the environment the code runs are key determinants of performance. In real Python applications, it's recommended we stick with the built in Python sort functions for their flexibility on the input and speed. | ||
算法执行的比较和交换量以及代码运行的环境是性能的关键决定因素。在实际的 Python 应用程序中,建议我们坚持使用内置的 Python 排序函数,因为它们在输入和速度上具有灵活性。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『算法执行的比较和交换量以及代码运行的环境是性能的关键决定因素。』=>『算法执行的比较和交换量以及代码运行的环境是决定性能的关键因素。』
|
||
Every time we select the smaller value at the beginning of a half, we move the index of which item needs to be compared by one. | ||
每次我们在半段的开头选择较小的值时,我们都会将需要比较的项的索引移动一个。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『我们都会将需要比较的项的索引移动一个』=>『我们都会移动需要比较的项目』
|
||
You would get different values if you set up the test yourself, but the patterns observed should be the same or similar. Bubble Sort is the slowest the worst performer of all the algorithms. While it useful as an introduction to sorting and algorithms, it's not fit for practical use. | ||
如果你自己设置测试,你会得到不同的值,但是观察到的模式应该是相同或相似的。冒泡排序是所有算法中执行速度最慢、最差的。虽然它作为排序和算法的介绍很有用,但不适合实际使用。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『但是观察到的模式应该是相同或相似的』=>『但是观察到的性能模型应该是相同或相似的』
|
||
You would get different values if you set up the test yourself, but the patterns observed should be the same or similar. Bubble Sort is the slowest the worst performer of all the algorithms. While it useful as an introduction to sorting and algorithms, it's not fit for practical use. | ||
如果你自己设置测试,你会得到不同的值,但是观察到的模式应该是相同或相似的。冒泡排序是所有算法中执行速度最慢、最差的。虽然它作为排序和算法的介绍很有用,但不适合实际使用。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『冒泡排序是所有算法中执行速度最慢、最差的。』=>『冒泡排序是所有算法中执行速度最慢、表现最差的。』
|
||
Be mindful of the environment when choosing your sorting algorithm, as it will affect performance. | ||
要注意环境的选择排序算法的时候,因为它会影响性能。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fireairforce 像这样的翻译,本文存在很多处。明显的翻译不准确,意思表达不到位,语句不通顺。总结本译文存在以下问题:
- 标点符号不符合翻译计划要求
- 存在多处专业名词翻译错误
- 意思表达不到位,语句不通顺
译者之前的翻译质量不错,那此次译者是否不熟悉 Python 和相关算法,是否翻译太匆忙未认真检查,还是有其他什么原因?
@fireairforce 请检查全文,根据校对意见认真修改,并且修改后自行润色优化。之后第二位校对再进行校对。 |
emmm,很抱歉,最近确实是期末考试然后加上刚入职要找房,确实是没有花太大精力去自习检查文章的质量,真的很抱歉。。我后续会好好润色文章并检查文章句子的。其实我确实是不了解python,但是我懂算法,我后续一定会好好fix的,再次道歉。 |
本文可以多给点时间,先忙自己的事,忙完好好检查下吧。 |
@leviding 已经重新 review 并且修改了 |
@csming1995 可以继续校对啦 |
收到了~ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fireairforce @leviding 校对完成
|
||
In this article we'll have a look at popular sorting algorithms, understand how they work and code them in Python. We'll also compare how quickly they sort items in a list. | ||
在本文中,我们将了解一些流行的排序算法,了解它们是如何工作的,并用 Python 来实现它们。我们还将会比较它们对列表中的元素的排序速度。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『我们还将会比较它们对列表中的元素的排序速度。』->『我们还将会比较它们对列表中的元素排序的速度。』
⬆️这样是否会更通顺一点
|
||
This simple sorting algorithm iterates over a list, comparing elements in pairs and swapping them until the larger elements "bubble up" to the end of the list, and the smaller elements stay at the "bottom". | ||
这个简单的排序算法会通过迭代列表来成对比较列表中的元素,并且交换它们,直到较大的元素“冒泡”到列表的末尾,较小的元素保持在“底部”。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『这个简单的排序算法会通过迭代列表来成对比较列表中的元素』->『这个简单的排序算法会通过迭代列表成对的比较列表中的元素』
这边第一眼看过去有点难以理解“来成对比较...”的意思,建议加一个“的”会比较清晰
|
||
How would we know that we're finished sorting? If the items were in order then we would not have to swap items. So, whenever we swap values we set a flag to `True` to repeat sorting process. If no swaps occurred, the flag would remain `False` and the algorithm would stop. | ||
那我们怎么知道已经完成了排序?如果元素是有序的,那我们就不必继续交换。因此,每当交换值时,我们会设置一个标志值为 `True` 来重复排序过程。如果没有发生交换,标志值将保持为 `False`,算法将停止。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『我们会设置一个标志值为 True
来重复排序过程。』->『我们将一个标志值设置为 True
以重复排序过程。』
|
||
With the optimization, we can implement the bubble sort in Python as follows: | ||
通过优化,我们可以通过以下的 Python 代码来实现冒泡排序: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『通过优化』->『优化之后』
|
||
In the worst case scenario (when the list is in reverse order), this algorithm would have to swap every single item of the array. Our `swapped` flag would be set to `True` on every iteration. Therefore, if we have *n* elements in our list, we would have *n* iterations per item - thus Bubble Sort's time complexity is O(n^2). | ||
在最坏的情况下(当列表处于相反的顺序时),该算法必须交换数组的每个项。每次迭代的时候,标志值 `swapped` 都会被设置为 `True`。因此,如果我们在列表中有 *n* 个元素,我们将对每个元素进行 *n* 次迭代,因此冒泡排序的时间复杂度为 O(n^2)。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『我们将对每个元素进行 n 次迭代』->『我们将对每个元素迭代 n 次』
|
||
```python | ||
def merge(left_list, right_list): | ||
sorted_list = [] | ||
left_list_index = right_list_index = 0 | ||
|
||
# We use the list lengths often, so its handy to make variables | ||
# 我们经常使用列表长度,因此可以方便地生成变量 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同意@JalanJiang的看法,原译文不够通顺易懂。附议。
else: | ||
sorted_list.append(right_list[right_list_index]) | ||
right_list_index += 1 | ||
|
||
# If we've reached the end of the of the left list, add the elements | ||
# from the right list | ||
# 如果已到达左列表的末尾,请添加右列表中的元素 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『请添加右列表中的元素』->『则添加右列表中的元素』
elif left_list_index == left_list_length: | ||
sorted_list.append(right_list[right_list_index]) | ||
right_list_index += 1 | ||
# If we've reached the end of the of the right list, add the elements | ||
# from the left list | ||
# 如果已到达右列表的末尾,请添加左列表中的元素 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『请添加左列表中的元素』->『则添加左列表中的元素』
|
||
This divide and conquer algorithm is the most often used sorting algorithm covered in this article. When configured correctly, it's extremely efficient and does not require the extra space Merge Sort uses. We partition the list around a pivot element, sorting values around the pivot. | ||
这种分而治之的算法是本文中最常用的排序算法。如果使用正确,它的效率很高,并且不需要像归并排序一样使用额外的空间。我们围绕一个基准值对列表进行分区,并对基准值周围的元素进行排序。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
『如果使用正确,它的效率很高』->『如果合理地使用,那么它将具有很高的效率』
```python | ||
# There are different ways to do a Quick Sort partition, this implements the | ||
# Hoare partition scheme. Tony Hoare also created the Quick Sort algorithm. | ||
# 快速排序分区有不同的方法,下面实现了 Hoare 的分区方案。Tony Hoare 还创建了快速排序算法。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
302行存在排版问题。行数不对齐。
@fireairforce 有空的时候可以修改了哈 |
那个排版问题是这样的,因为原作者的代码注释有的地方英文是分为了两行在写,如果我按照原文翻译的话,会导致句子不通顺,因而把有的注释直接翻译之后放在了一行 |
修改完成 @leviding |
@fireairforce 已经 merge 啦~ 快快麻溜发布到掘金然后给我发下链接,方便及时添加积分哟。 掘金翻译计划有自己的知乎专栏,你也可以投稿哈,推荐使用一个好用的插件。 |
|
希望以后还有时间来继续翻译QAQ,不过掘金社区肯定还是会经常逛的 |
哈哈哈,好滴 😆 |
Python 实现排序算法
译文翻译完成,resolve #5946