Advertisement
delvinkrasniqi

InsertionSort()

Jan 24th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. //InsertionSort
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. void InsertionMax(int A[],int size)
  7. {
  8. for (int i=0;i<size-1;i++)
  9. {
  10. int j=i+1;
  11. int tmp=A[j];
  12.  
  13. while (j>0 && tmp>A[j-1])
  14. {
  15. A[j]=A[j-1];
  16. j--;
  17. }
  18. A[j]=tmp;
  19. }
  20. }
  21.  
  22. void InsertionMin(int A[],int size)
  23. {
  24. for (int i=0;i<size-1;i++)
  25. {
  26. int j=i+1;
  27. int tmp=A[j];
  28.  
  29. while (j>0 &&tmp<A[j-1])
  30. {
  31. A[j]=A[j-1];
  32. j--;
  33. }
  34. A[j]=tmp;
  35. }
  36. }
  37.  
  38. void Print (int A[],int size)
  39. {
  40. for (int i=0;i<size;i++)
  41. {
  42. cout <<A[i]<<endl;
  43. }
  44. }
  45.  
  46. int main()
  47. {
  48. int A[]={1,8,2,55,4,0} , i , j , k ,tmp , size;
  49.  
  50. size=sizeof(A)/sizeof(int);
  51.  
  52. cout <<"Vargu fillestar : "<<endl;
  53. Print (A,size);
  54.  
  55. InsertionMin(A ,size);
  56. cout << "Vargu Minimal : " << endl;
  57. Print (A,size);
  58.  
  59. InsertionMax(A,size);
  60. cout <<"Vargu Maximal : " << endl;
  61. Print (A,size);
  62.  
  63. return 0;
  64.  
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement