From d9e57e8ca06096cc94879efe9e6e474cd57c6b2a Mon Sep 17 00:00:00 2001 From: mpabi Date: Sun, 26 May 2024 07:24:45 +0000 Subject: [PATCH] added bubble sort alg. --- cpp/myfunc.cpp | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/cpp/myfunc.cpp b/cpp/myfunc.cpp index b621842..c0ef119 100644 --- a/cpp/myfunc.cpp +++ b/cpp/myfunc.cpp @@ -1,35 +1,32 @@ #include "myfunc.h" -// Lokalna deklaracja funkcji my_strlen -static int my_strlen(const char *str) { - int length = 0; - while (str[length] != '\0') { - length++; +bool compareWords(const char* a, int lenA, const char* b, int lenB) { + int minLen = (lenA < lenB) ? lenA : lenB; + int cmp = strncmp(a, b, minLen); + if (cmp == 0) { + return lenA < lenB; } - return length; + return cmp < 0; } -void count_characters(const char* alfabet, const char* slowo, uint8_t* wynik) { - // Użycie my_strlen do obliczenia długości alfabetu - int alfabet_length = my_strlen(alfabet); +void bubbleSort(model arr[], int n) { + bool swapped; + for (int i = 0; i < n-1; i++) { + swapped = false; - for (int i = 0; i < alfabet_length; ++i) { - wynik[i] = 0; - } + for (int j = 0; j < n-i-1; j++) { + if (!compareWords(arr[j].ptr, arr[j].len, arr[j+1].ptr, arr[j+1].len)) { - for (int i = 0; i < alfabet_length; ++i) { - for (int j = 0; slowo[j] != '\0'; ++j) { - if (alfabet[i] == slowo[j]) { - wynik[i]++; + // Zamiana miejscami + model temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + swapped = true; } } + + if (!swapped) { + break; + } } -} - -// #include "myfunc.h" - -// const char* alfabet = "abcdefghijklmnopqrstuwxyz"; -// const char* slowo = "mpabi"; -// int wynik[26] = {0}; // Inicjalizacja tablicy wyników na 0 - -// count_characters(alfabet, slowo, wynik); +}