// print the elapsed time in seconds (nanaoseconds/ 1 billion)
java.io.File out = new java.io.File("quickSortResults.txt");
// Create a PrintWriter output stream and link it to the File object
java.io.PrintWriter outfile = new java.io.PrintWriter(out);
int pivotIndex; // the index of the chosen pivot element
int pivot; // the value of the chosen pivot
int midIndex = startIndex; // boundary element between high and low sets
// select the center element in the set as the pivot by integer averaging
pivotIndex = (startIndex + endIndex) / 2;
pivot = a[pivotIndex];
// put the pivot at the end of the set so it is out of the way
swap(a, pivotIndex, endIndex);
// iterate the set, up to but not including last element
for (int i = startIndex; i < endIndex; i++) { // Start for (int i = startIndex; i < endIndex; i++)
// if a[i] is less than the pivot
if (a[i] < pivot) { // Start if (a[i] < pivot)
long startTime = System.nanoTime();
// put a[i] in the low half and increment current Index
swap(a, i, midIndex);
midIndex = midIndex + 1;
long endTime = System.nanoTime();
// calculate elapsed time in nanoseconds
long duration = endTime - startTime;
outfile.printf("%12.8f %n", (double) duration / 1000000000);
} // End if (a[i] < pivot)
} // End for (int i = startIndex; i < endIndex; i++)
outfile.close();
// partitioning complete -- move pivot from end to middle
swap(a, midIndex, endIndex);
// return index of pivot
return midIndex;```
#Problem writing to a txt file
3 messages · Page 1 of 1 (latest)