shuffle function in c

To shuffle an array in C, you can use the Fisher-Yates algorithm. Here's an example code snippet:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void shuffle(int arr[], int n) {
    srand(time(NULL));
    for (int i = n - 1; i > 0; i--) {
        int j = rand() % (i + 1);
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    shuffle(arr, n);

    printf("Shuffled array: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

This code defines a shuffle function that takes an array arr and its length n as parameters. It uses the srand function from the time.h library to seed the random number generator. Then, it iterates from the last element of the array to the first, generating a random index j and swapping the elements at indices i and j. Finally, the main function creates an example array, shuffles it using the shuffle function, and prints the shuffled array.

When you run this code, you will see the shuffled array printed on the console.