summaryrefslogtreecommitdiff
path: root/Year_2/Algorithms/data_structures/cc
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2023-01-07 18:47:11 +0100
committerSanto Cariotti <santo@dcariotti.me>2023-01-07 18:47:11 +0100
commitba36beaec6d37d26b075d96e58aad73151d6d39e (patch)
tree23226d5a3d5b49616abbb37e1ba3f9666faa489d /Year_2/Algorithms/data_structures/cc
parenta5534f16ecaf4b93f1bcd775f1e1df64203b3b65 (diff)
Fix algs structure
Diffstat (limited to 'Year_2/Algorithms/data_structures/cc')
-rw-r--r--Year_2/Algorithms/data_structures/cc/countingsort.cc83
-rw-r--r--Year_2/Algorithms/data_structures/cc/ordinamento-lineare.cc115
2 files changed, 0 insertions, 198 deletions
diff --git a/Year_2/Algorithms/data_structures/cc/countingsort.cc b/Year_2/Algorithms/data_structures/cc/countingsort.cc
deleted file mode 100644
index 49f29d7..0000000
--- a/Year_2/Algorithms/data_structures/cc/countingsort.cc
+++ /dev/null
@@ -1,83 +0,0 @@
-#include <fstream>
-#include <iostream>
-#include <string.h>
-#include <string>
-
-using namespace std;
-
-void
-counting_sort(int* A, size_t n, ofstream& fout)
-{
- int i, j;
- int max, min;
- int* freq;
- int* t;
- int range;
-
- max = A[0], min = A[0];
- t = new int[n];
-
- for (i = 1; i < n; ++i) {
- if (A[i] > max)
- max = A[i];
-
- if (A[i] < min)
- min = A[i];
- }
-
- range = (max - min) + 1;
-
- freq = new int[range];
-
- for (i = 0; i < range; ++i) {
- freq[i] = 0;
- }
-
- for (i = 0; i < n; ++i) {
- freq[A[i] - min] += 1;
- }
- for (i = 1; i < range; ++i) {
- freq[i] = freq[i] + freq[i - 1];
- }
- fout << "0 ";
- for (i = 0; i < range - 1; ++i) {
- fout << freq[i] << ' ';
- }
-
- for (i = n - 1; i >= 0; --i) {
- t[freq[A[i] - min] - 1] = A[i];
- freq[A[i] - min]--;
- }
-
- for (i = 0; i < n; ++i) {
- A[i] = t[i];
- fout << A[i] << ' ';
- }
- fout << endl;
-
- delete[] freq;
- delete[] t;
-}
-
-int
-main()
-{
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- int N;
-
- for (int i = 0; i < 100; ++i) {
- fin >> N;
- int* a = new int[N];
-
- for (int j = 0; j < N; ++j) {
- fin >> a[j];
- }
-
- counting_sort(a, N, fout);
-
- delete[] a;
- }
-
- return 0;
-}
diff --git a/Year_2/Algorithms/data_structures/cc/ordinamento-lineare.cc b/Year_2/Algorithms/data_structures/cc/ordinamento-lineare.cc
deleted file mode 100644
index 5af1b1d..0000000
--- a/Year_2/Algorithms/data_structures/cc/ordinamento-lineare.cc
+++ /dev/null
@@ -1,115 +0,0 @@
-#include <fstream>
-#include <iostream>
-#include <stdlib.h>
-#include <string>
-
-using namespace std;
-
-template <typename T>
-void
-counting_sort(T* A, size_t n, ofstream& fout, int incr, bool is_char = false)
-{
- int i, j;
- int max, min;
- int* freq;
- int* t;
- int range;
-
- for (i = 0; i < n; ++i) {
- A[i] = A[i] * incr;
- }
-
- max = A[0], min = A[0];
- t = new int[n];
-
- for (i = 1; i < n; ++i) {
- if (A[i] > max)
- max = A[i];
-
- if (A[i] < min)
- min = A[i];
- }
-
- range = (max - min) + 1;
-
- freq = new int[range];
-
- for (i = 0; i < range; ++i) {
- freq[i] = 0;
- }
-
- for (i = 0; i < n; ++i) {
- freq[static_cast<int>(A[i] - min)] += 1;
- }
- for (i = 1; i < range; ++i) {
- freq[i] = freq[i] + freq[i - 1];
- }
- fout << "0 ";
- for (i = 0; i < range - 1; ++i) {
- fout << freq[i] << ' ';
- }
-
- for (i = n - 1; i >= 0; --i) {
- t[freq[static_cast<int>(A[i] - min)] - 1] = A[i];
- freq[static_cast<int>(A[i] - min)]--;
- }
-
- for (i = 0; i < n; ++i) {
- A[i] = t[i];
- if (is_char) {
- fout << (char)A[i] << ' ';
- } else {
- fout << A[i] / incr << ' ';
- }
- }
- fout << endl;
-
- delete[] freq;
- delete[] t;
-}
-
-int
-main()
-{
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- string type;
- int N;
-
- for (int i = 0; i < 100; ++i) {
- fin >> type;
- fin >> N;
-
- if (type == "double") {
- double* a = new double[N];
-
- for (int j = 0; j < N; ++j) {
- fin >> a[j];
- }
-
- counting_sort(a, N, fout, 10);
-
- delete[] a;
- } else if (type == "char") {
- char* a = new char[N];
-
- for (int j = 0; j < N; ++j) {
- fin >> a[j];
- }
-
- counting_sort(a, N, fout, 1, true);
- delete[] a;
- } else {
- int* a = new int[N];
-
- for (int j = 0; j < N; ++j) {
- fin >> a[j];
- }
-
- counting_sort(a, N, fout, 1);
- delete[] a;
- }
- }
-
- return 0;
-}