Solved

about sorting?

Posted on 1998-10-26
12
174 Views
Last Modified: 2010-04-16
Can you give me a solution to sort numbers in ascending or in descending order without using an array, record, pointer and OOP? and also give me the source code about this question? make it in a simple way, easy to understand and short?

0
Comment
Question by:lewis_loo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 7

Expert Comment

by:Motaz
ID: 1215852
What did you mean by ( whithout array ) where did you store numbers ?

Motaz
0
 
LVL 1

Author Comment

by:lewis_loo
ID: 1215853
I mean the source code for sorting without using array,OOP ,files and pointer
if I want to input 5 number and want to sort it by ascending and decending uses procedure and function, you can use a recursive cycle.

no need to store number
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1215854
Where are the numbers to sort located then?
0
Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

 

Expert Comment

by:Styler
ID: 1215855
You mean you keep your numbers in differrent variables???

Deny Styler.
0
 
LVL 4

Expert Comment

by:binkzz
ID: 1215856
The quickest way to do it is to put it into an array, and to send
that array to a function sort(array) which would return the array
sorted.
0
 
LVL 4

Expert Comment

by:binkzz
ID: 1215857
IHMO, without an array you will not get very far.
0
 

Expert Comment

by:Malensek
ID: 1215858
You mean you just input your numbers on the screen, and then the program sorts them and write them in sorted order.
If so the only solution is to use a different variables, though that solution is for the beginners and is very unpractical.
0
 

Expert Comment

by:flobecker
ID: 1215859
Maybe you mean somehting like this:
 this example is using 3 vars called aa,bb,cc (pascal), the lowest number will be in aa,  the highest in cc.

 if aa>bb then
  begin
    temp:=aa;
    aa:=bb;
    bb:=temp;
  end;
 if bb>cc then
  begin
    temp:=bb;
    bb:=cc;
    cc:=temp;
  end;
 if aa>bb then
  begin
    temp:=aa;
    aa:=bb;
    bb:=temp;
  end;

Hope it works! No idea how many compares and xchanges are needed for four vars, or even five!
If you ask me: I'd use an array!!! :)

flobecker

0
 
LVL 2

Expert Comment

by:kellyjj
ID: 1215860
doing it w/o a array is asking for some serious sore fingers from all the typing you will have to do, plus the fact that you increase the chance of something going wrong.
0
 
LVL 2

Expert Comment

by:joe_h
ID: 1215861
GIGO...... Here's your code for 5 numbers with no array, record, pointer or OOP. It will read in five numbers, and print them out in ascending order.

var a,b,c,d,e:byte;
    t:byte;
begin
 readln(a,b,c,d,e);
 if a>b then begin t:=a;a:=b;b:=t;end;
 if b>c then begin t:=b;b:=c;c:=t;end;
 if c>d then begin t:=c;c:=d;d:=t;end;
 if d>e then begin t:=d;d:=e;e:=t;end;

 if a>b then begin t:=a;a:=b;b:=t;end;
 if b>c then begin t:=b;b:=c;c:=t;end;
 if c>d then begin t:=c;c:=d;d:=t;end;

 if a>b then begin t:=a;a:=b;b:=t;end;
 if b>c then begin t:=b;b:=c;c:=t;end;

 if a>b then begin t:=a;a:=b;b:=t;end;

 writeln(a,b,c,d,e);
end.

As you can see, it looks like a Bubblesort implementation. However, I still don't understand why you wouldn't want to use an array... unless it's a stupid homework ;)

0
 

Expert Comment

by:betanio
ID: 1215862
I think that the question was answered; joe_h show the only way.
Or lewis_loo wish magic?
0
 
LVL 3

Accepted Solution

by:
vikiing earned 0 total points
ID: 1215863
If the purpose is to sort ONLY 5 numbers (or a equally small amount of numbers), the solution is Joe_h's.

But the normal process of sorting always involves, at last, any kind of array. One of the the simplest way to sort an array is this one:

     For i:=1 to Max-1 do
         For j:=i+1 to Max do
             If Arr[j] < Arr[i]   then begin
                 Aux:=Arr[i];  Arr[i]:=Arr[j];  Arr[j]:=Aux;
             end;

(provided you have Arr[] array of Max elements in it).

That solution works well for a reasonable small quantity of element, let's say, when Max is of a couple of hundreds, or so, but not much more.

If the size of the array increases too much, the algorithm is very slow, an can be accelerated MUCH MUCH MORE by using the Quicksort recursive procedure; although it's the fastest method, it has a problem: it's much more complex. If you still need another solution, just shout.  :)

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Getting to know the threat landscape in which DDoS has evolved, and making the right choice to get ourselves geared up to defend against  DDoS attacks effectively. Get the necessary preparation works done and focus on Doing the First Things Right.
This article is in regards to the Cisco QSFP-4SFP10G-CU1M cables, which are designed to uplink/downlink 40GB ports to 10GB SFP ports. I recently experienced this and found very little configuration documentation on how these are supposed to be confi…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question