Last active
February 28, 2024 18:04
-
-
Save Scherso/86d296063c214207b23d1862a7ad7a99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void swap(int* a, int* b) | |
{ | |
int temp = *a; | |
*a = *b; | |
*b = temp; | |
} | |
int partition(int arr[], int low, int high) | |
{ | |
int pivot = arr[high]; | |
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; | |
} | |
void sort(int arr[], int low, int high) | |
{ | |
if (low >= high || low < 0) | |
return; | |
int part = partition(arr, low, high); | |
sort(arr, low, part - 1); | |
sort(arr, part + 1, high); | |
} | |
int main() | |
{ | |
int arr[] = { 5, 2, 9, 3, 6, 1, 7 }; | |
sort(arr, 0, (*(&arr + 1) - arr) - 1); | |
for (int i = 0; i < (*(&arr + 1) - arr); ++i) | |
printf("%d ", arr[i]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment