#include <algorithm>
#include <cassert>
#include <iostream>
#include <random>
#include <stdexcept>
#include <vector>
void insertion(std::vector<int> &a) {
  for (std::size_t i = 1; i < a.size(); ++i) {
    int x = a[i];
    auto j = i;
    while (j > 0 && a[j - 1] > x) {
      a[j] = a[j - 1];
      --j;
    }
    a[j] = x;
  }
}
void selection(std::vector<int> &a) {
  for (std::size_t i = 0; i < a.size(); ++i) {
    auto k = i;
    for (auto j = i + 1; j < a.size(); ++j)
      if (a[j] < a[k])
        k = j;
    std::swap(a[i], a[k]);
  }
}
void bubble(std::vector<int> &a) {
  for (std::size_t end = a.size(); end > 1; --end) {
    bool changed = false;
    for (std::size_t j = 1; j < end; ++j)
      if (a[j - 1] > a[j]) {
        std::swap(a[j - 1], a[j]);
        changed = true;
      }
    if (!changed)
      break;
  }
}
void quick(std::vector<int> &a, int l, int r) {
  if (l >= r)
    return;
  int pivot = a[l + (r - l) / 2], i = l, j = r;
  while (i <= j) {
    while (a[i] < pivot)
      ++i;
    while (a[j] > pivot)
      --j;
    if (i <= j) {
      std::swap(a[i], a[j]);
      ++i;
      --j;
    }
  }
  if (l < j)
    quick(a, l, j);
  if (i < r)
    quick(a, i, r);
}
void sift(std::vector<int> &a, int root, int n) {
  while (2 * root + 1 < n) {
    int child = 2 * root + 1;
    if (child + 1 < n && a[child + 1] > a[child])
      ++child;
    if (a[root] >= a[child])
      break;
    std::swap(a[root], a[child]);
    root = child;
  }
}
void heapSort(std::vector<int> &a) {
  int n = static_cast<int>(a.size());
  for (int i = n / 2 - 1; i >= 0; --i)
    sift(a, i, n);
  for (int end = n - 1; end > 0; --end) {
    std::swap(a[0], a[end]);
    sift(a, 0, end);
  }
}
void mergePart(std::vector<int> &a, std::vector<int> &b, int l, int r) {
  if (r - l <= 1)
    return;
  int m = l + (r - l) / 2;
  mergePart(a, b, l, m);
  mergePart(a, b, m, r);
  int i = l, j = m, k = l;
  while (i < m && j < r)
    b[k++] = a[i] <= a[j] ? a[i++] : a[j++];
  while (i < m)
    b[k++] = a[i++];
  while (j < r)
    b[k++] = a[j++];
  for (int t = l; t < r; ++t)
    a[t] = b[t];
}
void mergeSort(std::vector<int> &a) {
  std::vector<int> b(a.size());
  mergePart(a, b, 0, static_cast<int>(a.size()));
}
std::vector<int> counting(const std::vector<int> &a, int k) {
  if (k < 0)
    throw std::invalid_argument("negative range");
  std::vector<int> cnt(k + 1), out;
  for (int x : a) {
    if (x < 0 || x > k)
      throw std::invalid_argument("out of range");
    ++cnt[x];
  }
  for (int x = 0; x <= k; ++x)
    for (int n = cnt[x]; n > 0; --n)
      out.push_back(x);
  return out;
}
int main() {
  std::mt19937 gen(7);
  std::vector<std::vector<int>> cases{{},        {1},       {2, 2, 2},
                                      {1, 2, 3}, {3, 2, 1}, {-2, 4, 0, -2}};
  for (int n = 0; n < 80; ++n) {
    std::vector<int> a(n);
    for (int &x : a)
      x = static_cast<int>(gen() % 21) - 10;
    cases.push_back(a);
  }
  for (const auto &a : cases) {
    auto expected = a;
    std::sort(expected.begin(), expected.end());
    for (auto sort : {insertion, selection, bubble, heapSort, mergeSort}) {
      auto b = a;
      sort(b);
      assert(b == expected);
    }
    auto b = a;
    quick(b, 0, static_cast<int>(b.size()) - 1);
    assert(b == expected);
  }
  assert((counting({3, 0, 3, 1}, 3) == std::vector<int>{0, 1, 3, 3}));
  std::cout << "chapter07: passed\n";
}
