Sorting: bubble, selection, and insertion · الترتيب: فقاعي، اختيار، وإدراج
Putting an array in order
- Sorting rearranges an array so the items go from smallest to largest.
- We sort in place: we move items around inside the same array, with no return value.
- Three classic methods do this: bubble, insertion, and selection sort.
ترتيب مصفوفة
- الترتيب يعيد ترتيب المصفوفة بحيث تنتقل العناصر من الأصغر إلى الأكبر.
- نرتب في المكان: نحرك العناصر داخل نفس المصفوفة، بدون قيمة إرجاع.
- ثلاث طرق كلاسيكية تقوم بذلك: البزغ، الإدراج، و الاختيار الترتيب.
Swapping two elements
- Every sort needs to swap two items. Save one in a temporary, then overwrite.
int t = a[i]; a[i] = a[j]; a[j] = t;exchanges positionsiandj.- Forgetting the temporary loses one of the values — always keep it.
تبديل عنصرين
- كل ترتيب يحتاج إلى تبديل عنصرين. احفظ أحدهما مؤقتًا، ثم استبدله.
int t = a[i]; a[i] = a[j]; a[j] = t;يغير مواضعiوj.- نسيان المؤقت يفقد أحد القيم — احتفظ به دائمًا.
Bubble sort
- Go through the array and compare each pair of neighbours:
a[j]anda[j + 1]. - If they are in the wrong order, swap them. The biggest value "bubbles" to the end.
- Repeat the passes until the whole array is in order.
ترتيب الفقاعات
- مرر عبر المصفوفة وقارن كل زوج من الأجوار:
a[j]وa[j + 1]. - إذا كانا في الترتيب الخاطئ، قم بتبديلهما. القيمة الأكبر "تطفو" نحو النهاية.
- كرر المرورات حتى تكون المصفوفة بأكملها مرتبة.
Insertion sort
- Treat the left part of the array as already sorted, and grow it one item at a time.
- Take the next item as a key, shift bigger sorted items to the right, then drop the key into the gap.
- This is how many people sort playing cards in their hand.
الترتيب بالإدراج
- تعامل مع الجزء الأيسر من المصفوفة على أنه مرتب بالفعل، ووسّعه عنصرًا واحدًا في كل مرة.
- خذ العنصر التالي كـ مفتاح، حرك العناصر المرتبة الأكبر إلى اليمين، ثم ضع المفتاح في الفراغ.
- هكذا يرتب الكثير من الناس بطاقات اللعب في أيدهم.
#include <stdio.h>
int main(void) {
int a[] = {3, 1, 2};
// one bubble pass: compare neighbours and swap if out of order
for (int j = 0; j < 2; j++) {
if (a[j] > a[j + 1]) {
int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
}
}
printf("%d %d %d\n", a[0], a[1], a[2]); // 1 2 3
return 0;
}
Common mistakes
- Bubble, selection and insertion sort are all O(n²).
- Trace a small array by hand to check your sort.
أخطاء شائعة
- البزغ، الاختيار والإدراج الترتيب هي جميعها O(n²).
- تتبع مصفوفة صغيرة يدويًا للتحقق من ترتيبك.
Now you try
- Sort ascending (smallest first), in place, with no return value.
- The checker tries several arrays, including negatives and an already-sorted one.
- Do not write a
main— the checker provides one.
الآن جرب بنفسك
- رتّب تصاعديًا (الأصغر أولاً)، في المكان، بدون قيمة إرجاع.
- يحاول المتحقق عدة مصفوفات، بما في ذلك السالبة والأخرى المرتبة مسبقًا.
- لا تكتب
main— لأن الفاحص يوفر واحداً تلقائياً.
Watch a sort run · مشاهدة عملية ترتيب
Bubble / selection / insertion all compare and swap into order. · الفقاعية / الاختيارية / الإدراجية تقارن جميعها وتبدل العناصر لترتيبها.
Complete void swap(int a[], int i, int j) so it exchanges the items at index i and j, in place (no return). Do not write a main. · أكمل void swap(int a[], int i, int j) لتبادل العناصر عند الفهارس i وj، محلياً (بدون إرجاع قيمة). لا تكتب دالة main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete void bubble_sort(int a[], int n) so it sorts the array ascending, in place, using bubble sort. Do not write a main. · أكمل void bubble_sort(int a[], int n) ليصنف المصفوفة تصاعدياً، في الموقع، باستخدام ترتيب الفقاعات. لا تكتب ** main **.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete void insertion_sort(int a[], int n) so it sorts the array ascending, in place, using insertion sort (take each item as a key and shift bigger items right). Do not write a main. · أكمل void insertion_sort(int a[], int n) ليصنف المصفوفة تصاعدياً، في الموقع، باستخدام ترتيب الإدراج (خُذ كل عنصر كمفتاح ونزّل العناصر الأكبر يميناً). لا تكتب ** main **.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.