Skip to content

Commit 03791d6

Browse files
committed
<feat> extended original counting sort to work with all integers
1 parent 0c57597 commit 03791d6

File tree

8 files changed

+61
-6
lines changed

8 files changed

+61
-6
lines changed

Diff for: CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ add_executable(test_counting_sort test_counting_sort.cpp counting_sort.h test.h)
1212
add_executable(test_selection_sort.cpp test_selection_sort.cpp selection_sort.h test.h)
1313

1414
add_executable(test_intro_sort.cpp test_intro_sort.cpp intro_sort.h test.h)
15+
16+
add_executable(test_counting_sort_extended test_counting_sort_extended.cpp counting_sort.h test.h)

Diff for: analisis/.RData

17 Bytes
Binary file not shown.

Diff for: analisis/.Rhistory

+11
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,14 @@ plot(log10(S$n), log10(S$`duration(ns)`), col="red", main="Tiempo de ejecución
385385
points(log10(C$n), log10(C$`duration(ns)`), col="green", pch=20, type = "b")
386386
points(log10(I$n), log10(I$`duration(ns)`), col="blue", pch=20, type="b")
387387
legend("topleft", col=c("red", "blue", "green"), legend=c("Selection sort", "Intro sort", "Counting sort"), lty=1)
388+
library(readr)
389+
library(ggplot2)
390+
C <- read_csv("counting_sort.csv")
391+
I <- read_csv("intro_sort.csv")
392+
S <- read_csv("selection_sort.csv")
393+
CE <- read_csv("counting_sort_extended.csv")
394+
plot(log10(S$n), log10(S$`duration(ns)`), col="red", main="Tiempo de ejecución vs. tamaño de entrada (escala logarítmica)", xlab="log(n)", ylab="log(Tiempo de ejecución (ns))", pch=20, type="b")
395+
points(log10(C$n), log10(C$`duration(ns)`), col="green", pch=20, type = "b")
396+
points(log10(CE$n), log10(CE$`duration(ns)`), col="pink", pch=20, type = "b")
397+
points(log10(I$n), log10(I$`duration(ns)`), col="blue", pch=20, type="b")
398+
legend("topleft", col=c("red", "blue", "green", "pink"), legend=c("Selection sort", "Intro sort", "Counting sort", "Counting sort extended"), lty=1)

Diff for: analisis/analisis.Rmd

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ library(ggplot2)
1313
C <- read_csv("counting_sort.csv")
1414
I <- read_csv("intro_sort.csv")
1515
S <- read_csv("selection_sort.csv")
16+
CE <- read_csv("counting_sort_extended.csv")
1617
```
1718
```{r}
1819
plot(log10(S$n), log10(S$`duration(ns)`), col="red", main="Tiempo de ejecución vs. tamaño de entrada (escala logarítmica)", xlab="log(n)", ylab="log(Tiempo de ejecución (ns))", pch=20, type="b")
1920
points(log10(C$n), log10(C$`duration(ns)`), col="green", pch=20, type = "b")
21+
points(log10(CE$n), log10(CE$`duration(ns)`), col="pink", pch=20, type = "b")
2022
points(log10(I$n), log10(I$`duration(ns)`), col="blue", pch=20, type="b")
21-
legend("topleft", col=c("red", "blue", "green"), legend=c("Selection sort", "Intro sort", "Counting sort"), lty=1)
23+
legend("topleft", col=c("red", "blue", "green", "pink"), legend=c("Selection sort", "Intro sort", "Counting sort", "Counting sort extended"), lty=1)
2224
```
2325

2426

Diff for: analisis/analisis.nb.html

+20-5
Large diffs are not rendered by default.

Diff for: counting_sort.h

+20
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,24 @@ void counting_sort(int *a, int size) {
3535
delete[] c;
3636
}
3737

38+
39+
// enteros de nk a k (todos los enteros en un rango finito)
40+
void counting_sort_extended(int *a, int size) {
41+
// 1. encontrar el elemento minimo
42+
int nk = a[0];
43+
for (int i = 0; i < size; ++i) {
44+
nk = std::min(nk, a[i]);
45+
}
46+
// 2. quitar a cada elemento el minimo
47+
for (int i = 0; i < size; ++i) {
48+
a[i] = a[i] - nk;
49+
}
50+
// 3. utilizar counting sort normal
51+
counting_sort(a, size);
52+
// 4. sumar el minimo a cada elemento
53+
for (int i = 0; i < size; ++i) {
54+
a[i] = a[i] + nk;
55+
}
56+
}
57+
3858
#endif //ORDENAMIENTO_COUNTING_SORT_H

Diff for: images/grafico.JPG

-31.3 KB
Loading

Diff for: main.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ int main() {
2323
print_array(a2);
2424
counting_sort(a2, 9);
2525
print_array(a2);
26+
// ejemplo 3
27+
int a3[] = {-39, 34, 42, 40, -44, 44, 31, 19, -3, -1, -7, 0, 0, -1, -40};
28+
print_array(a3);
29+
counting_sort_extended(a3, 15);
30+
print_array(a3);
2631
}

0 commit comments

Comments
 (0)