LuchoBazz / c-algorithm-snippets

(In Progress) In this repository there is a collection of useful algorithms to use in competitive programming in C.
MIT License
1 stars 0 forks source link

Add std qsort with random obfuscation to avoid worst case #3

Closed LuchoBazz closed 3 months ago

LuchoBazz commented 3 months ago

1435730

Reference Rainboy: https://codeforces.com/contest/1401/submission/90590519 Reference 1435730: https://codeforces.com/contest/1956/submission/256487813

#define sort_type ll
sort_type compare(const void *a, const void *b) {
  sort_type ia = *(sort_type *)a;
  sort_type ib = *(sort_type *)b;

  return ia - ib;
}

void sort(sort_type *array, int n) {
  srand(time(NULL));

  sort_type tmp, j;
  for (int i = 0; i < n; i++) {
    j = rand() % (i + 1);
    tmp = array[i], array[i] = array[j], array[j] = tmp;
  }
  qsort(array, n, sizeof *array, compare);
}
LuchoBazz commented 3 months ago

Commits:

1ab4331865ba8ed4085d584c09e8b76e90082024 d2195d350f2031984bd4afdd07728a53dd77364c