Tech Support > Computers & Technology > Programming > give me some advice
give me some advice
Posted by kthpink on March 8th, 2007


how to make a number sorting use stack in pascal?

Posted by osmium on March 8th, 2007


"kthpink" writes:

Your question is not very clear to me. Is there some reason not to write a
Pascal program which uses the quick sort algorithm?



Posted by SubstituteMyFirstNameHere@pherber.com on March 8th, 2007


On 7 Mar 2007 22:51:55 -0800, "kthpink" <kthpink@gmail.com> wrote:

1. Get a dictionary
2. Look up the word "please"



Posted by santosh on March 8th, 2007


kthpink wrote:
Why do you insist on using a stack for sorting in Pascal?


Posted by user923005 on March 8th, 2007


On Mar 7, 10:51 pm, "kthpink" <kthp...@gmail.com> wrote:
A stack is the wrong sort of thing to sort with. Use an array or a
list.
Stacks access things at the ends (whether FIFO or LIFO) and to sort
effectively, you need to be able (at least) to scan the whole thing.


Posted by rossum on March 8th, 2007


On 8 Mar 2007 10:21:04 -0800, "santosh" <santosh.k83@gmail.com> wrote:

problem written in Pascal and using a stack.

rossum


Posted by user923005 on March 8th, 2007


On Mar 8, 11:00 am, rossum <rossu...@coldmail.com> wrote:
That's like an automotive teacher asking his students to overhaul an
engine with a pipe wrench, a crowbar, and a 15 pound sledge.
Destined to end in frustration and disaster.




Posted by Chris Uppal on March 9th, 2007


user923005 wrote:

I don't see why; some sorting algorithms are inherently recursive (such
as Quicksort, of course); and implementing one using an explicit stack
rather than recursion would be a valuable exercise both in theory and
practise.

-- chris

Posted by user923005 on March 9th, 2007


On Mar 8, 5:04 pm, "Chris Uppal" <chris.up...@metagnostic.REMOVE-
THIS.org> wrote:
Now that you put it that way, that is probably what was originally
sought.



Posted by Richard Heathfield on March 9th, 2007


user923005 said:

And therefore sorting with one would be akin to running a sack race, and
they can be kinda fun. So let's give it a whirl.

I'll start off with one input stream, three initially empty stacks, and
crossed fingers, since I have no idea as to whether or not this is
going to work. Here's my input stream (which will be familiar to those
with QWERTY keyboards and a talent for vertical thinking):

QAZWSXEDCRFV

We'll start off by pushing Q onto Stack 0, and storing a copy of it for
comparison purposes:

Input stream: AZWSXEDCRFV

Comparator: Q
Stack 0: [top]Q[bottom]
Stack 1: [top][bottom]
Stack 2: [top][bottom]

Now we'll plunder the input stream, pushing onto either Stack 0 or Stack
1 as dictated by the comparator:

Stack 0: [top]FCDEAQ[bottom]
Stack 1: [top]VRXSWZ[bottom]
Stack 2: [top][bottom]

We now take the top item from Stack 0 as our comparator, push it onto
stack 2, and take anything greater than it from Stack 0 to Stack 1.
Anything less goes onto Stack 2:

Comparator: F
Stack 0: [top][bottom]
Stack 1: [top]QVRXSWZ[bottom]
Stack 2: [top]AEDCF[bottom]

We keep doing this, alternating between Stack 0 and Stack 2, until
nothing except the comparator is moved from one to the other.

Comparator: A
Stack 0: [top]A[bottom]
Stack 1: [top]FCDEQVRXSWZ[bottom]
Stack 2: [top][bottom]

We now output the comparator A, flush stack 0, and then repeat:

Comparator: C
Stack 0: [top]ZWSXRVQED[bottom]
Stack 1: [top][bottom]
Stack 2: [top]C[bottom]

We now output the comparator C, flush stack 2, and then repeat (ad
nauseam).

There's bound to be a better way, but this way /will/ work. Hop hop hop.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.

Posted by CBFalconer on March 9th, 2007


rossum wrote:
All we need is a function that will return 0 or 1 with random
probability, and a function to test that things are sorted: The
rest are easily programmed.

PROGRAM bogosort(input, output);

(* put declarations of data and functions here *)

BEGIN
ct := loadarray(VAR a);
WHILE NOT sorted(a) DO BEGIN
saved := a[1];
FOR i := 2 TO ct DO
IF random > 0 THEN push(a[i])
ELSE BEGIN
push(saved); saved := a[1];
END;
FOR i := 1 to ct DO a[i] := pop;
END;
END. (* untested *)

It may take a while, and you need a truly random random.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


--
Posted via a free Usenet account from http://www.teranews.com



Similar Posts