summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Guschin <guschin.drew@gmail.com>2022-02-17 12:26:41 +0400
committerAndrew Guschin <guschin.drew@gmail.com>2022-02-17 12:26:41 +0400
commit8065f44d09a9dd570bbcc94b9806ee558456aa62 (patch)
tree211ff09da311442b16924761c5f5cf2880b1e72d
Добавил сортировку прямым выбором
-rw-r--r--straight-selection.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/straight-selection.c b/straight-selection.c
new file mode 100644
index 0000000..f05d098
--- /dev/null
+++ b/straight-selection.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+
+void
+straight_selection(int *array, size_t size)
+{
+ for (int i = 0; i < size; ++i)
+ {
+ int k = i;
+ int x = array[i];
+ for (int j = i; j < size; ++j)
+ {
+ if (array[j] < x)
+ {
+ k = j;
+ x = array[j];
+ }
+ }
+ array[k] = array[i];
+ array[i] = x;
+ }
+}
+
+void
+print_array(int *array, size_t size)
+{
+ if (size == 0) printf("array = { }\n");
+
+ printf("array = { ");
+ for (int i = 0; i < size - 1; ++i)
+ printf("%i, ", array[i]);
+ printf("%i };\n", array[size - 1]);
+}
+
+int
+main()
+{
+ int array[] = { 2, 7, 12, 30, 11, 4, 12, 5, 20 };
+ size_t size = sizeof(array) / sizeof(*array);
+ print_array(array, size);
+ straight_selection(array, size);
+ print_array(array, size);
+
+ return 0;
+}