AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Sorting an array (https://forums.alliedmods.net/showthread.php?t=187622)

Liverwiz 06-15-2012 16:44

Sorting an array
 
hoekay, i have an array that i want to sort from highest to lowest. And in an attempt to do so i ran into 2 questions.

1: why can't i declare an array as such:
Spoiler


2: is this right, and efficient? The wiki was kinda vague.
Spoiler

Exolent[jNr] 06-15-2012 17:25

Re: Sorting an array
 
1. Array sizes have to be constant and cannot be dependent on a variable.
If you need to have a dynamic array size, look into cell arrays.

2. You don't even need that list.
Code:
public cmdRank(id)  // Possibly replace nVault with SQL, makes sorting easier and more efficient {     if(get_pcvar_num(toggle_pcvar) == 0 || get_pcvar_num(frags_pcvar) )         return PLUGIN_CONTINUE             // devlop to MOTD listing of server-wide rank (connected players     new players[32], num     get_players(players, num, "ch")         SortCustom1D(players, num, "sortHelper")         // players are sorted         return PLUGIN_HANDLED } public sortHelper(const index1, const index2, const players[], const data[], const data_size) {     new diff = gi_playerFrags[players[index2]] - gi_playerFrags[players[index1]]         return (diff < 0) ? -1 : (diff ? 1 : 0) }

Liverwiz 06-15-2012 20:47

Re: Sorting an array
 
Quote:

Originally Posted by Exolent[jNr] (Post 1729463)
1. Array sizes have to be constant and cannot be dependent on a variable.
If you need to have a dynamic array size, look into cell arrays.

2. You don't even need that list.
Code:
public cmdRank(id)  // Possibly replace nVault with SQL, makes sorting easier and more efficient {     if(get_pcvar_num(toggle_pcvar) == 0 || get_pcvar_num(frags_pcvar) )         return PLUGIN_CONTINUE             // devlop to MOTD listing of server-wide rank (connected players     new players[32], num     get_players(players, num, "ch")         SortCustom1D(players, num, "sortHelper")         // players are sorted         return PLUGIN_HANDLED } public sortHelper(const index1, const index2, const players[], const data[], const data_size) {     new diff = gi_playerFrags[players[index2]] - gi_playerFrags[players[index1]]         return (diff < 0) ? -1 : (diff ? 1 : 0) }

1. I only wanted it dynamic like that so i wouldn't have to have more cells then needed. But if its not possible, screw it.

2. wouldn't that change the frags for the players? because frags are stored in the cell index that correlates to their playerID, this would rearrange values, but not IDs. I need to keep both numbers together.

Though i must say....that return statement gives me a nerd-on. :mrgreen:

Exolent[jNr] 06-15-2012 21:04

Re: Sorting an array
 
It doesn't even change the player's frags or even how they are associated.
It just reorders the players array.

Liverwiz 06-16-2012 01:24

Re: Sorting an array
 
Quote:

Originally Posted by Exolent[jNr] (Post 1729559)
It doesn't even change the player's frags or even how they are associated.
It just reorders the players array.

OH! durr. I feel dumb. Its a trend this week....
Ordering the players array by frags is a great idea! Thanks, man. What would i do without you? Other than have horribly inefficient code.....

Also....does anyone know what kind of sort algorithm they use for these? I'd assume a bubble sort, which would make me want to get friskey with new algorithms, if i ever care enough to do so.

EDIT: They use Quicksort. (should have guessed-but i forgot about) which i suppose is the best way to do it in a lower-level language such as pawn. And if i ever figure out an easy way to do objects....i just might write a tree-sort. But objects will be relatively difficult. And even then....i'm sure it'll be way too memory intensive for the people around here....even if it is O(log n)

EDIT AGAIN: I figured out a way to do it. structure{[value], [leftIndex], [rightIndex]} done in array format We'll see where this takes us when i get around to coding it....


All times are GMT -4. The time now is 06:06.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.