Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.c
- * Author: Gerardo Cicalese
- *
- * Created on 18 novembre 2019, 14.22
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- typedef char Info;
- void quickSort(Info *, int);
- int partition(Info *, int);
- void swap(Info *, Info *);
- int compare(Info, Info);
- int main(void)
- {
- srand(time(NULL));
- Info array[10];
- for(int i=0; i<10; ++i)
- array[i] = 'a' + rand()%('z' - 'a' + 1);
- quickSort(array, 10);
- for(int i=0; i<10; ++i)
- printf("%c\t", array[i]);
- return 0;
- }
- void quickSort(Info * array, int len)
- {
- if(len <= 1)
- return;
- int pivot = partition(array, len);
- quickSort(array, pivot);
- quickSort(array+pivot+1, len-pivot-1);
- }
- int partition(Info * array, int len)
- {
- int i = 1, j = 1;
- for(; i < len; ++i)
- if(-1 == compare(array[i], array[0]))
- swap(array+i, array+j++);
- swap(array, array+j-1);
- return j-1;
- }
- void swap(Info * a, Info * b)
- {
- Info temp = *a;
- *a = *b;
- *b = temp;
- }
- int compare(Info a, Info b)
- {
- if(a == b)
- return 0;
- if(a > b)
- return 1;
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement