jackfrued / jackfrued/Python-100-Days

Python Day 16 冒泡排序

Open
#589 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
187k
Forks
55.8k
PR merge metrics
No merged PRs in 30d

Description

冒泡排序的代码中一处错误,就是在减少时间复杂度的循环中,应该不是从i还是迭代,而是从0开始迭代,从i开始迭代的话,指针会一直往中间位置移动,最终排序的结果可能会导致前面一部分并没有排序成功
def bubble_sort(items, comp=lambda x, y: x > y): """冒泡排序""" items = items[:] for i in range(len(items) - 1): swapped = False for j in range(i, len(items) - 1 - i):《========== if comp(items[j], items[j + 1]): items[j], items[j + 1] = items[j + 1], items[j] swapped = True if not swapped: break return items bubble_sort([48, 3, 5, 9, 1, 2])

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the bubble_sort snippet in the Python Day 16 material and inspect the inner loop marked in the issue. Run the shown bubble_sort([48, 3, 5, 9, 1, 2]) example and verify that the complete returned list is sorted, including its leading elements.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.