You work at the college library. You're in the middle of a quiet afternoon when suddenly a shipment of 1,280 different books arrives. The books have been dropped of in one long straight line, but they're all out of order, and the automatic sorting system is broken. To make matters worse, classes start tomorrow, which means that first thing in the morning, students will show up in droves looking for these books. How can you get them all sorted in time? One way would be to start at one end of the line with the first pair of books. If the first two books are in order, then leave them as they are. Otherwise, swap them. Then, look at the second and third books, repeat the process, and continue until you reach the end of the line. At some point, you'll come across the book that should be last, and keep swapping it with every subsequent book, moving it down the line until it reaches the end where it belongs. Then, start from the beginning and repeat the process to get the second to last book in its proper place, and keep going until all books are sorted. This approach is called Bubble Sort. It's simple but slow. You'd make 1,279 comparisons in the first round, then 1,278, and so on, adding up to 818,560 comparisons. If each took just one second, the process would take over nine days. A second strategy would be to start by sorting just the first two books. Then, take the third book and compare it with the book in the second spot. If it belongs before the second book, swap them, then compare it with the book in the first spot, and swap again if needed. Now you've sorted the first three books. Keep adding one book at a time to the sorted sub-line, comparing and swapping the new book with the one before it until it's correctly placed among the books sorted so far. This is called Insertion Sort. Unlike Bubble Sort, it usually doesn't require comparing every pair of books. On average, we'd expect to only need to compare each book to half of the books that came before it. In that case, the total number of comparisons would be 409,280, taking almost five days. You're still doing way too many comparisons. Here's a better idea. First, pick a random book. Call it the partition and compare it to every other book. Then, divide the line by placing all the books that come before the partition on its left and all the ones that come after it on its right. You've just saved loads of time by not having to compare any of the books on the left to any of the ones on the right ever again. Now, looking only at the books on the left, you can again pick a random partition book and separate those books that come before it from those that come after it. You can keep creating sub-partitions like this until you have a bunch of small sub-lines, each of which you'd sort quickly using another strategy, like Insertion Sort. Each round of partitioning requires about 1,280 comparisons. If your partitions are pretty balanced, dividing the books into 128 sub-lines of ten would take about seven rounds, or 8,960 seconds. Sorting these sub-lines would add about 22 seconds each. All in all, this method known as QuickSort could sort the books in under three and a half hours. But there's a catch. Your partitions could end up lopsided, saving no time at all. Luckily, this rarely happens. That's why QuickSort is one of the most efficient strategies used by programmers today. They use it for things like sorting items in an online store by price, or creating a list of all the gas stations close to a given location sorted by distance. In your case, you're done quick sorting with time to spare. Just another high-stakes day in the library.
אתם עובדים בספריית המכללה. אתם באמצע אחר צהריים שקט כשפתאום מגיע משלוח של 1,280 ספרים שונים. הספרים הונחו בשורה אחת ישרה, אבל הם אינם מסודרים, ומכונת הסידור האוטומטית מקולקלת. בנוסף לכך, השיעורים מתחילים מחר, מה שאומר שדבר ראשון בבוקר, יגיעו מלא סטודנטים שיחפשו את הספרים האלו. איך אתם יכולים לסדר את כולם בזמן? דרך אחת היא להתחיל בצד אחד של השורה עם זוג הספרים הראשון. אם שני הספרים הראשונים מסודרים, השאירו אותם כמו שהם. אם לא, החליפו אותם. ואז, הביטו בספר השני והשלישי, וחזרו על התהליך, והמשיכו כך עד שתגיעו לקצה השורה. בנקודה מסויימת, אתם תגיעו לספר שצריך להיות אחרון, ולהמשיך להחליף אותו עם כל ספר שלפניו, להזיז אותו במורד השורה עד שהוא יגיע לסוף לשם הוא שייך. אז, התחילו מההתחלה וחזרו על התהליך כדי להביא את הספר השני מהסוף למקומו הראוי, ולהמשיך עד שכל הספרים מסודרים. הגישה הזו נקראת 'מיון בועה'. היא פשוטה אבל איטית. אתם תעשו 1279 השוואות בסיבוב הראשון, אז 1278, וכך הלאה, עד שתגיעו ל 818,560 השוואות. אם כל אחת תיקח שניה, התהליך יקח יותר מתשעה ימים. אסטרטגיה שניה תהיה להתחיל במיון רק שני הספרים הראשונים. ואז, קחו את הספר השלישי והשוו אותו עם הספר במקום השני. אם הוא שייך לפני הספר השני, החליפו אותם, אז השוו אותו לספר במקום הראשון, והחליפו אותם אם יש צורך. עכשיו סידרתם את שלושת הספרים הראשונים. המשיכו להוסיף ספרים אחד אחרי השני לתת השורה הממויינת, השוו והחליפו את הספר החדש עם זה שלפניו עד שהוא ממוקם במקומו בין הספרים הממויינים עד כה. זה נקרא 'מיון הכנסה'. בניגוד למיון בועה, הוא בדרך כלל לא דורש השוואה של כל זוג ספרים. בממוצע, נצפה שנדרש להשוות כל ספר לחצי מהספרים שהגיעו לפניו. במקרה הזה, הסך הכולל של השוואות יהיה 409,280, ויקח כמעט חמישה ימים. אתם עדיין עורכים יותר מדי השוואות. הנה רעיון טוב יותר. ראשית, בחרו ספר אקראי. קראו לו המחיצה, והשוו אותו לכל ספר אחר. אז, חלקו את הקו בכך שתשימו כל ספר שבא לפני המחיצה לשמאלו וכל אלה שמגיעים אחריו לימינו. עכשיו חסכתם המון זמן בכך שלא השוונו אף ספר משמאל לאף ספר מימין. עכשיו, אם מביטים רק בספרים משמאל, אתם יכולים לבחור שוב ספר אקראי ולהפריד את הספרים שמגיעים לפניו מאלה שמגיעים אחריו. אתם יכולים להמשיך ליצור תת-מחיצות כמו זו עד שיש לכם קבוצה של תת-שורות קטנות, כל אחת תמיינו בשימוש באסטרטגיה אחרת, כמו מיון הכנסה. כל סיבוב של חלוקה דורש בערך 1280 השוואות. אם המחיצות שלכם די מאוזנות, חלוקת הספרים ל 129 תת-שורות של עשר תקח בערך שבעה סבבים, או 8,960 שניות. מיון תת-השורות האלו יוסיף בערך 22 שניות לכל אחת. בסך הכל, השיטה הזו שידועה כ'סידור מהיר' יכולה לסדר את הספרים תוך פחות משלוש וחצי שעות. אבל יש קאטץ'. המחיצות שלכם יכולת להיות לא מאוזנות, ולא לחסוך זמן בכלל. למרבה המזל, זה כמעט אף פעם לא קורה. לכן מיון מהיר היא אחת האסטרטגיות הכי יעילות בשימוש על ידי מתכנתים היום. הם משתמשים בה לדברים כמו מיון עצמים בחנות אינטרנטית לפי מחיר, או ליצירת רשימה של כל תחנות הדלק שקרובות למיקום נתון, ממוינות לפי מרחק. במקרה שלכם, אתם תסיימו מיון מהיר עם זמן עודף. זהו עוד יום של חיים על הקצה בספריה.