Exercise 6: Searching and Sorting
Prerequisite
You have completed Exercise 5.
Learning Outcomes
Be comfortable studying properties in the input and apply them to efficiently solve problems.
Deadline
This exercise is part of the CS1010 formative assessment. Submit your solution before 30 October 2023, 23:59 to receive feedback and earn your achievement "badges".
Acceptance Link
The link to accept the exercise is not available publicly. Visit Canvas for the link.
Concepts and Difficulty
Question 1: Sort
A v-array is an array of integers with a special property. The array can be partitioned into two segments: in the first segment, the numbers are sorted in non-ascending order. In the second part, the numbers are sorted in non-descending order. For example, 9 4 2 5 5 8
is a v-array. 9 4
is in non-ascending order, while 2 5 5 8
are in non-descending order. On the other hand, 9 4 5 2 5 8
is not a v-array.
A v-array can be only non-ascending or non-descending. For instance, both 1 2 3
, 8 8 8 8
, and -2 -5 -10
are all valid v-arrays.
Write a program sort
, that reads from standard input:
- a positive integer
, - followed by
numbers that form a v-array.
The program sort
then sorts the numbers in the v-array in non-descending order and prints the numbers to the standard output, one number per line.
You must solve this problem with an
Sample Runs
1 2 3 4 5 6 7 8 9 |
|
Question 2: Valley
A strict v-array is a v-array where no two adjacent elements have the same value.
In this problem, we wish to find the minimum value of a strict v-array, known as a valley.
Write a program valley
, that reads from standard input:
- a positive integer
, - followed by
numbers that form a strict v-array.
The program valley
then prints the valley of the array to the standard output.
You must solve this problem with an
Sample Runs
1 2 3 4 5 |
|
Question 3: Inversion
In an array that is supposed to be sorted in some order, an inversion is a pair of numbers that is out of order. For instance, if we want to sort the array "1 3 4 2", in increasing order, the pair (3, 2) and (4, 2) are out of order. So there are two inversions in this array.
An array that is sorted has no inversion, while an array of size
The concept of inversion is not new. In the problem kendall
, you have counted the number of inversions. In bubble sort, every pass removes some number of inversions until no more inversion is left.
In this question, you are given an array of
Write a program inversion
that reads, from the standard input, the following:
- An integer
, followed by integers
You program should then prints, to the standard output, the number of inversions in the input.
You have already solved this problem in kendall
in
Sample Runs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Question 4: Lexicon
Given a lexicon consisting of words that are made up of a set of symbols, we wish to sort them in increasing lexicographical order. Assume that the order of two symbols is defined, the ordering of two words is defined as follows:
-
Given two words
and of equal length. Let be the smallest index where the two words differ, i.e., for and . Then if . -
Given two words
and of different length, with , then the ordering pads with a special symbol that is smaller than every other symbols, until both and are of equal length.
For this question, we consider the set of printable ASCII characters, except the white space ' '
, as the set of symbols in our lexicon. The ordering of two symbols is defined by the ordering of the ASCII value of the characters. We can thus use the null character `'\0', with an ASCII value of 0, as the padding symbols.
For instance, the word ooi
is smaller than oops
. The first symbol that differs is at position 2 (the first letter is position 0) and the ASCII code for i
is smaller than p
.
As another example, given ooi
and ooink
, the ordering pad ooi
with the null character at position 3, to give the order ooi
< ooink
.
Write a program lexicon
, that reads from standard input:
- a positive integer
, - followed by
words
Print, to the standard output,
- the list of words in lexicographical order, one word per line.
Suppose that
Solving this in
Sample Runs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|