Bubble sort · ترتيب الفقاعات
Sorting puts items in order
- Sorting rearranges a list into order — smallest to largest, say.
- Bubble sort is the standard method named in the syllabus.
- It compares neighbours and swaps any that are the wrong way round.
الترتيب يضع العناصر في ترتيب
- الترتيب يعيد ترتيب قائمة ما — من الأصغر إلى الأكبر، على سبيل المثال.
- ترتيب الفقاعات هو الطريقة القياسية المذكورة في المنهج.
- يقارن الأجانب ويقوم بتبديل أي منها إذا كانا في الاتجاه الخاطئ.
Swapping two values
- To sort, you must swap two items.
- Python can swap in one line:
a, b = b, a.
تبديل قيمتين
- للترتيب، يجب عليك تبديل عنصرين.
- بايثون يمكنه التبديل في سطر واحد:
a, b = b, a.
a = 5
b = 9
a, b = b, a
print(a, b) # 9 5
Bubble sort: compare neighbours
- Walk the list comparing each pair of neighbours.
- If the left one is bigger, swap them — the biggest "bubbles" to the end.
- Repeat the passes until the list is in order.
ترتيب الفقاعات: مقارنة الجيران
- امشِ عبر القائمة مقارناً كل زوج من الجيران.
- إذا كان العنصر الأيسر أكبر، قم بتبديلهما — حيث "يطفو" العنصر الأكبر إلى النهاية.
- كرر المرات حتى تصبح القائمة مرتبة.
nums = [5, 1, 4, 2]
for i in range(len(nums) - 1):
for j in range(len(nums) - 1 - i):
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
print(nums) # [1, 2, 4, 5]
In the exam: bubble sort
- The exam writes bubble sort in pseudocode with nested loops and a swap.
في الامتحان: ترتيب الفقاعات
- يكتب الامتحان ترتيب الفقاعات باستخدام الكود الوهمي مع حلقات متداخلة وتبديل.
FOR I ← 1 TO N - 1
FOR J ← 1 TO N - 1
IF List[J] > List[J + 1]
THEN
Temp ← List[J]
List[J] ← List[J + 1]
List[J + 1] ← Temp
ENDIF
NEXT J
NEXT I
Watch bubble sort work · شاهد عمل فرز الفقاعات
Compare neighbours and swap any out of order; the biggest bubbles to the end. · قارن الجيران وقم بتبديل أيّ غير مرتب؛ تتجه أكبر الفقاعات نحو النهاية.
Common mistakes
- Each pass moves the largest remaining value to the end.
- If a whole pass makes no swaps, the list is already sorted.
أخطاء شائعة
- كل مرحلة تحرك أكبر قيمة متبقية إلى النهاية.
- إذا لم يتم إجراء أي تبديلات خلال مرحلة كاملة، فإن القائمة مرتبة بالفعل.
Now you try
- Swap two values, then sort a whole list.
- Press Check answer to test your code.
الآن جرب بنفسك
- قم بتبديل قيمتين، ثم رتّب القائمة كاملة.
- اضغط على تحقق من الإجابة لاختبار الكود الخاص بك.
Swap the values of a and b, so that afterwards a is 9 and b is 5. · بدّل قيمتي a و b، بحيث تصبح afterward a تساوي 9 و b تساوي 5.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Sort the list nums into ascending order (smallest first) using a bubble sort. The result should be [1, 2, 4, 5]. · رتّب القائمة nums إلى ترتيب تصاعدي (الأصغر أولًا) باستخدام فرز الفقاعات. يجب أن يكون الناتج [1, 2, 4, 5].
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write is_sorted(nums) that returns True if the list is already in ascending order, otherwise False. · اكتب is_sorted(nums) تُرجع True إذا كانت القائمة مرتبة بالفعل تصاعديًا، وإلا ترجع False.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.