diff --git a/algorithms/C/README.md b/algorithms/C/README.md index 50a7eb55..95c7796c 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -5,6 +5,7 @@ - [Even and Odd](arrays/even-and-odd.c) - [Unique Elements in an array](arrays/unique-elements-in-an-array.c) - [Reverse an array](arrays/reverse-array.c) +- [Shuffle an array](arrays/shuffle_array.c) - [Maximum difference](arrays/maximum-difference.c) - [Largest Element](arrays/largestElement.c) - [Second Largest Element](arrays/secondLargestElement.c) diff --git a/algorithms/C/arrays/shuffle_array.c b/algorithms/C/arrays/shuffle_array.c new file mode 100644 index 00000000..fa94cb1f --- /dev/null +++ b/algorithms/C/arrays/shuffle_array.c @@ -0,0 +1,54 @@ +/* + +[PROBLEM]: Given an array, of size n, shuffle it. + +[TIME COMPLEXITY]: O(N) + +[SAMPLE OF OUTPUT]: + +Before shuffling: +1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 +After shuffling: +9 6 4 10 11 15 5 3 12 1 7 14 2 8 13 + +*/ + +#include +#include +#include + +void swap(int *a, int *b) // Swap values between *a and *b +{ + int tmp = *a; + *a = *b; + *b = tmp; +} + +void shuffle(int *array, int size) // Randomly shuffles given array +{ + for (int i = 0; i < size; i++) { + swap(array + i, array + (rand() % size)); // Each element swaps with another random element + } +} + +void print_array(int *array, int size) // Printing array +{ + for (int i = 0; i < size; i++) { + printf("%d ", array[i]); + } + putchar('\n'); +} + +int main() +{ + srand(time(NULL)); // Setup of random seed for random generation + int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; + printf("Before shuffling:\n"); + print_array(array, 15); + + shuffle(array, 15); + printf("After shuffling:\n"); + print_array(array, 15); + + return 0; +}