Advertisement
bernardolansing

Algoritmo de ordenação

Apr 14th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void ordena(int array[], int len) {
  4.     int ord[len], i, j, menor = array[0], maior = array[0], indmenor = 0, indmaior = 0;
  5.  
  6.     for (i = 1; i < len; i++) {
  7.         if (array[i] > maior) {
  8.             maior = array[i];
  9.             indmaior = i;
  10.         }
  11.     }
  12.  
  13.     for (i = 0; i < len; i++) {
  14.        
  15.         for (j = 0; j < len; j++) {
  16.             if (array[j] < menor) {
  17.                 indmenor = j;
  18.                 menor = array[indmenor];
  19.             }
  20.         }
  21.  
  22.         ord[i] = menor;
  23.         array[indmenor] = maior + 1;
  24.         menor = maior + 1;
  25.     }
  26.  
  27.     for (i = 0; i < len; i++) printf("%i  ", ord[i]);
  28. }
  29.  
  30. int main() {
  31.     int vetor[] = {-2, 10, 15, 31, -32, 3};
  32.  
  33.     ordena(vetor, 6);
  34.  
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement