Author Topic: function Sort(input: array of integer): array of integer;  (Read 1475 times)

0 Members and 1 Guest are viewing this topic.

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
function Sort(input: array of integer): array of integer;
« on: February 24, 2010, 10:40:08 pm »
This is just a simple sort algorithm. It takes an input of integers ( you can easily edit it to fit the type of your need ) and outputs it in ascending order. Use as you please.

Code: [Select]
function Sort(input: array of integer): array of integer;
// function Sort, by Hacktank, input an array of integers and it will output them in ascending order.
var i,ii,first,last,len,cur: integer; used: array of boolean;
begin
len := getarraylength(input);
setarraylength(result,len);
setarraylength(used,len);
for i := 0 to len-1 do begin
for ii := 0 to len-1 do if not used[ii] then begin
cur := input[ii];
first := ii;
break;
end;
last := first;
for ii := first to len-1 do if not used[ii] then if input[ii] < cur then begin
cur := input[ii];
last := ii;
end;
result[i] := cur;
used[last] := true;
end;
end;


Offline Mighty

  • Camper
  • ***
  • Posts: 276
Re: function Sort(input: array of integer): array of integer;
« Reply #1 on: July 03, 2010, 01:28:16 pm »
Edit2: Solved, sry.

is there any fast way to modify this so it sorts 2D array?

i got table MyTable[[One,1],[Ten,10],[Three,3]]
I want it to be sorted like that: [[One,1],[Three,3],[Ten,10]]

I rly was trying hard to understand how did U sort it... but no way. :/

Edit: I need this sorting to be as fast as possible and all my ideas... well, aren't perfect.


« Last Edit: July 03, 2010, 01:43:37 pm by Mighty »
xFire: macmil        e-mail: macekmil@gmail.com
My scripts: Accuracy Script       Flashbang       Punishments GUID
            CatchMe Gamemod       AntiFake
            CW System             AntiFakeGUID

Offline VirtualTT

  • Veteran
  • *****
  • Posts: 1026
Re: function Sort(input: array of integer): array of integer;
« Reply #2 on: July 03, 2010, 01:39:03 pm »
This function fails at the very begging... What's the point of building and returning new array and not just modifying given one? Also your algorithm seem to be somewhat wrong... At least it's definitely much more complicated than it supposed to be... 
« Last Edit: July 03, 2010, 01:44:57 pm by VirtualTT »

Offline Swompie

  • Camper
  • ***
  • Posts: 390
Re: function Sort(input: array of integer): array of integer;
« Reply #3 on: July 03, 2010, 02:22:45 pm »
Edit2: Solved, sry.

is there any fast way to modify this so it sorts 2D array?

i got table MyTable[[One,1],[Ten,10],[Three,3]]
I want it to be sorted like that: [[One,1],[Three,3],[Ten,10]]

I rly was trying hard to understand how did U sort it... but no way. :/

Edit: I need this sorting to be as fast as possible and all my ideas... well, aren't perfect.

I'll take a look at this later ;p
Gah, maybe someone else will do it, can't brainstorm at moment :p
« Last Edit: July 03, 2010, 02:48:03 pm by Swompie »