Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define ILOSC_NUMBEROW 10
- typedef int int_array_type[ILOSC_NUMBEROW];
- void fill_array(int_array_type array)
- {
- int i;
- for(i = 0; i < sizeof(int_array_type)/sizeof(array[0]); i++)
- array[i] = rand() % 100;
- }
- void print_array(int_array_type array)
- {
- int i;
- for(i = 0; i < sizeof(int_array_type)/sizeof(array[0]); i++)
- printf("%d ", array[i]);
- printf("\n");
- }
- void swap(int *first, int *second)
- {
- int tmp = *first;
- *first = *second;
- *second = tmp;
- }
- int partition(int_array_type array, int low, int high)
- {
- int pivot = array[low];
- int i = low - 1;
- int j = high + 1;
- while(i < j){
- while(array[--j] > pivot)
- ;
- while(array[++i] < pivot)
- ;
- if(i < j)
- swap(&array[i], &array[j]);
- }
- return j;
- }
- void quicksort(int_array_type array, int low, int high)
- {
- if(low < high){
- int partition_index = partition(array, low, high);
- quicksort(array, low, partition_index);
- quicksort(array, partition_index+1, high);
- }
- }
- int main(void)
- {
- srand(time(NULL));
- int_array_type array;
- fill_array(array);
- print_array(array);
- quicksort(array, 0, sizeof(int_array_type)/sizeof(array[0])-1);
- print_array(array);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement