Advertisement
Syndragonic

BubbleSort + Call by Ref + Call by Val CS311

Aug 25th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. void swapnumbers (int &, int &);
  4. void bubblesort (int arr[], int n)
  5. {
  6. int i, j;
  7. for(i=n-1; i>0; i--)
  8. {
  9. for (j=0; j<i;j++)
  10. {
  11. if (arr[j] > arr[j+1])
  12. {
  13. int temp= arr[j+1];
  14. arr[j+1]=arr[j];
  15. arr[j]=temp;
  16. }
  17. }
  18. }
  19. }
  20. void swapnumbers (int &A, int &B)
  21. {
  22. int temp = B;
  23. B = A;
  24. A = temp;
  25. }
  26. void swapval (int x, int y)
  27. {
  28. int temp = y;
  29. y = x;
  30. x = temp;
  31.  
  32. return;
  33. }
  34.  
  35. int main()
  36. {
  37. cout<<"Bubble Sort\n";
  38. int array[20]= {1,2,5,6,3,7,6,8,9,3,6,4,7,3,7,5,4,8,5,9};
  39. int size = 20;
  40. bubblesort(array, size);
  41. for (int i = 0; i<20; i++)
  42. {
  43. cout<<array[i]<<" ";
  44. }
  45. cout<<"\n";
  46. cout<<"\n";
  47. cout<<"Call by Reference to Swap to Values\n";
  48. int a = 10;
  49. int b = 5;
  50. cout<<"Before Swap "<<a<<" "<<b;
  51. cout<<"\n";
  52. swapnumbers (a,b);
  53. cout<<"After Swap "<<a<<" "<<b;
  54. cout<<"\n";
  55. cout<<"\n";
  56. cout<<"Call by Value to Swap but not actually Swap\n";
  57. int x = 20;
  58. int y = 10;
  59. cout<<"Before Swap "<<x<<" "<<y;
  60. cout<<"\n";
  61. swapnumbers (a,b);
  62. cout<<"After Swap "<<x<<" "<<y;
  63. cout<<"\n";
  64. cout<<"\n";
  65.  
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement