You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby语言five_sort算法死循环问题求助

Fixing the Infinite Loop in Your five_sort Implementation

Hey there, let's tackle that infinite loop issue you're hitting with your five_sort function. First, let's break down why this happens—most of the time, it's because your pointer logic isn't correctly moving towards terminating the loop, or you're not handling cases where you hit a 5 (or non-5) properly.

Common Causes of the Infinite Loop

  • You might be stuck moving only one pointer when you should adjust both, e.g., when you find a 5 at the left, you don't shift the right pointer to hunt for a non-5 to swap with, so you keep rechecking the same 5 indefinitely.
  • Your loop condition might be too vague (like while True instead of checking pointer positions), or you forget to update pointers after swapping, leading to the same pair of indices being processed over and over.

A Working Implementation (No Infinite Loops)

The best fit here is a two-pointer technique—one starting at the array's start to hunt for 5s, and one at the end to hunt for non-5s. This keeps non-5 elements in their original order while shuttling 5s to the end, and it aligns perfectly with your while loop and array method restrictions.

Here's the code:

def five_sort(arr):
    left = 0
    right = len(arr) - 1  # Calculate once at the start
    
    while left < right:
        # Move left pointer right until we find a 5 or meet the right pointer
        while left < right and arr[left] != 5:
            left += 1
        
        # Move right pointer left until we find a non-5 or meet the left pointer
        while left < right and arr[right] == 5:
            right -= 1
        
        # Swap the 5 (left) with the non-5 (right) if pointers haven't crossed
        if left < right:
            arr[left], arr[right] = arr[right], arr[left]
            left += 1
            right -= 1
    
    return arr

Let's Walk Through the Logic

  1. Initialization: We start with left at the first index and right at the last index of the array.
  2. Inner Left Loop: We skip over non-5 elements by moving left right—these elements are already in the correct position.
  3. Inner Right Loop: We skip over 5 elements at the end by moving right left—these 5s are already where they need to be.
  4. Swap & Update Pointers: If left is still before right, we swap the misplaced 5 with the out-of-place non-5, then move both pointers toward the center. This ensures we never reprocess the same elements, and the loop will terminate once the pointers meet.

Testing this with your example input [1,2,5,3,2,5,5,7] gives the expected output [1,2,3,2,7,5,5,5], and no infinite loops because every iteration either moves pointers closer together or ends the loop.

Why This Avoids Infinite Loops

Every pass through the outer while loop does one of three things:

  • Moves left rightwards,
  • Moves right leftwards,
  • Swaps elements and moves both pointers inward.

Since left can never exceed right, the loop will always terminate once the pointers meet or cross.

内容的提问来源于stack exchange,提问作者James Stuckey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:12:29