Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // sortowanie tablicy za pomoca algorytmu quicksort
- #include <stdio.h>
- void swapValues(int *ElementA,int *ElementB);
- void quicksort(int t[], int p, int q);
- int partition(int t[], int p, int q);
- void displayArray(int t[],int size)
- {
- printf("\n");
- for(int i = 0; i < size; i ++)
- {
- printf("%d\t",t[i]);
- }
- }
- int main ( void )
- {
- int t[] = {4,3,2,-5,0,-1,-9,8,2,7};
- displayArray(t, 10);
- quicksort(t,0,10);
- displayArray(t, 10);
- getchar();
- return 0;
- }
- void quicksort(int t[], int p, int q)
- {
- int r;
- if( p < q )
- {
- r = partition(t,p,q);
- quicksort(t,p,r);
- quicksort(t,r+1,q);
- }
- }
- int partition(int t[], int p, int q)
- {
- int x = t[p];
- int i = p, j;
- for(j = p + 1; j < q; j++)
- {
- if(t[j] <= x)
- {
- i++;
- swapValues(&t[j],&t[i]);
- }
- }
- swapValues(&t[i],&t[p]);
- return i;
- }
- void swapValues(int *ElementA,int *ElementB)
- {
- int temp = *ElementA;
- *ElementA = *ElementB;
- *ElementB = temp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement