Advertisement
193030

Algorithms, Part 1 W1 Quick Find

Jul 21st, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. // Quick find
  2. // https://www.coursera.org/learn/algorithms-part1/lecture/EcF3P/quick-find
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. int array[10] = {0,1,2,3,4,5,6,7,8,9};
  7. int secondValue = 0;
  8. void unionN(int first, int second)
  9. {
  10.     secondValue = array[second];
  11.     array[second] = array[first];
  12.     for(int i =0; i<sizeof(array)/sizeof(array[0]); i++)
  13.     {
  14.        
  15.         if (array[i] == secondValue)
  16.         {
  17.             array[i] = array[first];
  18.         }
  19.        
  20.     }
  21.    
  22. }
  23. void printArray()
  24. {
  25.     for(int i =0; i<sizeof(array)/sizeof(array[0]); i++)
  26.     {
  27.         cout << "Index: " << i << " value: "<< array[i] << endl;
  28.     }
  29. }
  30. int main()
  31. {
  32.  unionN(1,2);
  33.  unionN(5,6);
  34.  unionN(1,5);
  35.  printArray();
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement