Given a set of integers, how to write a program in C
to sort these set of integers using C, given the following conditions
a. Do not use arrays
b. Do not use any comparison function like if/then
or switch-case
c. you can use pointers only
d. you cannot use any of the loops either.
If this is an accurate statement of the problem issued by the
instructor and if you had the option, I think many of us would
recommend you change instructors.
From a pedagogical point of view, others have already pointed out how
completely unrealistic it is. We never hear of structural engineering
students being asked to rebuild stonehenge or the pyramids without the
use of ramps and pulleys. The fact that someone was able to solve the
problem umpteen thousand years ago does not mean there is any
educational benefit in redoing it.
From a technical point of view:
Someone should tell your instructor that if and switch are not
functions but statements.
Since pointers must point somewhere to be valid and useful, the
program must contain some variables of a type other than pointer
unless every pointer points to a dynamically allocated area.
Since you are writing a complete program and not just a function,
you must have a main. Since you will be given the values to be sorted
they must be passed to main from the command line. In order for main
to receive these values it must have two parameters, the first of
which must be an int. You now have something other than a pointer
variable.
There are no standard integer comparison functions. There are
comparison operators that accept integers and at least three
non-integer comparison functions. Which are you forbidden to use, the
operators or the functions? Does your instructor consider the ?:
operator to be one of the forbidden ones?
Since you cannot use do, for, while, if, or switch, I do not see how
you can handle an unknown quantity (determined at run time) of
integers. If the quantity of integers is known at coding time, some
approaches come to mind:
Dynamically allocate an area of memory capable of holding the
quantity of integers. Transfer the values to this area, possibly by
using memcpy(), strtod(), one of the functions in the scanf family,
fread, etc, depending on how they are given to you. Finally call
qsort to sort the values in what looks like an array but was never
declared as one. For the comparison function, simply subtract one
value from the other. qsort requires only that the sign of the return
value indicate the relative ordering.
Convert each integer value to a dynamically allocated "array" of
strings (remember to include leading zeros). Call qsort. For the
compare function, simply adjust the pointer types and call strcmp.
Use a nested sequence of ?: operators. This is definitely not
good style but for values a, b, and c
a > b?
(a > c ?
(b > c ?
(x = a, y = b, z = c) :
(x = a, y = c, z = b)) :
(x = c, y = a, z = b)) :
(b > c ?
(a > c ?
(x = b, y = a, z = c) :
(x = b, y = c, z = a)) :
(x = c, y = b, z = a))
produces x, y, and z in order.
Questions like these come up rather frequently. If you google the
archives with a sufficiently clever search parameter you may come up
with some better previous suggestions.
<<Remove the del for email>>