added bubble sort alg.
This commit is contained in:
parent
a0f7585ae1
commit
d9e57e8ca0
|
@ -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)) {
|
||||
|
||||
// Zamiana miejscami
|
||||
model temp = arr[j];
|
||||
arr[j] = arr[j+1];
|
||||
arr[j+1] = temp;
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < alfabet_length; ++i) {
|
||||
for (int j = 0; slowo[j] != '\0'; ++j) {
|
||||
if (alfabet[i] == slowo[j]) {
|
||||
wynik[i]++;
|
||||
}
|
||||
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);
|
||||
|
|
Loading…
Reference in New Issue