Link to home
Start Free TrialLog in
Avatar of lewis_loo
lewis_loo

asked on

about sorting?

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?

Avatar of Motaz
Motaz

What did you mean by ( whithout array ) where did you store numbers ?

Motaz
Avatar of lewis_loo

ASKER

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
Where are the numbers to sort located then?
You mean you keep your numbers in differrent variables???

Deny Styler.
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.
IHMO, without an array you will not get very far.
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.
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

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.
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 ;)

I think that the question was answered; joe_h show the only way.
Or lewis_loo wish magic?
ASKER CERTIFIED SOLUTION
Avatar of vikiing
vikiing

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial