title: How to bubblesort a pointers in the array without changing order of the array
body: <p>So say I have two arrays  intdataArray[10] = {61 34 46 114 73 29 13 93} and int *pointerDataArray[10] which has the pointers of the dataArray in the same respective index. How would I be able to sort the array through pointers and get the sorted array but also while still being able to print the original array.</p>
  <p>My bubble sort is working but it is also changing dataArray as well.</p>
  <p>This is the wanted output(correct)</p>
  <blockquote>
  <p>dataArray before sort: 61 34 46 114 73 29 13 93<br />
  pointerArray before sort: 61 34 46 114 73 29 13 93<br />
  dataArray after sort: 61 34 46 114 73 29 13 93<br />
  pointerArray after sort: 13 29 34 46 61 73 93 114</p>
  </blockquote>
  <p>This is the output I am getting(wrong)</p>
  <blockquote>
  <p>dataArray before sort: 61 34 46 114 73 29 13 93<br />
  pointerArray before sort: 61 34 46 114 73 29 13 93<br />
  dataArray after sort: 13 29 34 46 61 73 93 114<br />
  pointerArray after sort: 13 29 34 46 61 73 93 114</p>
  </blockquote>
  <p>Here is my code</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  int readFile(string filename, int dataArray[10], int *pointerDataArray[10]);
  void pointerSort(int dataArray[],int *pointerDataArray[] , int length);
  void swapIntPtr(int *p1, int *p2);
  void displayDataArray(int dataArray[], int length);
  void displayPointerArray(int *pointerDataArray[], int length);
  
  int main() {
      int dataArray[10] = {};
      int *pointerDataArray[10];
  
      int length = readFile(&quot;arrayData.txt&quot;, dataArray, pointerDataArray);
  
      cout &lt;&lt; &quot;dataArray before sort: &quot;;
      displayDataArray(dataArray, length);
      cout &lt;&lt; &quot;pointerArray before sort: &quot;;
      displayPointerArray(pointerDataArray, length);
      cout &lt;&lt; endl;
  
      pointerSort(dataArray, pointerDataArray, length);
  
      cout &lt;&lt; &quot;dataArray after sort: &quot;;
      displayDataArray(dataArray, length);
      cout &lt;&lt; &quot;pointerArray after sort: &quot;;
      displayPointerArray(pointerDataArray, length);
  
  }
  
  int readFile(string filename, int dataArray[10], int *pointerDataArray[10]) {
      ifstream inputFile(filename);
      if (inputFile.is_open()) {
  
          int length;
          inputFile &gt;&gt; length;
          for (int i = 0; i &lt; length; i++) {
              inputFile &gt;&gt; dataArray[i];
              pointerDataArray[i] = &amp;dataArray[i];
          }
          return length;
      }
      return -1;
  }
  
  void pointerSort(int dataArray[],int *pointerDataArray[] , int length){
      int i, j;
      for (i = 0; i &lt; length-1; i++)
          for (j = 0; j &lt; length-i-1; j++)
              if (dataArray[j] &gt; dataArray[j+1]) {
                  swapIntPtr(&amp;dataArray[j], &amp;dataArray[j + 1]);
              }
  }
  
  void swapIntPtr(int *pointer1, int *pointer2) {
      int pSwap = *pointer1;
      *pointer1 = *pointer2;
      *pointer2 = pSwap;
  }
  
  void displayDataArray(int dataArray[], int length){
      for (int i = 0; i &lt; length; i++) {
          cout &lt;&lt; dataArray[i] &lt;&lt; &quot; &quot;;
      }
      cout &lt;&lt; endl;
  }
  
  void displayPointerArray(int *pointerDataArray[], int length){
      for (int i = 0; i &lt; length; i++) {
          cout &lt;&lt; *pointerDataArray[i] &lt;&lt; &quot; &quot;;
      }
  }
  </code></pre>