summaryrefslogtreecommitdiff
path: root/graphs/piscine/selection_sort
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
commitc9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch)
tree3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/piscine/selection_sort
add: graphs et rushs
Diffstat (limited to 'graphs/piscine/selection_sort')
-rw-r--r--graphs/piscine/selection_sort/selection_sort.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/graphs/piscine/selection_sort/selection_sort.c b/graphs/piscine/selection_sort/selection_sort.c
new file mode 100644
index 0000000..98adc7e
--- /dev/null
+++ b/graphs/piscine/selection_sort/selection_sort.c
@@ -0,0 +1,30 @@
+#include <stddef.h>
+
+void swap(int *a, int *b)
+{
+ int tmp = *a;
+ *a = *b;
+ *b = tmp;
+}
+
+unsigned array_min(const int arr[], unsigned start, unsigned size)
+{
+ unsigned min = start;
+ for (; start < size; start++)
+ {
+ if (arr[min] > arr[start])
+ {
+ min = start;
+ }
+ }
+ return min;
+}
+
+void selection_sort(int arr[], unsigned size)
+{
+ for (size_t i = 0; i < size; i++)
+ {
+ unsigned j = array_min(arr, i, size);
+ swap(&(arr[i]), &(arr[j]));
+ }
+}