title: Bubble sort on a text file using the file itself as a container
body: <p>I am trying to write a bubble sort on a large text file that has 1 column of numbers that are all between 1 and 99.  I want to sort it without bringing the contents or the data in memory  at the same time. I used a loop of 20 passes to get the logic down before I do this on the whole file. I am using streamreader and streamwriter to receive and send the numbers back and forth so only 2 numbers are processed at any one time. the numbers in the text file are coded int32.I converted them in the writing process. I have been unable to populate the "sorted" file with the data.  Here is my code:</p>
  
  <pre><code> static void BubbleSort()
      { 
          int bubbleOut, bubbleIn; 
          StreamReader readToSort = new StreamReader(@"C:Random # File.txt");
          StreamWriter writeSorted = new StreamWriter(@"C:Sorted_File.txt");
          bubbleIn = readToSort.Read();
          bubbleOut = readToSort.Read();
  
          for (bubbleIn = 1; bubbleIn &lt;= 20; bubbleIn++)
          {
              for (bubbleOut = bubbleIn; bubbleIn &lt; bubbleOut; bubbleOut++ )
              {
                  if (bubbleOut &gt; bubbleIn)
                  {
                      int temp = bubbleIn;
                      bubbleIn = bubbleOut;
                      bubbleOut = temp;
                      writeSorted.WriteLine(bubbleIn);
                      writeSorted.WriteLine(bubbleOut);
                  }
              }
          }
  }
  </code></pre>