Ejemplo n.º 1
0
 /**
  * Recursive function to sort array of integers using the Quick Sort Algorithm.
  *
  * @param array Array of integers to sort.
  * @param begin The starting index in <code>array</code> of the range to be sorted.
  * @param end The ending index in <code>array</code> of the range to be sorted.
  */
 void sort(int[] array, int begin, int end) {
   int[] arraycpy = new int[array.length];
   for (int i = 0; i < array.length; i++) arraycpy[i] = array[i];
   if (begin < end) {
     // fibQuestion q = new fibQuestion(out, (new Integer(qIndex)).toString());
     // Remember that Animal wants quotes around the question text
     // q.setQuestionText("\"Which number in the array will be chosen as pivot index?\"");
     // questions.addQuestion(q);
     // out.println("{");
     // questions.animalInsertQuestion(qIndex);
     // out.println("}");
     int pivIndex = partition(array, begin, end);
     // After calling on partition, pivIndex is used to set the answer
     // for the question
     // And, remember that Animal wants quotes around the question answer
     // q.setAnswer("\"" + arraycpy[pivIndex] + "\"");
     // qIndex++;
     sort(array, begin, pivIndex - 1);
     sort(array, pivIndex + 1, end);
   } else {
     out.println("{");
     out.println("highlightArrayCell on \"qarray\" position " + begin);
     out.println("}");
   }
 }
Ejemplo n.º 2
0
  /**
   * Performs the section of the Quick Sort Algorithm which creates partitions.
   *
   * @param array The array of integers to be sorted.
   * @param first The first index in <code>array</code> of the range to be partitioned.
   * @param last The last index in <code>array</code> of the range to be partitioned.
   */
  int partition(int[] array, int first, int last) {
    int pivVal = array[first];
    int up = first;
    int down = last;
    int tmp;
    int lastIndex = array.length + 1;
    fibQuestion q = null;
    out.println("{");
    out.println("moveArrayIndex \"lo\" to position " + first);
    out.println("moveArrayIndex \"hi\" to position " + last);
    out.print("arraySwap on \"qarray\" position " + first + " with " + lastIndex);
    out.println(" within 500 ms");
    out.println("}");
    boolean question_at_end = false;
    while (up < down) {

      // The next two loops are "fakes", doing a preliminary pass to
      // see if we need to generate a question
      int up_dup = up;
      int down_dup = down;
      while ((array[up_dup] <= pivVal) && (up_dup < last)) up_dup++;
      while ((array[down_dup] > pivVal) && (down_dup > first)) down_dup--;

      if ((up_dup < down_dup)) {
        q = new fibQuestion(out, (new Integer(qIndex)).toString());
        // Remember that Animal wants quotes around the question text
        q.setQuestionText("\"Which numbers in the array will swap next?\"");
        questions.addQuestion(q);
        out.println("{");
        questions.animalInsertQuestion(qIndex);
        out.println("}");
      } else {
        question_at_end = true;
        q = new fibQuestion(out, (new Integer(qIndex)).toString());
        // Remember that Animal wants quotes around the question text
        q.setQuestionText("\"At what number in the array will the pointers meet?\"");
        questions.addQuestion(q);
        out.println("{");
        questions.animalInsertQuestion(qIndex);
        out.println("}");
      }

      while ((array[up] <= pivVal) && (up < last)) {
        up++;
        out.println("{");
        out.println("moveArrayIndex \"lo\" to position " + up);
        out.println("}");
      }
      while ((array[down] > pivVal) && (down > first)) {
        down--;
        out.println("{");
        out.println("moveArrayIndex \"hi\" to position " + down);
        out.println("}");
      }
      if (up < down) {
        // And, remember that Animal wants quotes around the question answer
        q.setAnswer(
            "\""
                + array[up]
                + " "
                + array[down]
                + "\""
                + "\n"
                + "\""
                + array[down]
                + " "
                + array[up]
                + "\"");
        qIndex++;
        tmp = array[up];
        array[up] = array[down];
        array[down] = tmp;
        out.println("{");
        out.print("arraySwap on \"qarray\" position " + up + " with " + down);
        out.println(" within 500 ms");
        out.println("}");
      }
    }
    // And, remember that Animal wants quotes around the question answer
    // There was no swap but the pointers met
    if (question_at_end) {
      q.setAnswer("\"" + array[up] + "\"");
      qIndex++;
    }

    if (first != down) {
      // And, remember that Animal wants quotes around the question answer
      // q.setAnswer("\"" + array[first] + " " + array[down] + "\"" + "\n"
      //	+ "\"" + array[down] + " " + array[first] + "\"");
      // qIndex++;
      tmp = array[first];
      array[first] = array[down];
      array[down] = tmp;
      out.println("{");
      out.print("arraySwap on \"qarray\" position " + first + " with " + down);
      out.println(" within 500 ms");
      out.println("}");
    }
    out.println("{");
    out.print("arraySwap on \"qarray\" position " + down + " with " + lastIndex);
    out.println(" within 500 ms");
    out.println("highlightArrayCell on \"qarray\" position " + down);
    out.println("}");
    return down;
  }