Skip to content

Commit eb870be

Browse files
committed
feature: new SelectionSort and function ranking ended
1 parent b8242a2 commit eb870be

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

libs/list/main.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,32 @@ unsigned char popElement(List* _list, void* store, const size_t sizeOfStore) {
121121
return 1;
122122
}
123123

124-
void sort(List* _list, int (*cmp)(const void* a, const void* b)) {
125-
// TODO
124+
void selectionSort(List* _list, int (*cmp)(const void* a, const void* b)) {
125+
void* aux;
126+
unsigned tam;
127+
List *minor, *iterator;
128+
129+
if (!(*_list)) return;
130+
131+
while ((*_list)->__next) {
132+
iterator = &(*_list)->__next;
133+
minor = _list;
134+
while (*iterator) {
135+
if (cmp((*minor)->__data, (*iterator)->__data) > 0) minor = iterator;
136+
iterator = &(*iterator)->__next;
137+
}
138+
if (*minor != *_list) {
139+
aux = (*_list)->__data;
140+
tam = (*_list)->__sizeOfData;
141+
142+
(*_list)->__data = (*minor)->__data;
143+
(*_list)->__sizeOfData = (*minor)->__sizeOfData;
144+
145+
(*minor)->__data = aux;
146+
(*minor)->__sizeOfData = tam;
147+
}
148+
_list = &(*_list)->__next;
149+
}
126150
}
127151

128152
void randomSort(List* _list) {
@@ -184,4 +208,4 @@ void map(List* _list, action _action, void* punt) {
184208
_action((*_list)->__data, punt);
185209
_list = &(*_list)->__next;
186210
};
187-
}
211+
}

libs/list/main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ unsigned char pushElement(List* _list, void* data, const size_t sizeOfData);
3333

3434
unsigned char popElement(List* _list, void* store, const size_t sizeOfStore);
3535

36-
void sort(List* _list, int (*cmp)(const void* a, const void* b));
36+
void selectionSort(List* _list, int (*cmp)(const void* a, const void* b));
3737

3838
void randomSort(List* _list);
3939

src/show-ranking/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ unsigned char showRanking(const Configuration* config) {
3434
return 0;
3535
}
3636

37-
sort(&players, &cmpPlayers);
37+
selectionSort(&players, &cmpPlayers);
3838

3939
playersLength = getLength(&players);
4040

src/show-ranking/utilities.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
int cmpPlayers(const void* playerA, const void* playerB) {
77
APIPlayer* _playerA = (APIPlayer*)playerA;
88
APIPlayer* _playerB = (APIPlayer*)playerB;
9-
return _playerA->points - _playerB->points;
9+
return _playerB->points - _playerA->points;
1010
}

0 commit comments

Comments
 (0)