Quicksort
Posté : dim. sept. 08, 2024 5:42 pm
Code : Tout sélectionner
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int partition (char* arr[], int low, int high) {
char* pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high- 1; j++) {
if (strcmp(arr[j], pivot) < 0) {
i++;
char* swap = arr[i];
arr[i] = arr[j];
arr[j] = swap;
}
}
char* swap = arr[i+1];
arr[i+1] = arr[high];
arr[high] = swap;
return (i + 1);
}
void quickSort(char* arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
char* arr[] = {"banane", "cerise", "abricot", "poire", "orange"};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
for (int i=0; i<n; i++)
printf("%s\n", arr[i]);
return 0;
}