Jump Sort Visualization

Visualizing an O(n + k) distribution sort algorithm

Speed:
Ready to sort...
Java
Python
public class JumpSort {
    public int[] sortArray(int[] nums) {
        // Separate negatives and non-negatives
        int[] negatives = new int[nums.length];
        int[] nonNegatives = new int[nums.length];
        int negCount = 0, posCount = 0;

        for (int num : nums) {
            if (num < 0) negatives[negCount++] = num;
            else nonNegatives[posCount++] = num;
        }

        // Handle negatives (bucketed using abs)
        int maxAbsNeg = 0;
        for (int i = 0; i < negCount; i++) {
            maxAbsNeg = Math.max(maxAbsNeg, Math.abs(negatives[i]));
        }
        // ... (Bucket creation logic) ...
        
        // Flatten and combine results
        int[] result = new int[nums.length];
        // ... (Merge logic) ...
        return result;
    }
}
class JumpSort:
    def sortArray(self, nums):
        # Separate negatives and non-negatives
        negatives = [x for x in nums if x < 0]
        non_negatives = [x for x in nums if x >= 0]

        # Prepare negative side using counts (Pigeonhole sort variation)
        if negatives:
            max_neg = abs(min(negatives))
            neg_counts = [0] * max_neg
            for num in negatives:
                index = abs(num) - 1
                neg_counts[index] += 1
            # Reconstruct negative list (sorted)
            neg_list = []
            for i in reversed(range(max_neg)):
                neg_list.extend([-i - 1] * neg_counts[i])
        else:
            neg_list = []

        # Prepare non-negative side using counts
        if non_negatives:
            max_nonneg = max(non_negatives)
            pos_counts = [0] * (max_nonneg + 1)
            for num in non_negatives:
                pos_counts[num] += 1
            # Reconstruct positive list
            pos_list = []
            for i in range(max_nonneg + 1):
                pos_list.extend([i] * pos_counts[i])
        else:
            pos_list = []

        return neg_list + pos_list