chore(C): add shuffle array algorithm (#827)

Co-authored-by: Ivan Kirspu <luneyune@users.noreply.github.com>
pull/828/head
Ivan Kirspu 2022-09-03 21:55:52 +07:00 committed by GitHub
parent 170a706b1b
commit cfc589e55f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 0 deletions

View File

@ -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)

View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <time.h>
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;
}