Home
Locate
CS 101
CS 496
Login
Tutors
Marks
About
Send
Close
Add comments:
(status displays here)
Got it!
This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.
nbsp; Note: This appears on each machine/browser from which this site is accessed.
Insertion sort using goto statements
by RS
admin@ycp.powersoftwo.org
c
go
java
js
lua
py
vbs
rkt
scm
pro
1. Insertion sort using goto statements
2. Insert Sort without gotos
Here is the C code.
#include <stdio.h> int main() { int xList[] = {0, 5, 2, 11, 13, 3, 7}; // convert the C code to use only goto and if ... goto // use labels L10, L20, L30, etc., in order introduced in the code // no code improvements , just the transformations // start here int xLen = 6; int xPos; int xSortPos = 0; while (xSortPos != xLen) { xSortPos += 1; xList[0] = xList[xSortPos]; xPos = xSortPos; while (xList[xPos-1] < xList[0]) { xList[xPos] = xList[xPos-1]; xPos -= 1; } xList[xPos] = xList[0]; } printf("results:\n"); // end here xPos = 0; while (xPos != xLen) { xPos += 1; int xValue = xList[xPos]; printf("\n%d. %d", xPos, xValue); } printf("\n"); return 0; }
Here is the output of the C code.
results: 1. 13 2. 11 3. 7 4. 5 5. 3 6. 2
3. Insert Sort with gotos
Here is the C code.
#include <stdio.h> int main() { int xList[] = {0, 5, 2, 11, 13, 3, 7}; // convert the C code to use only goto and if ... goto // use labels L10, L20, L30, etc., in order introduced in the code // no code improvements , just the transformations // start here int xLen = 6; int xPos; int xSortPos = 0; L10: if (! (xSortPos != xLen)) goto L20; xSortPos += 1; xList[0] = xList[xSortPos]; xPos = xSortPos; L30: if (! (xList[xPos-1] < xList[0])) goto L40; xList[xPos] = xList[xPos-1]; xPos -= 1; goto L30; L40: xList[xPos] = xList[0]; goto L10; L20: printf("results:\n"); // end here xPos = 0; while (xPos != xLen) { xPos += 1; int xValue = xList[xPos]; printf("\n%d. %d", xPos, xValue); } printf("\n"); return 0; }
Here is the output of the C code.
results: 1. 13 2. 11 3. 7 4. 5 5. 3 6. 2
4. End of page
by RS
admin@ycp.powersoftwo.org