Beispiel #1
0
    /* Creates a specific sorting case as an exercise for the user
     * - Since the elements have already been instantiated -> sorting values and hands them out again
     */
    public void ElementsBasedOnCase(GameObject[] sortingElements, string sortingCase)
    {
        // First gathers the values of the elements (which has been instantiated)
        int[] values = new int[sortingElements.Length];
        for (int x = 0; x < sortingElements.Length; x++)
        {
            values[x] = sortingElements[x].GetComponent <SortingElementBase>().Value;
        }

        // Sort these values (using insertion sort)
        switch (sortingCase)
        {
        case UtilSort.BEST_CASE: values = InsertionSort.InsertionSortFixCase(values, false); break;

        case UtilSort.WORST_CASE: values = InsertionSort.InsertionSortFixCase(values, true); break;

        default: Debug.LogError("Case '" + sortingCase + "' not added."); break;
        }

        // Change the values of the instantiated elements
        for (int x = 0; x < sortingElements.Length; x++)
        {
            sortingElements[x].GetComponent <SortingElementBase>().Value = values[x];
        }
    }
Beispiel #2
0
    public override Dictionary <int, InstructionBase> UserTestInstructions(InstructionBase[] sortingElements)
    {
        Dictionary <int, InstructionBase> instructions = new Dictionary <int, InstructionBase>();
        int instNr = 0;

        // Line 0 (set parameter)
        instructions.Add(instNr, new InstructionBase(Util.FIRST_INSTRUCTION, instNr++));

        // Create buckets
        Vector3[] pos = new Vector3[1] {
            bucketManager.FirstBucketPosition
        };
        int numberOfBuckets = bucketSortManager.NumberOfBuckets;

        bucketManager.CreateObjects(numberOfBuckets, pos);

        // Line 1 (Create buckets)
        instructions.Add(instNr, new InstructionBase(UtilSort.CREATE_BUCKETS_INST, instNr++));

        int x;

        // Add elements to buckets
        for (x = 0; x < sortingElements.Length; x++)
        {
            // Line 2 (Update for-loop)
            instructions.Add(instNr, new InstructionLoop(UtilSort.FIRST_LOOP, instNr++, x, Util.NO_VALUE, Util.NO_VALUE)); // TODO: create one unique instruction for each loop

            // Get element
            BucketSortInstruction element = (BucketSortInstruction)sortingElements[x];
            int bucketIndex = BucketIndex(element.Value, numberOfBuckets, maxValue);

            // Line 3 (Display bucket index)
            instructions.Add(instNr, new BucketSortInstruction(UtilSort.BUCKET_INDEX_INST, instNr++, x, Util.NO_VALUE, Util.NO_VALUE, element.SortingElementID, element.Value, true, false, element.HolderID, UtilSort.NO_DESTINATION, bucketIndex));

            // Line 4 (Put element into bucket)
            instructions.Add(instNr, new BucketSortInstruction(UtilSort.MOVE_TO_BUCKET_INST, instNr++, x, Util.NO_VALUE, Util.NO_VALUE, element.SortingElementID, element.Value, false, false, element.HolderID, UtilSort.NO_DESTINATION, bucketIndex));
        }
        // Line 2: condition
        instructions.Add(instNr, new InstructionLoop(UtilSort.FIRST_LOOP, instNr++, x, Util.NO_VALUE, Util.NO_VALUE));

        // Line 5 (end for-loop)
        instructions.Add(instNr, new InstructionLoop(UtilSort.END_LOOP_INST, instNr++, Util.NO_VALUE, Util.NO_VALUE, Util.NO_VALUE, UtilSort.OUTER_LOOP));

        // Line 6 (make the buckets sort what they hold)
        instructions.Add(instNr, new InstructionBase(UtilSort.PHASING_INST, instNr++));

        // Sorting elements
        int[] values = new int[sortingElements.Length];
        for (int y = 0; y < sortingElements.Length; y++)
        {
            values[y] = ((BucketSortInstruction)sortingElements[y]).Value;
        }
        int[] sorted = InsertionSort.InsertionSortFixCase(values, false);

        // Creating fictionary buckets
        Dictionary <int, List <BucketSortInstruction> > buckets = new Dictionary <int, List <BucketSortInstruction> >();

        for (int y = 0; y < numberOfBuckets; y++)
        {
            buckets[y] = new List <BucketSortInstruction>();
        }

        // Look for correct value and add element to bucket
        for (int r = 0; r < sorted.Length; r++)
        {
            for (int s = 0; s < sortingElements.Length; s++)
            {
                BucketSortInstruction t = (BucketSortInstruction)sortingElements[s];
                if (sorted[r] == t.Value)
                {
                    int bucketIndex = BucketIndex(t.Value, bucketSortManager.NumberOfBuckets, maxValue);
                    BucketSortInstruction displayInstruction = new BucketSortInstruction(UtilSort.DISPLAY_ELEMENT, instNr, Util.NO_VALUE, Util.NO_VALUE, Util.NO_VALUE, t.SortingElementID, t.Value, false, true, Util.NO_VALUE, UtilSort.NO_DESTINATION, bucketIndex);
                    instructions.Add(instNr++, displayInstruction);
                    buckets[bucketIndex].Add(displayInstruction);
                    break;
                }
            }
        }

        int i, j, k = 0;

        // Line 7 (For-loop: Concatenate all buckets)
        instructions.Add(instNr, new InstructionBase(Util.SET_VAR_K, instNr++));

        // Holder positions (where the sorting elements initialized)
        Vector3[] holderPos = sortMain.HolderManager.GetHolderPositions();
        for (i = 0; i < numberOfBuckets; i++)
        {
            List <BucketSortInstruction> bucket = buckets[i];
            int numberOfElementsInBucket        = bucket.Count;

            // Line 8 (For-loop: Concatenate all buckets)
            instructions.Add(instNr, new InstructionLoop(UtilSort.UPDATE_LOOP_INST, instNr++, i, numberOfBuckets, k, UtilSort.OUTER_LOOP));

            for (j = 0; j < numberOfElementsInBucket; j++)
            {
                // Line 9 (2nd For-loop: Concatenate all buckets)
                instructions.Add(instNr, new InstructionLoop(UtilSort.UPDATE_LOOP_INST, instNr++, i, j, numberOfElementsInBucket, UtilSort.INNER_LOOP));

                // Line 10 (Put element back into list)
                instructions.Add(instNr, new BucketSortInstruction(UtilSort.MOVE_BACK_INST, instNr++, i, j, k, bucket[j].SortingElementID, bucket[j].Value, false, true, Util.NO_VALUE, k, bucket[j].BucketID));

                // Line 11 (Update k)
                instructions.Add(instNr, new InstructionLoop(Util.UPDATE_VAR_K, instNr++, i, j, k));
                k++;
            }
            // Line 9: condition
            instructions.Add(instNr, new InstructionLoop(UtilSort.UPDATE_LOOP_INST, instNr++, i, j, numberOfElementsInBucket, UtilSort.INNER_LOOP));
            // Line 12 (2nd for-loop end)
            instructions.Add(instNr, new InstructionLoop(UtilSort.END_LOOP_INST, instNr++, i, Util.NO_VALUE, k, UtilSort.INNER_LOOP));
        }
        // Line 8: condition
        instructions.Add(instNr, new InstructionLoop(UtilSort.UPDATE_LOOP_INST, instNr++, i, numberOfBuckets, k, UtilSort.OUTER_LOOP));
        // Line 13 (2nd for-loop end)
        instructions.Add(instNr, new InstructionBase(Util.FINAL_INSTRUCTION, instNr++));

        return(instructions);
    }