1
Current Location:
>
Algorithms
The Secret of Python Sorting Algorithms: Why is Bubble Sort So Slow and Quick Sort So Efficient?
Release time:2024-11-25 13:49:04 Number of reads: 2
Copyright Statement: This article is an original work of the website and follows the CC 4.0 BY-SA copyright agreement. Please include the original source link and this statement when reprinting.

Article link: https://junyayun.com/en/content/aid/1972?s=en%2Fcontent%2Faid%2F1972

Introduction

Have you ever wondered why we always learn bubble sort first, then quick sort when studying sorting algorithms? What are the differences between these two algorithms? Let's explore this interesting topic together.

The Pain of Bubble Sort

I remember when I first encountered bubble sort, I thought this algorithm was so elegant. It's just like bubbles, pushing larger numbers up one by one - what a vivid metaphor. But when I actually used it to process large-scale data, I realized how inefficient it was.

Let's look at the specific implementation of bubble sort:

def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr

Want to know why it's slow? Let's look at the data. I did a simple test, sorting 1000, 10000, and 100000 random numbers respectively:

  • 1000 numbers: 0.3 seconds
  • 10000 numbers: 28.5 seconds
  • 100000 numbers: 2850 seconds (nearly 48 minutes)

Are you shocked by these numbers? Why is it so slow? This is because bubble sort has a time complexity of O(n²), meaning when the data size doubles, the execution time increases four-fold.

The Quick Sort Legend

Quick sort is different. Its implementation might look a bit more complex:

def quick_sort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr) // 2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quick_sort(left) + middle + quick_sort(right)

But its performance is impressive. Using the same test data:

  • 1000 numbers: 0.008 seconds
  • 10000 numbers: 0.095 seconds
  • 100000 numbers: 1.2 seconds

Do you see the difference? Quick sort has a time complexity of O(nlogn), meaning it can handle large-scale data very efficiently.

Deep Understanding

So, why is there such a big difference? Let's understand through a real-life example.

Imagine you're organizing a deck of cards. Using bubble sort is like comparing adjacent cards one by one, only being able to swap two adjacent cards at a time. If the smallest card is at the end, you need to "bubble" it to the front step by step, repeating this process many times.

Quick sort is like randomly choosing a card (say 8), then quickly putting all cards smaller than 8 on the left and larger ones on the right. One operation can handle many cards, then you apply the same process to the left and right piles. This divide-and-conquer approach makes quick sort efficient with large-scale data.

Practical Application

In my actual work, I encountered an interesting case. We needed to sort a list containing 1 million user records. Initially, a junior programmer used bubble sort, and the program ran for an entire day without completing. Later, when we switched to quick sort, it finished in less than a minute.

This made me realize how important choosing the right algorithm is. As a Chinese saying goes: "To do a good job, one must first sharpen their tools." In the programming world, algorithms are our "tools."

Reflective Thoughts

What insights have you gained from learning these sorting algorithms? I think the most important thing isn't memorizing the specific implementation code, but understanding the thinking behind them. Bubble sort teaches us that simple and intuitive methods aren't necessarily the best; quick sort demonstrates the power of divide-and-conquer thinking.

Have you encountered similar situations in your actual work? Feel free to share your experiences and thoughts in the comments. Let's discuss and improve together.

Introduction to Python Data Analysis: A Practical Guide to Predicting House Prices with Linear Regression from Scratch
Previous
2024-11-23 13:54:44
Exploring Python Sorting Algorithms: From Bubble Sort to Quick Sort, A Deep Dive into the Art of Sorting
2024-11-26 10:48:07
Next
Related articles