Last active
March 5, 2018 05:08
-
-
Save sdocy/3665f924cfa629b0622315c17d9624c0 to your computer and use it in GitHub Desktop.
an example of an instrumented sorting algorithm for my Data Structures and Algorithms graphical tutorial prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Code instrumentation, these instructions update | |
// the text info displayed on the screen for the | |
// executing algorithm. | |
// | |
// walkThru.shine(n); - highlight the line of code at index 'n', turning off the prior highlighted line of code | |
// walkThru.setVar(n, "value"); - set the text for the variable at index 'n' to the specified value | |
// arrCtrl.initArray(array, array_size); - initialize the on-screen array using the specified array and array size | |
// arrCtrl.updateArrayElem(n, "value"); - set the on-sreen array value at index 'n' to the specified value | |
// arrCtrl.updateArrayTag(n, "value", m); - update the tag for the on-screen array, setting the index tag at 'n' to the specified value, and turning off the tag at index `m` | |
// while (walkThru.isPaused) yield return null; - if code walk thru is paused, wait here until it is unpaused | |
// yield return new WaitForSeconds(programWalkThru.walkDelay); - delay execution of the next line of algorithm code or instrumentation code so the user can follow along | |
// postProcessing(); - do whatever needs to be done after the algorithm has finished executing | |
// /**/ - precedes lines of the actual algorithm I am instrumenting | |
// | |
// void bSort3(int[] arr, int size) { | |
// int j, tmp, i = 0; | |
// bool sorted = false; | |
// | |
// while (!sorted) { | |
// sorted = true; | |
// for (j = 0; j < (size - 1 - i); j++) { | |
// if (arr[j] > arr[j + 1]) { | |
// tmp = arr[j]; | |
// arr[j] = arr[j + 1]; | |
// arr[j + 1] = tmp; | |
// sorted = false; | |
// } | |
// } | |
// i++; | |
// } | |
// } | |
// | |
IEnumerator bSort3(int[] arr, int size) { | |
int j, tmp, i = 0; | |
bool sorted = false; | |
// display function parameter variables | |
walkThru.shine(0); | |
walkThru.setVar(0, "size = " + size); | |
arrCtrl.initArray(arr, size); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
// display local variables | |
walkThru.shine(1); | |
walkThru.setVar(1, "j = undefined"); | |
walkThru.setVar(2, "tmp = undefined"); | |
walkThru.setVar(3, "i = " + i); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
// display local variables | |
walkThru.shine(2); | |
walkThru.setVar(4, "sorted = " + sorted); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
// need to initialize `j` internally here because it is used below to clear | |
// the array tag at the end of each for-loop iteration, just set it to 0 | |
// for first iteration since there is no tag to clear | |
j = 0; | |
// highlight while-loop | |
walkThru.shine(4); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
/**/while (!sorted) { | |
/**/sorted = true; | |
// update variable value | |
walkThru.shine(5); | |
walkThru.setVar(4, "sorted = " + sorted); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
// highlight for-loop, set value of counter variable | |
walkThru.shine(6); | |
walkThru.setVar(1, "j = 0"); | |
arrCtrl.updateArrayTag(0, "j", j); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
/**/for (j = 0; j < (size - 1 - i); j++) { | |
// add a comparison | |
addCompare(); | |
// highlight conditional | |
walkThru.shine(7); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
/**/if (arr[j] > arr[j + 1]) { | |
/**/tmp = arr[j]; | |
// update variable value | |
walkThru.shine(8); | |
walkThru.setVar(2, "tmp = " + tmp); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
/**/arr[j] = arr[j + 1]; | |
// update variable value | |
walkThru.shine(9); | |
arrCtrl.updateArrayElem(j, arr[j].ToString()); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
/**/arr[j + 1] = tmp; | |
// update variable value | |
walkThru.shine(10); | |
arrCtrl.updateArrayElem(j + 1, arr[j + 1].ToString()); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
/**/sorted = false; | |
// update variable value | |
walkThru.shine(11); | |
walkThru.setVar(4, "sorted = " + sorted); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
} | |
/**for**/ | |
// highlight for-loop, set value of counter variable | |
walkThru.shine(6); | |
walkThru.setVar(1, "j = " + (j + 1)); | |
arrCtrl.updateArrayTag(j + 1, "j", j); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
} | |
/**/i++; | |
// update variable value | |
walkThru.shine(14); | |
walkThru.setVar(3, "i = " + i); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
/**while**/ | |
// highlight while-loop | |
walkThru.shine(4); | |
while (walkThru.isPaused) yield return null; | |
yield return new WaitForSeconds(programWalkThru.walkDelay); | |
} | |
// highlight final closing bracket | |
walkThru.shine(15); | |
// wrap up algorithm execution | |
postProcessing(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment