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— הבוקר מספק אותו already.
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) כך שתסדר את המערך בסדר עולה, במקום, באמצעות מיון הכנסה (take each item as a key and shift bigger items right). אל תכתוב main.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.