Re: sort array
From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 06/12/04
- Next message: B. v Ingen Schenau: "Re: References"
- Previous message: B. v Ingen Schenau: "Re: [newbie] stack with templates"
- In reply to: Robert W Hand: "Re: sort array"
- Next in thread: Jerry Coffin: "Re: sort array"
- Reply: Jerry Coffin: "Re: sort array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 12 Jun 2004 10:52:34 +0100
In article <tgilc0l1j42sifertmksoa3op4vpvu7p1o@4ax.com>, Robert W Hand
<rwhand@NOSPAMoperamail.com> writes
>You need to compare more than two consecutive values of the array. It
>means that you need to loop twice. There are a number of sorting
>algorithms. Here is one simple minded implementation of a simple
>sorting algorithm.
Some of us have been trying for over a decade to convince people to stop
advocating a ripple sort. Except in very special cases it is a poor
method. A simple, in place insertion sort is much better and just as
simple to understand:
Look through the unsorted elements and find the smallest, swap that with
the first unsorted element and move down one place, repeat until there
is only a single element left.
Visualisation with cards:
Take an unsorted deck of cards and find the smallest, swap that with the
top card (note this is an in place sort hence the swap). Now repeat the
exercise for all but the cards that have been swapped to the top.
>
>
>void sorting (float A[M], int n)
> {
> for(int i=0; i<n; ++i)
>/* Loop through all the elements of array */
> for(int j=i+1; j<n; ++j)
>/* Assume that elements less than i are sorted. Loop through unsorted
>elements to the right of i. */
> if(A[i]>A[j])
> swapf(A[i], A[j]);
>/* If the elements are out of order, swap them. */
>/*
> }
>
>swapf is a macro that switches values between the first and second
>argument. It would be similar to the last three lines of your
>function. HTH.
-- Francis Glassborow ACCU Author of 'You Can Do It!' see http://www.spellen.org/youcandoit For project ideas and contributions: http://www.spellen.org/youcandoit/projects
- Next message: B. v Ingen Schenau: "Re: References"
- Previous message: B. v Ingen Schenau: "Re: [newbie] stack with templates"
- In reply to: Robert W Hand: "Re: sort array"
- Next in thread: Jerry Coffin: "Re: sort array"
- Reply: Jerry Coffin: "Re: sort array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|