Advertisement
domdealm

Bubble Sort + MALLOC

Mar 2nd, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. void bubble(int vetor[],int n);
  5.  
  6. int main(){
  7. int *vec,size,count;
  8. printf("insira o tamanho do vetor a ser ordenado: ");
  9. scanf("%d",&size);
  10. vec = (int *) malloc(sizeof(int)*size);
  11. for (count =0;count < size; count ++){
  12.     printf("\nDigite a posição %d do vetor: ",count+1);
  13.     scanf("%d",&vec[count]);
  14. }
  15. printf("\nVETOR DESORDENADO: \n");
  16. for (count =0;count < size; count ++){
  17.     printf("\nvec[%d]: %d",count+1,vec[count]);
  18. }
  19. bubble(vec,size);
  20. printf("\nVETOR ORDENADO: \n");
  21. for (count =0;count < size; count ++){
  22.     printf("\nvec[%d]: %d",count+1,vec[count]);
  23. }
  24. return 0;
  25. }
  26.  
  27. void bubble(int vetor[],int n){
  28. int k = n-1,aux,i,j;
  29. for(i=0;i<n;i++){
  30.         for(j=0;j<k;j++){
  31.             if(vetor[j] > vetor[j+1]){
  32.                 aux = vetor[j];
  33.                 vetor[j] = vetor [j+1];
  34.                 vetor[j+1] = aux;
  35.             }
  36.     }
  37.     k--;
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement