Jump to content
  • 0

I need help with sorting variables.


anacondaq

Question


  • Group:  Members
  • Topic Count:  42
  • Topics Per Day:  0.01
  • Content Count:  1096
  • Reputation:   345
  • Joined:  02/26/12
  • Last Seen:  

Sorry for my english, some question:

 

I have many variables, for example

a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p;

all of them have different values (number)

I need to find variable where number is bigger then all other.

 

For example:

set a,10;

set b,54;

set c,13;

set d,0;

set e,12;

set f,9;

set g,43;

set h,2;

set i,15;

set j,13;

set m,1512;

set n,236;

set o,51;

set p,521;

 

 

I need to find m variable. because it's have 1512, but before finding i need analize all another variables to get who have biggest value.

Help me please.

Link to comment
Share on other sites

2 answers to this question

Recommended Posts


  • Group:  Members
  • Topic Count:  72
  • Topics Per Day:  0.02
  • Content Count:  2997
  • Reputation:   1130
  • Joined:  05/27/12
  • Last Seen:  

If you don't store the numbers in an array, the only way to do this without writing it out would be through storing the variable names in an array, and looping with getd() - extremely messy and unnecessarily resource-intensive. If these are character variables (i.e. you can't use an array), you can probably find a way to do this with an SQL function.

Assuming all the variables are in an array, you can use an algorithm like this:

	set .@max, .@arr[0];
	for(set .@i,1; .@i<getarraysize(.@arr); set .@i,.@i+1) {
		if (.@arr[.@i] > .@max) {
			set .@max, .@arr[.@i];
			set .@ele, .@i;
			deletearray .@tie[0], getarraysize(.@tie);
		} else if (.@arr[.@i] == .@max) {
			if (!getarraysize(.@tie))
				set .@tie[0], .@arr[.@ele];
			set .@tie[getarraysize(.@tie)], .@i;
		}
	}
And that'll give you your max, .@max; the element where it's located, .@ele; and an array of any elements it's equal to, .@tie[].
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  42
  • Topics Per Day:  0.01
  • Content Count:  1096
  • Reputation:   345
  • Joined:  02/26/12
  • Last Seen:  

Euphy, can you recommend for me maby some book with algorythms?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...