mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-25 14:59:36 +03:00
Merge pull request #225 from danghai/master
Add partition sort algorithm
This commit is contained in:
commit
8b9de23919
62
sorting/partitionSort.c
Normal file
62
sorting/partitionSort.c
Normal file
@ -0,0 +1,62 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void swap(int *a, int *b) {
|
||||
int tmp = *a;
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
|
||||
int partition(int arr[], int low, int high) {
|
||||
int pivot = arr[low];
|
||||
int i = low - 1, j = high + 1;
|
||||
|
||||
while(1) {
|
||||
/* Find leftmost element >= pivot */
|
||||
do {
|
||||
i++;
|
||||
} while(arr[i] < pivot);
|
||||
|
||||
/* Find rightmost element <= pivot */
|
||||
do {
|
||||
j--;
|
||||
} while(arr[j] > pivot);
|
||||
|
||||
/* if two pointers met */
|
||||
if (i >= j)
|
||||
return j;
|
||||
|
||||
swap(&arr[i], &arr[j]);
|
||||
}
|
||||
}
|
||||
|
||||
void partitionSort(int arr[], int low, int high) {
|
||||
if(low < high) {
|
||||
int value = partition(arr, low, high);
|
||||
partitionSort(arr, low, value);
|
||||
partitionSort(arr, value + 1, high);
|
||||
}
|
||||
}
|
||||
|
||||
void printArray(int arr[], int n) {
|
||||
int i;
|
||||
for(i = 0; i < n; i++)
|
||||
printf("%d ", arr[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
int arr[20];
|
||||
int i, range = 100;
|
||||
for(i = 0; i < 20; i++) {
|
||||
arr[i] = rand() % range + 1;
|
||||
}
|
||||
int size = sizeof arr / sizeof arr[0];
|
||||
printf("Array: \n");
|
||||
printArray(arr, size);
|
||||
partitionSort(arr, 0, size - 1);
|
||||
printf("Sorted Array: \n");
|
||||
printArray(arr, size);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user