Shuffling List with Fisher-Yates Algorithm

  • Share this:

Code introduction


Define a function that uses the Fisher-Yates algorithm to shuffle an input list in-place, meaning to randomly reorder the elements of the list.


Technology Stack : Define a function that uses the Fisher-Yates algorithm to shuffle an input list in-place, meaning to randomly reorder the elements of the list.

Code Type : Function

Code Difficulty : Intermediate


                
                    
import os
import sys
import time
import json
import math
import random

def shuffle_list(input_list):
    """Shuffle a list in-place using the Fisher-Yates algorithm."""
    for i in range(len(input_list) - 1, 0, -1):
        j = random.randint(0, i)
        input_list[i], input_list[j] = input_list[j], input_list[i]
    return input_list