Re: sort array

From: Francis Glassborow (francis_at_robinton.demon.co.uk)
Date: 06/12/04


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


Relevant Pages

  • Re: massively OT: depressing/frustrating Katrina relief news
    ... sort clothes, but you can't go to any of the shelters without "training." ... getting their evacuees settled elsewhere. ... > a lot of the cards are missing in action--no one knows how many there were ...
    (rec.crafts.textiles.quilting)
  • Sorts
    ... These two programs -- Sorts and Lists -- demonstrate Cobol Features ... sort algorithms. ... When we had to sort a large number of punched cards, ... perform timer-on ...
    (comp.lang.cobol)
  • Re: massively OT: depressing/frustrating Katrina relief news
    ... out that a lot of the cards are missing in action--no one knows how many ... The people in the shelters are finding out much ... Cards and other assistance comes to the shelters, ... were homeless in N.O. before they are helping people who had some sort ...
    (rec.crafts.textiles.quilting)
  • massively OT: depressing/frustrating Katrina relief news
    ... Many, many Walmart gift cards were bought in the days after the storm, and people who bought them gave them to various organizations for distribution. ... The people in the shelters are finding out much more information about assistance and receiving much more help than those who have been able to stay with friends or find temporary housing. ... It also turns out that, here at least, agencies are helping those who were homeless in N.O. before they are helping people who had some sort of shelter there. ...
    (rec.crafts.textiles.quilting)
  • Re: Inexpensive, good gigabit ethernet NIC?
    ... > Installing cards can be a pain with the blind swap cassets (You will ... > need to find cassets also if your system is missing them). ... > cards, the type that have screws are the most difficult to assemble, the ...
    (comp.unix.aix)