Beijing World Youth Academy Computer Science
Sorting Double Arrays
In some programming cases, we need to sort items in a data structure according to some criteria
that we specify, such as values in a different structure. The good news is that if we know how to
sort an array based on its own values, it's fairly easy to sort it based on other values. All we do is
ignore the array we are given and sort the other values, but whenever we make a change to the
order of the other values, we make the same change to the given array. In this lab we're going to
explore this idea further by sorting an array of strings according to the number of characters in the
strings rather than the strings themselves.
Setup
Update defs.py in your project, Sorting, so that it contains the following.
defs.py
from ibdp_classes import Array
def STRING_LENGTHS(xs: Array[str]) -> Array[int]:
return Array(*[len(x) for x in xs._elements])
This de nes a function we can use in our pseudocode called STRING_LENGTHS that accepts an
array of strings and returns an array of integers containing how many characters are in the
respective strings. For example, create a le called test.pseudo that contains the following
pseudocode.
test.pseudo
WORDS = new Array(
"ball", "apple", "cat", "google", "doodle", "egg",
"fritter", "kangaroo", "jig", "igloo", "hi"
)
output STRING_LENGTHS(WORDS)
Now type the following into the terminal:
python -m ibdp_classes -defs defs.py test.pseudo
You should see the following output, which contains the number of characters in the respective
strings in WORDS, generated:
Array { 4, 5, 3, 6, 6, 3, 7, 8, 3, 5, 2 }
Computer Lab Sorting Double Arrays Page 1 of 3

fi

fi
Beijing World Youth Academy Computer Science
Task
Design a procedure with signature SORT_ON_LENGTH(XS, N), where XS is an array of strings and
N is the length of XS, that sorts the strings in XS from shortest to longest. You may use the
de ned function STRING_LENGTHS. Use either bubble sort or insertion sort in the design, and
add a comment to the pseudocode that states which algorithm you have chosen.
Assessment
This lab will not be formally assessed. Instead, you will share your algorithm design with the class
for informal, verbal feedback from your peers.
Computer Lab Sorting Double Arrays Page 2 of 3
fi


Beijing World Youth Academy Computer Science
Notes on Sorting in Python
Python has built in functionality to sort items in a list, either according to the items values
themselves or according to some other speci cation. For example, if we have de ned the list
words as in this lab:
words = [
"ball",
"apple",
"cat",
"google",
"doodle",
"egg",
"fritter",
"kangaroo",
"jig",
"igloo",
"hi",
]
We can sort the words (alphabetically) according to the values in the list:
print("Sort according to the value of the string:")
for i, word in enumerate(sorted(words)):
print(f"{i + 1}. {word}")
Or we can sort the words according to a speci cation passed in as “key”, which in this case is the
length of the string:
print("Sort according to the length of the string:")
for i, word in enumerate(sorted(words, key=lambda word: len(word))):
print(f"{i + 1}. {word}")
Try it out to see the difference!
Computer Lab Sorting Double Arrays Page 3 of 3


fi
fi
fi