forked from menault/TD1_DEV51_Qualite_Algo
32 lines
724 B
C
32 lines
724 B
C
|
#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);
|
||
|
}
|
||
|
}
|