内容中心

评价高的2026年商场触摸一体机实力厂家怎么联系

To address your question about Bubble Sort, let’s break down the algorithm, its key properties, and common use cases with examples.

What is Bubble Sort?

Bubble Sort is a simple in-place sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order. The largest unsorted element "bubbles up" to its correct position at the end of the list after each pass.

Key Steps of Bubble Sort

  1. Compare adjacent elements (e.g., arr[j] and arr[j+1]).
  2. Swap them if arr[j] > arr[j+1].
  3. Move to the next pair until the end of the unsorted part of the list.
  4. Repeat until no swaps are needed (list is sorted).

Time & Space Complexity

  • Worst Case: O(n²) (reverse-sorted array, requires n passes with n comparns each).
  • Best Case: O(n) (already sorted array, with an optimization to stop early if no swaps occur).
  • Space: O(1) (no extra space needed).

Common Variations & Code Examples


1. Basic Bubble Sort (with Early Termination)

This version stops early if the array becomes sorted before all passes are done.

def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        swapped = False
        # Last i elements are already sorted
        for j in range(n - i -1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
                swapped = True
        if not swapped:
            break  # No swaps → array is sorted
    return arr

# Example usage
arr = [64,34,25,12,22,11,90]
print("Sorted array:", bubble_sort(arr))  # Output: [11,12,22,25,34,64,90]

2. Count Number of Swaps

Track how many swaps are needed to sort the array:

def bubble_sort_with_swaps(arr):
    n = len(arr)
    swap_count =0
    for i in range(n):
        swapped = False
        for j in range(n -i -1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
                swap_count +=1
                swapped = True
        if not swapped:
            break
    return arr, swap_count

# Example
arr = [64,34,25,12,22,11,90]
sorted_arr, swaps = bubble_sort_with_swaps(arr)
print("Swaps:", swaps)  # Output: 15 (for this example)

3. Array After k Passes

Return the array state after exactly k passes (or earlier if sorted):

def bubble_sort_k_passes(arr, k):
    n = len(arr)
    for i in range(k):
        swapped = False
        for j in range(n -i -1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
                swapped = True
        if not swapped:
            break  # Stop if sorted early
    return arr

# Example: After 2 passes
arr = [64,34,25,12,22,11,90]
print("After 2 passes:", bubble_sort_k_passes(arr,2))  # Output: [25,12,22,11,34,64,90]

Summary

Bubble Sort is easy to implement but inefficient for large datasets. It’s useful for small or nearly sorted arrays, and for teaching basic sorting concepts. Let me know if you need clarification on any specific variation! ✨
If your original question had a specific input (like a given array and k), feel free to share it, and I can provide a tailored solution.

```python
# Final code snippet (all-in-one helper)
def bubble_sort_helper(arr, k=None, count_swaps=False):
    n = len(arr)
    swap_count =0
    sorted_early = False
    max_passes = k if k is not None else n

    for i in range(max_passes):
        swapped = False
        for j in range(n -i -1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
                swap_count +=1
                swapped = True

        if not swapped:
            sorted_early = True
            break

    if count_swaps:
        return arr, swap_count
    elif k is not None:
        return arr
    else:
        return arr

Use this helper to get:  
- Sorted array: `bubble_sort_helper(arr)`  
- Array after k passes: `bubble_sort_helper(arr, k=2)`  
- Swap count: `bubble_sort_helper(arr, count_swaps=True)`  

Let me know if you need further help! 
            

作者声明:本文包含人工智能生成内容。

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?