Files
TD1_DEV51_Qualite_Algo/quicksort.c

32 lines
724 B
C
Raw Permalink Normal View History

2025-09-10 16:50:05 +02:00
#include <stdio.h>
// Fonction pour échanger deux entiers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Partition du tableau
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Choisir le dernier élément comme pivot
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
// Fonction récursive QuickSort
void quicksort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}