ArchiSys/TD/TD2/td2_lucas/util.c

53 lines
979 B
C
Raw Normal View History

2024-02-09 09:20:19 +01:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "defns.h"
char** read_strings(int n) {
char** elems = malloc(sizeof(char*)*n);
for (int i=0; i < n; i++) {
printf("Chaîne.%d : ", i+1);
scanf("%ms", &(elems[i]));
}
return elems;
}
void delete(char** elems, int n) {
for (int i=0; i < n; i++) {
free(elems[i]);
}
free(elems);
}
void print_strings(char** elems, int n) {
for (int i=0; i < n; i++) {
printf(" %s\n", elems[i]);
}
}
void selsort(char** begin, char** end) {
if (begin == end)
return;
char** mini = pos_min(begin, end);
swap(begin, mini);
return selsort(begin+1, end);
}
char** pos_min(char** begin, char** end) {
char** mini = begin;
for (char** ptr=begin; ptr < end; ptr++) {
mini = strcmp(mini[0], ptr[0]) > 0 ? ptr : mini;
}
return mini;
}
void swap(char** a, char** b) {
char* tmp = *a;
*a = *b;
*b = tmp;
}