TGViewer
PyCoders (پایتون) PyCoders (پایتون) @python_rd · 4.1K subscribers
Post #2096 622
🔆 Insertion Sort

🔹 الگوریتم از عنصر دوم شروع می‌کنه، هر بار یک عدد رو به‌عنوان key برمی‌داره، اعداد بزرگ‌ترِ بخش مرتب‌شده رو یک خانه به راست شیفت می‌ده و key رو توی جای درستش قرار می‌ده. این کار تا آخر آرایه ادامه پیدا می‌کنه تا کل لیست مرتب بشه.

کد:

def insertion_sort(a):
n = len(a)
for i in range(1, n):
key = a[i]
j = i - 1

while j > 0 and a[j - 1] > key:
a[j] = a[j - 1]
j -= 1

a[j] = key

🐍 @python_rd
  • ❤ 3
More from @python_rd
  1. Oct 15, 2026🌐 خروجی این کد پایتون چیست؟ x = [1, 2, 3] y = x x += [4, 5] print(x) print(y) 🔹 گزینه‌ها…
  2. Oct 15, 2026🔆 آیا enumerate() بهتر از index-based loop است؟ 🔹 خیلی‌ها هنوز این شکلی می‌نویسند: items…
  3. Oct 15, 2026🔆 یک تله عجیب در پایتون: bool در واقع زیرکلاس int است 🔹 خیلی‌ها می‌دانند True == 1 و Fal…
  4. Oct 13, 2026کوییز پایتون 🔆 خروجی اجرای کد زیر چیست؟ t = (1, 2, [3, 4]) try: t[2] += [5] except TypeEr…
  5. Oct 13, 2026🔆 راز مکانیزم پیاده‌سازی و تغییر جهت NaN در الگوریتم مرتب‌سازی پایتون (Timsort) 🔹 باگ ری…
  6. Oct 13, 2026🔆 حذف تاریخی GIL از پایتون (Free-threaded Python) 🔹 یک رویداد تاریخی: حذف قفل مفسر (GIL)…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →