SA-MP Forums Archive
Array sorting bug - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Array sorting bug (/showthread.php?tid=618179)



Array sorting bug - NeXoR - 02.10.2016

Hey guys, I have a 10 cells array which I tried to sort.
I tested it and it has only sorted 9 cells, and the last cell wasn't sorted
Can you solve it for me ?
PHP код:
for(new 0sizeof(Array); i++)
        {
            for(new 
1sizeof(Array) -1j++)
            {
                if(Array[
i] > Array[j])
                {
                    
tmp = Array[i];
                    Array[
i] = Array[j];
                    Array[
j] = tmp;
                }
            }
        } 



Re: Array sorting bug - SickAttack - 02.10.2016

Why did you create a new thread?

https://sampforum.blast.hk/showthread.php?tid=618131


Re: Array sorting bug - SyS - 02.10.2016

PHP код:
new i,j;
for( 
0sizeof(Array)-1i++) 
        { 
            for(
1sizeof(Array); j++) 
            { 
                if(Array[
i] > Array[j]) 
                { 
                    
tmp = Array[i]; 
                    Array[
i] = Array[j]; 
                    Array[
j] = tmp
                } 
            } 
        } 
last time when i posted there was a mistake that forgot to change the i loop should be itter from 0 - (upper bond -1)
not j loop sorry. try the above code it will work now.


Re: Array sorting bug - NeXoR - 02.10.2016

Quote:
Originally Posted by Sreyas
Посмотреть сообщение
PHP код:
new i,j;
for( 
0sizeof(Array)-1i++) 
        { 
            for(
1sizeof(Array); j++) 
            { 
                if(Array[
i] > Array[j]) 
                { 
                    
tmp = Array[i]; 
                    Array[
i] = Array[j]; 
                    Array[
j] = tmp
                } 
            } 
        } 
last time when i posted there was a mistake that forgot to change the i loop should be itter from 0 - (upper bond -1)
not j loop sorry. try the above code it will work now.
Thank you very much, honestly I care more about the explanation so I could do it myself in the future, but I think I got it.


Re: Array sorting bug - SyS - 02.10.2016

Quote:
Originally Posted by NeXoR
Посмотреть сообщение
Thank you very much, honestly I care more about the explanation so I could do it myself in the future, but I think I got it.
if you are interested in knowing the algorithm you can take a look @ below things

its the simplest sorting method (if you want more optimized sorting check my array experimenting repository)
the sorting method is known as linear sorting generally.This is how linear sort works
consider an array have following elements

PHP код:
A[]={3,2,5,1}; 
as you can see the above array elements are not sorted so we use linear sorting method to sort it.

Algorithm of linear sorting
1.First we select the first element of the array in the above example it is 3
ie; A[0]
then we use this element and check the rest of elements ahead in this element(2,5,1)

2.what we want to do is a ascending sort so the smallest element should be @ index 0
so we use 3 and check whether it is wrongly placed ie is it is not element that we dont want to be in that postion or in other words the next element is smaller than 3 (not sorted)

3.the next element is 2 so we should swap this element (2) with 3
now 2 will be our selected element then we check this 2 with 5 and 1 the element 1 is it is also swapped to first position

4.now after one iteration of j loop we gets smallest element @ the begining
now we dont need to bother about fist position the next element 3( after first iteration of j loop the array will be like tihs 1,3,5,2) is check with all other aheading elements and after 2nd iteration second smallest element is placed @ second position.and this continues till the array get sorted.

PHP код:
new i,j
for( 
0sizeof(Array)-1i++)  
/*the sizeof(Array)-1 is given because if i loop reaches the end the j loop will access out of bound element 
(if array has size 5 (upper bond 4) and i loop is in 5th iteration it  the j loop (j=i+1) will access 6 element 
(index 5)*/  
        
{  
           
/*selecting one element*/
 
for(1sizeof(Array); j++)  
            {  
            
/*looping to end of array so as to check with the selected element*/
                
if(Array[i] > Array[j])  
                {  
                    
tmp = Array[i];  
                    Array[
i] = Array[j];  
                    Array[
j] = tmp;  
                }  
            }  
        }