From fb0c97cb976b4d2f14fc5dea9ffbb35fa3149b81 Mon Sep 17 00:00:00 2001 From: Hyder Date: Mon, 18 Aug 2025 19:08:30 +0300 Subject: I have sorted numbers using selection sort :D --- Palindrome/Palindrome.c | 2 +- README | 3 ++- SelectionSort/SelectionSort.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 SelectionSort/SelectionSort.c diff --git a/Palindrome/Palindrome.c b/Palindrome/Palindrome.c index cf01782..6a70b80 100644 --- a/Palindrome/Palindrome.c +++ b/Palindrome/Palindrome.c @@ -1,7 +1,7 @@ #include -// i made string_length equal tp (-1) cause string indices start from number 0, probably there is a better way represent what i want :( +// i made string_length equal to (-1) cause string indices start from number 0, probably there is a better way to represent what i want :( char *reverse_string(char *string_input) { int string_length = -1; diff --git a/README b/README index 1a85d40..2f414fa 100644 --- a/README +++ b/README @@ -11,4 +11,5 @@ CONTENTS - Hexadecimal to integer converter solution, in ROOT/HTOI/HTOI.c - Vowels and consonants counter of a string, in ROOT/VACC/VowelsAndConsonantCounter.c - Binary(28-bit) to decimal converter, in ROOT/BTD/28bitToDecimal.c -- Palindrome words checker exercise, in ROOT/Palindrome/Palindrome.c \ No newline at end of file +- Palindrome words checker exercise, in ROOT/Palindrome/Palindrome.c +- Sorting numbers with (selection sort), in ROOT/SelectionSort/SelectionSort.c diff --git a/SelectionSort/SelectionSort.c b/SelectionSort/SelectionSort.c new file mode 100644 index 0000000..e9845c6 --- /dev/null +++ b/SelectionSort/SelectionSort.c @@ -0,0 +1,35 @@ +#include + +void selection_sort(int *Array, int length) { + for (int i = 0;i < length;i++) { + int current = Array[i]; + int smallest = Array[i]; + int tmp = i; + for (int j = i + 1;j < length;j++) { + if (Array[j] < smallest) { + smallest = Array[j]; + tmp = j; + } + } + Array[i] = smallest; + Array[tmp] = current; + } +} + +int main() { + + int Array[] = {95, 12, 84, 23, 7, 66, 41, 59, 18, 32, 73, 5, 91, 46, 27, 63, 14, 39, 100, 2, 77, 50, 35, 9, 81, 3, 45, 22, 60, 88, 11, 36, 71, 25, 49, 16, 68, 1, 53, 29, 86, 19, 43, 30, 62, 8, 79, 4, 21, 98}; + int ArrayLength = sizeof(Array) / sizeof(Array[0]); + printf("Not Sorted: "); + for (int i = 0;i < ArrayLength;i++) { + printf("%d ", Array[i]); + } + printf("\n\n"); + selection_sort(Array, ArrayLength); + printf("Sorted: "); + for (int i = 0;i < ArrayLength;i++) { + printf("%d ", Array[i]); + } + printf("\n"); + return 0; +} \ No newline at end of file -- cgit v1.2.3