473,729 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting a list of integers in C

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.
Nov 14 '05 #1
16 3385
Use qsort()

aruna wrote:
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.


--
remove .spam from address to reply by e-mail.
Nov 14 '05 #2
ar********@yaho o.co.in (aruna) writes:
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.

Why do some College professors continue to set such unrealistic homework
questions!? Why don't students question the unrealistic nature of these
questions?

_______________ _______________ _______________ _______________ _______________ ____
Dr Chris McDonald EMAIL: ch***@csse.uwa. edu.au
School of Computer Science & Software Engineering
The University of Western Australia WWW: http://www.csse.uwa.edu.au/~chris
Crawley, Western Australia, 6009 PH: +61 8 6488 2533, FAX: +61 8 6488 1089
Nov 14 '05 #3
On 2004-04-24, James McIninch <ja************ @comcast.net.sp am> wrote:
[Top posting fixed by "AutoTopPostInc inerator X++ Professional Edition" ]
aruna wrote:
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.


Use qsort()


Right, but the other piece of the puzzle is how to absorb the input
without using a loop?

I suppose I could cheat with recursion like this:

#include <stdio.h>

int read_one_int(FI LE *fp, int *i)
{
int v = fscanf(fp, "%d", i);
return (v != 0 && v != EOF);
}

int read_all_ints(F ILE *fp, int *v, unsigned num)
{
return num && read_one_int(fp , v) && read_all_ints(f p, v+1, num-1);
}

-- James
Nov 14 '05 #4
Chris McDonald <ch***@csse.uwa .edu.au> writes:
ar********@yaho o.co.in (aruna) writes:
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.


Why do some College professors continue to set such unrealistic homework
questions!? Why don't students question the unrealistic nature of these
questions?


The point of such an exercise is to let the students learn about some
advanced construct. The best way to learn how to use something is to
try it out. A problem for which the construct is the natural solution
may be too complex for the students at their current level; applying
the construct to a simpler problem is actually sensible, even if
there's a simpler solution to the same problem.

When a student writes a sorting routine, it's not because he really
needs to sort a list of integers. Sorting a simple array of integers
isn't a realistic problem (in real life, it's more common to want to
sort a list of records using one field of each record as the key), but
it's a good way to teach sorting without bringing in too many other
concepts.

Having said that, I'm not sure what the problem description is aiming
for. The no-arrays restriction seems to imply linked lists, but the
restriction on if-then, switch-case, and loops is odd. (Recursion and
the "?:" operator, maybe?)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #5
On 24 Apr 2004 07:45:01 -0700, ar********@yaho o.co.in (aruna) wrote:
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>>
Nov 14 '05 #6
Keith Thompson posted the following:
Chris McDonald <ch***@csse.uwa .edu.au> writes:
ar********@yaho o.co.in (aruna) writes:
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.


Why do some College professors continue to set such unrealistic homework
questions!? Why don't students question the unrealistic nature of these
questions?


The point of such an exercise is to let the students learn about some
advanced construct. The best way to learn how to use something is to
try it out. A problem for which the construct is the natural solution
may be too complex for the students at their current level; applying
the construct to a simpler problem is actually sensible, even if
there's a simpler solution to the same problem.

When a student writes a sorting routine, it's not because he really
needs to sort a list of integers. Sorting a simple array of integers
isn't a realistic problem (in real life, it's more common to want to
sort a list of records using one field of each record as the key), but
it's a good way to teach sorting without bringing in too many other
concepts.

Having said that, I'm not sure what the problem description is aiming
for. The no-arrays restriction seems to imply linked lists, but the
restriction on if-then, switch-case, and loops is odd. (Recursion and
the "?:" operator, maybe?)


I think it's a stupid, stupid assignment.

If I were to attack it I would think of a tree, recursion and conditional
expressions. After all there isn't much left of the C "repertoire " after
all those savage removals. And even this is dependant on the instructor's
personal cherished meaning of "like" in clause b. Like is a sufficiently
nebulous word that this might pass muster.

This kind of thing belongs in a bar where programmers hang out after work,
not in a school.
Nov 14 '05 #7
aruna wrote:
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.


#include <stdlib.h>

int main(void) {
/* you might write code to create the argument for following line
with the appropriate substitutions for the file names and the
name of the sorting program */
system("sort <unsorted_file_ of_ints >sorted_file_of _ints");
return 0;
}
Nov 14 '05 #8
ar********@yaho o.co.in (aruna) writes:
b. Do not use any comparison function like if/then
or switch-case


You're in luck: `if', `switch', and `case' are statements, not
functions, and C doesn't even have `then'. This is obviously a
trick designed to let those who know proper C write a reasonable
program, but force others to deal with stupid restrictions.
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield
Nov 14 '05 #9
ar********@yaho o.co.in (aruna) writes:
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.


e. do not cheat with your homework
Wolfgang Denk

--
Software Engineering: Embedded and Realtime Systems, Embedded Linux
Phone: (+49)-8142-4596-87 Fax: (+49)-8142-4596-88 Web: www.denx.de
"Send lawyers, guns and money..." - Lyrics from a Warren Zevon song
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
4634
by: Paul | last post by:
Hi all I have a sorting problem, but my experience with Python is rather limited (3 days), so I am running this by the list first. I have a large database of 15GB, consisting of 10^8 entries of approximately 100 bytes each. I devised a relatively simple key map on my database, and I would like to order the database with respect to the key.
22
4162
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b) { return -1; } else
6
1723
by: Michel | last post by:
Hi All, I need to loop through a sorted datatable. Looping is not a problem, but sorting is. The datacolumns on which I want to sort, exists of numbers, which causes the 10 to appear before the 2. This is the code I use to sort the table
25
2222
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort function that will merge like elements into a chain, so that they won't have to be recompared as many times? Thanks!
16
2765
by: Kittyhawk | last post by:
I would like to sort an Arraylist of objects on multiple properties. For instance, I have a Sort Index property and an ID property (both integers). So, the results of my sort would look like this: sort index, id 1000,1 1000,2 1000,3 1001,1 1001,2
9
1979
by: robin9876 | last post by:
I have a list of integers (example sub set of data below), what is the best method of sorting these in code? 1, 872 5, 1283 8, 343 9, 123
3
6632
by: Harry Haller | last post by:
Hello, I want to implement a generic list which will be used to display 7 columns in a GridView. One should be able to sort, filter and page each of the 7 columns. Ideally the filter should be implemented simultaneously for multiple columns - but the data need only be sorted by a single column at a time. Sorting should be both ascending and descending. I'm currently using a DataView but it's far too slow, because there are a large...
5
3186
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort. First, the user is asked whether he/she wants to perform a search option, a sort operation, or exit the program. If...
4
5323
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file (Sorts.java): import java.util.*; /**
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.