Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursive Sort method issues
    primarykey
    data
    text
    <p>This is the working merge sort method. the thing is that i want to print on the screen the steps of the sort, and the final sorted result on a separate text file. Since it si recursive, it does the above several times more than needed. EDIT: how to write the final array on the text file only once?</p> <pre><code>import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class Merge { public static void Sort (LinkedList listIn, int size) throws Exception { String[] mArray = new String[size] ; String textContent = null ; File outputFile ; do { outputFile = new File("[Merge] Sorted Entries.txt") ; if(!outputFile.exists()) { outputFile.createNewFile (); System.out.println("Sorted file created.txt"); System.out.println(""); } else { System.out.println("File Updated."); } }while (!outputFile.exists()) ; //copy the list values in the array for (int i = 0 ; i &lt; size ; i++) { mArray [i] = listIn.get(i).printNode(); } mergeSort(mArray, 0, mArray.length-1) ; } public static &lt;T extends Comparable&lt;? super T&gt;&gt; void mergeSort(T[] array, int min, int max) throws Exception { T[] temp ; int index1 ; int left ; int right ; Stopwatch timer = new Stopwatch().start(); // if array is of size 1 if (min == max) return ; // find length and midpoint int size = max - min + 1 ; int pivot = (min + max) / 2 ; temp = (T[]) (new Comparable[size]) ; mergeSort(array, min, pivot) ; mergeSort(array, pivot + 1, max) ; for (index1 = 0 ; index1 &lt; size ; index1++) { temp[index1] = array[min + index1] ; } left = 0 ; right = pivot - min + 1 ; for (index1 = 0 ; index1 &lt; size ; index1++) { if (right &lt;= max - min) if (left &lt;= pivot - min) if (temp[left].compareTo(temp[right]) &gt; 0) array[index1 + min] = temp[right++] ; else array[index1 + min] = temp[left++] ; else array[index1 + min] = temp[right++] ; else array[index1 + min] = temp[left++] ; } timer.stop() ; } public static &lt;T extends Comparable&lt;? super T&gt;&gt; void output (T[] array, int index) { System.out.println(array[index] + " ") ; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload