Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <time.h>
  4.  
  5. int main()
  6. {
  7.     int i;
  8.     int a[] = {5, 3, 4, 9, 1, 2};
  9.     int tam = ((int) (sizeof(a) / sizeof(a[0]))) - 1;
  10.     quicksort(a, 0, tam);
  11.     for(i = 0; i < tam+1; i++){
  12.     printf("%d ", a[i]);
  13.     }
  14. }
  15. void quicksort(int *a, int esq, int dir)
  16. {
  17.   int pivot, i, j, temp;
  18.   if(esq < dir) {
  19.     pivot = esq;
  20.     i = esq;
  21.     j = dir;
  22.     while(i < j) {
  23.  
  24.       while(a[i] <= a[pivot] && i <= dir)
  25.         i++;
  26.  
  27.       while(a[j] > a[pivot] && j >= esq)
  28.         j--;
  29.  
  30.       if(i < j) {
  31.         temp = a[i];
  32.         a[i] = a[j];
  33.         a[j] = temp;
  34.       }
  35.     }
  36.  
  37.     temp = a[j];
  38.     a[j] = a[pivot];
  39.     a[pivot] = temp;
  40.  
  41.     quicksort(a, esq, j-1);
  42.     quicksort(a, j+1, dir);
  43.   }
  44. }
  45.  
  46. int mediana(int tam){
  47.     srand ( time(NULL));
  48.     int v1, v2, v3;
  49.     v1 = 0;
  50.     v2 = 0;
  51.     v3 = 0;
  52.    
  53.     while (v1 == v2 || v1 == v3 || v2 == v3){
  54.         v1 = rand() % tam;
  55.         v2 = rand() % tam;
  56.         v3 = rand() % tam;
  57.     }
  58.    
  59.     int aux;
  60.     if (v1 > v2){
  61.         aux = v1;
  62.         v1 = v2;
  63.         v2 = aux;
  64.     }
  65.    
  66.     if (v1 > v3){
  67.         aux = v1;
  68.         v1 = v3;
  69.         v3 = aux;
  70.     }
  71.    
  72.     if(v2 > v3){
  73.         aux = v2;
  74.         v2 = v3;
  75.         v3 = aux;
  76.     }
  77.     printf("%d %d %d\n", v1,v2,v3);
  78.     return v2;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement