Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int *vetor,*aux,i,tam,elm;
  6. printf("Insira o tamanho do vetor\n");
  7. scanf("%i",&tam);
  8. vetor= (int*)malloc(tam*sizeof(int));
  9. aux= (int*)malloc(tam*sizeof(int));
  10. BotarElementos(vetor,tam);
  11. InsertSort(vetor,tam,aux);
  12.  
  13. printf("\nVETOR ORIGINAL\n:");
  14. for (i=0;i<tam;i++)
  15. {
  16. printf("%i\t",vetor[i]);
  17. }
  18.  
  19. printf("\nVETOR AUXILIAR\n:");
  20. for (i=0;i<tam;i++)
  21. {
  22. printf("%i\t",aux[i]);
  23. }
  24. }
  25. int BotarElementos(int *vetor,int tam)
  26. {
  27. int i;
  28. for (i=0; i<tam; i++)
  29. {
  30. printf("Insira o elemento [%i] do vetor:",i);
  31. scanf("%i",&vetor[i]);
  32. }
  33.  
  34. }
  35. int InsertSort(int *vetor, int tam,int *aux){
  36. int i,flag,k;
  37. aux[0]=vetor[0];
  38. for (i=1;i<tam;i++)
  39. {
  40.  
  41. k=i-1;
  42.  
  43. if (vetor[i]<aux[i-1])
  44. {
  45. aux[i]=aux[i-1];
  46. aux[i-1]=vetor[i];
  47.  
  48. while(k>0 && vetor[i]<aux[k-1])
  49. {
  50. aux[k]=aux[k-1];
  51. aux[k-1]=vetor[i];
  52.  
  53. k=k-1;
  54. }
  55. }
  56. else
  57. {
  58. aux[i]=vetor[i];
  59. }
  60. }
  61.  
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement