Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. void quicksort(int a[10],int first,int last, int k){
  6. int pivot,j,temp,i,aux,soma;
  7. for (i=0;i<k;i++){
  8. aux = rand()%a[i];
  9. soma=soma+aux;
  10. }
  11. soma=soma/k;
  12. pivot=soma;
  13. if(first<last){
  14. pivot=first;
  15. i=first;
  16. j=last;
  17.  
  18. while(i<j){
  19. while(a[i]<=a[pivot]&&i<last)
  20. i++;
  21. while(a[j]>a[pivot])
  22. j--;
  23. if(i<j){
  24. temp=a[i];
  25. a[i]=a[j];
  26. a[j]=temp;
  27. }
  28. }
  29.  
  30. temp=a[pivot];
  31. a[pivot]=a[j];
  32. a[j]=temp;
  33. quicksort(a,first,j-1,k);
  34. quicksort(a,j+1,last,k);
  35.  
  36. }
  37. }
  38.  
  39. int main(){
  40. int s,i,k;
  41. printf("Entre com o valor de k: \n");
  42. scanf("%d",&k);
  43. printf("Entre com o tamanho do vetor: \n");
  44. scanf("%d",&s);
  45.  
  46. int *a = (int *) malloc(1000000 * sizeof(int));
  47. srand(time(NULL));
  48.  
  49. for(i=0 ; i < s ; i++)
  50. a[i]=rand();
  51.  
  52. quicksort(a,0,s-1,k);
  53.  
  54. printf("Elementos ordenados: \n");
  55. for(i=0;i<s;i++)
  56. printf("%d\n",a[i]);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement