473,757 Members | 9,145 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
16 3390

"aruna" <ar********@yah oo.co.in> wrote in message
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.

You need to find out what your instructor is driving at. For instance is a
pointer to malloced memory allowed instead of an array? In a real program
you would only rarely have a fixed-length array to sort.

The second question is, is qsort() allowed? In a hosted, ie standard,
environment this is how a C programmer would naturally tackle the problem.
Of course if the problem is to implement qsort() itself, then this is
cheating.
qsort takes a function pointer which returns less than zero, zero, or
greater than zero. Integers thus have the quirk that no conditional jump is
needed in the comparison function.
I don't understand the loop restriction, since the obvious way to test that
the sort has worked is to loop through it printing out values. It is also
hard to write a sane sort routine that doesn't use loops at some stage,
though it is probably possible to do so.
Nov 14 '05 #11
kal
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.


WARNING: NO WARRANTY OF ANY KIND IS IMPLIED OR PROVIDED.
CODE GIVEN HEREIN HAS NOT BEEN TESTED IN ANY WAY WHATSOEVER.

Seems to be a reasonable problem but I am not sure if you have
stated the problem as given to you. The mischief seems to be
in two places.

(1) "Given a set of integers" is ok for math but HOW are these
integers _given_ to the program? One assumes that it is a finite
set. If so is the code to be written for one particular cardinality?

It is dangerous to use big words like "set." It is often the case
that _interesting_ programs that one thinks of are often very
difficult to state precisely.

(2) Does condition (b) aplly to all parts of the code or for
comparing the integers only.

The task seems to involve pointers, linked lists, recursion and
one mathematical trivia. The trivia is: given two integers,
a and b, how does one find which is lesser and which is greater,
if any. The following code stores the lesser (or equal) integer
in 'a' and the greater (or equal) in 'b'.

int a, b;
long x, y;

x = a + b;
y = abs(a - b);
a = (x - y) / 2;
b = (x + y) / 2;

Now, to sort a finite set of integers whose cardinality is known
apriori (another big word for you) all one has to do is implement
the bubble sort alogorithm but unroll the loops.

May be someone else with a better knowledge of C will post as to
how to do this without unrolling the loops.

Hope this helps.

P.S. If one is using C++ then one can make use of the try - catch
mechanism to detect end conditions, such as end of file, and thus
avoid having to unroll the read loop or the bubble sort loop.
Nov 14 '05 #12
On Sat, 24 Apr 2004 15:30:17 +0000 (UTC), RE: Re: Sorting a list of
integers in C Chris McDonald <ch***@csse.uwa .edu.au> wrote:
ar********@yah oo.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?


An exercise in pointlessness?

"...We don't learn that much subject matter in school anyway in
proportion to the huge part of our lives that we spend there. But what
we do learn very well, thanks to the method, is to accept choices that
have been made for us. Which rule they make you follow is less
important than the fact that there are rules. I hear about English
teachers who won't allow their students to begin a sentence with
"and." Or about high schools where the male students are not permitted
to wear a T- shirt unless it has a pocket. I no longer dismiss such
rules as merely pointless. The very point to such rules is their
pointlessness.. ." Jerry Farber, 1969
--
To reply to me directly, remove the XXX characters from my email address.
Nov 14 '05 #13
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.


the only way that I see without cheat (array of struct or array of
pointers) is save the numbers in the input args of a recursive
functions.
Nov 14 '05 #14

In article <a5************ **************@ posting.google. com>, k_*****@yahoo.c om (kal) writes:

May be someone else with a better knowledge of C will post as to
how to do this without unrolling the loops.


An iterative algorithm can always be transformed into a recursive
algorithm and vice versa.

It's easy to see how to implement a selection sort, for example, on a
linked list using recursion. Set a "current-smallest" variable to
the value of the first node in the list. Recurse on the remainder of
the list, passing a pointer to the current-smallest. On entry to the
recursion function check that the value of the head of the list is
not smaller than the value pointed to by current- smallest; if it is,
set current-smallest to the new value.

When the first recursive pass ends, emit current-smallest. Then
recurse through the list again and remove the first node you
encounter that has the value you just emitted.

Now the top-level function calls itself again on the modified list.
The entire process will continue recursively until the list is empty
(the top-level function checks this as its end condition).

--
Michael Wojcik mi************@ microfocus.com

Painful lark, labouring to rise!
The solemn mallet says:
In the grave's slot
he lies. We rot. -- Basil Bunting
Nov 14 '05 #15


The Question no one asked is what are you learning?
The limitations given must be to force the use of some classroom topic.
If the topic is linked lists handing in qsort() teaches nothing.

Nov 14 '05 #16
Neil Kurzman <ns*@mail.asb.c om> writes:
The Question no one asked is what are you learning?
The limitations given must be to force the use of some classroom topic.
If the topic is linked lists handing in qsort() teaches nothing.


I'd say that the restrictions given would also cause learning
just about nothing, except how to get around unreasonable
restrictions.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming
Nov 14 '05 #17

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
4164
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
1724
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
2228
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
2766
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
1980
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
5325
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
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
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...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
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
7286
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
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.