SHARE
TWEET

BubbleSort + Call by Ref CS311

Syndragonic Aug 25th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.                
  27. int main()
  28. {
  29.     cout<<"Bubble Sort\n";
  30.     int array[20]= {1,2,5,6,3,7,6,8,9,3,6,4,7,3,7,5,4,8,5,9};
  31.     int size = 20;
  32.      bubblesort(array, size);
  33.      for (int i = 0; i<20; i++)
  34.         {
  35.             cout<<array[i]<<" ";
  36.         }
  37.     cout<<"\n";
  38.     cout<<"\n";
  39.     cout<<"Call by Reference to Swap to Values\n";
  40.     int a = 10;
  41.     int b = 5;
  42.     cout<<"Before Swap "<<a<<" "<<b;
  43.     cout<<"\n";
  44.     swapnumbers (a,b);
  45.     cout<<"After Swap "<<a<<" "<<b;
  46.     cout<<"\n";
  47.      cout<<"\n";
  48.     cout<<"Call by Value to Swap but not actually Swap";
  49.    
  50.     return 0;
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top