Sorting an Array 
Author Message
 Sorting an Array

Suppose you wanted to sort an array of word strings alphabetically
before writing it to an output file. Is it simply a matter of looking
at the first character of the first index, compare it to the next
index, switch if lower, then compare 2 and 3, etc?

Or is there a much more efficient and easier way to do this? I'm tying
myself up in knots trying to code it.

And does anyone have a function for determining if a single word
string is a palindrome or not?



Wed, 18 Jun 1902 08:00:00 GMT  
 Sorting an Array

Quote:

> Suppose you wanted to sort an array of word strings alphabetically
> before writing it to an output file. Is it simply a matter of looking
> at the first character of the first index, compare it to the next
> index, switch if lower, then compare 2 and 3, etc?

> Or is there a much more efficient and easier way to do this? I'm tying
> myself up in knots trying to code it.

> And does anyone have a function for determining if a single word
> string is a palindrome or not?

TP can compare strings with > < and >= <= = .
Probably you have to convert them to uppercase prior to the
comparison.
For sorting algorithms look at the TP-links page
http://www.geocities.com/~franzglaser/tp.html
In chapters "source" and FAQ there are examples.

On pg. 2 there is a Java demo of how Bubble-Sort and
QuickSort work in reality.

There is a QSORT.PAS example in the BP 7 package.

Regards,
Franz Glaser
http://members.eunet.at/meg-glaser



Wed, 18 Jun 1902 08:00:00 GMT  
 Sorting an Array

Quote:
>Suppose you wanted to sort an array of word strings alphabetically
>before writing it to an output file. Is it simply a matter of looking

Easiest way to do this - using an object tStringCollection from unit
OBJECTS.
Harry Poloscov

----  Processor not found - use a software emulation -----


Wed, 18 Jun 1902 08:00:00 GMT  
 Sorting an Array


Quote:

>Suppose you wanted to sort an array of word strings alphabetically
>before writing it to an output file. Is it simply a matter of looking
>at the first character of the first index, compare it to the next
>index, switch if lower, then compare 2 and 3, etc?

Try the following:

Type CompareFunc=Function(i,j:integer):Boolean;
     SwapProc=Procedure(i,j:integer);

Procedure ShellSort(first,n:integer;InOrder:CompareFunc; Swap:SwapProc);
var i,j,incr,last:integer;
Begin
  incr:=longint(n)*10 div 17;
  last:=n+1-first;

  while incr>0 do begin
    for i:=first to last-incr do begin
      j:=i;
      while (j>=first) and Not InOrder(j,j+incr) do begin   { Short circuit! }
          Swap(j,j+Incr);
          dec(j,incr);
      End
    End;
    incr:=longint(incr)*10 div 17;
  End;
End;

That is you need to write function that compares two elements and a
procedure that swaps two elements and make them far. After that you pass
the first index, the number of elements  and above routines to
Shellsort().

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. sorting an array

2. Sorting an array

3. sorting an array

4. Shell sort vs. Comb sort (was: sorting arrays)

5. lgarray0.zip TP unit for huge arrays and arrays of sorted strings

6. How to randomize a sorted array?

7. Sorting Array

8. sort index in array

9. sorting arrays

10. Sorting arrays help needed

11. Need Help with sorting through numbers in an array

12. Sorting filerecords without an array?

 

 
Powered by phpBB® Forum Software