if(Array[i] > Array[i+1] && Array[i+1] < sizeof(Array))
{
printf("Array[%d] = %d, Array[%d+1] = %d.", i, Array[i], i, Array[i+1]);
tmp = Array[i];
printf("Array[%d] = %d, Array[%d+1] = %d.", i, Array[i], i, Array[i+1]);
Array[i] = Array[i+1];
printf("Array[%d] = %d, Array[%d+1] = %d.", i, Array[i], i, Array[i+1]);
Array[i+1] = tmp;
printf("Array[%d] = %d, Array[%d+1] = %d.", i, Array[i], i, Array[i+1]);
}
sortArray( arr[ ], len = sizeof arr ) {
new tmpVar1, tmpVar2, i;
while(i < len) {
for( new j = len - 1; j > -1; j --) {
if( arr[ j ] < arr[ i ] ) {
tmpVar1 = arr[ i ];
arr[ i ] = arr[ j ];
arr[ j ] = tmpVar1;
}
}
i++;
}
for( i = 0; i < len; i ++ ) {
if( i == 0 ) {
tmpVar1 = arr[ i ];
}
tmpVar2 = arr[ i + 1 ];
arr[ i + 1 ] = tmpVar1;
tmpVar1 = tmpVar2;
if( i == (len - 1) ) {
arr[ 0 ] = arr[ len ];
}
}
return true;
}
You can't sort an array like that, honestly, it's more complicated than what you think. It took me 2 hours to figure out how to sort an array in this morning, though I managed to find a way to get the job done, but it's really complicated, it might help you though. I've posted it in your other topic (I guess) but I've improved it a bit. Here it is:
PHP Code:
|
new array[] = {30, 100, 20, 40, 5, 23, 56, 21, 2000, 40, 60, 20, 70, 80, 290, 5000, 20};
SortValuesInArray(array);
for(new i = 0; i < sizeof(array); i ++)
{
printf("%d", array[i]);
}
stock SortValuesInArray(array[], size_of = sizeof(array))
{
new temp;
for(new i = 1, j = 0; i < size_of; i ++)
{
for(j = i; j != 0; j --)
{
if(array[j] < array[j - 1])
{
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
return 1;
}
Your function can't process 7+ iterations, it breaks down. So I couldn't benchmark it.
|
new
arr[ 10 ] = { 70, 12, 1, 9, 7, 6, 3, 7, 10, 90 }
;
START_BENCH( 1000 );
{
sortArray( arr );
}
FINISH_BENCH( "sortArray" );
for(new k; k < sizeof arr; k++)
printf("%i", arr[ k ]);
sortArray( arr[ ], len = sizeof arr ) {
new tmpVar1, tmpVar2, i;
while(i < len) {
for( new j = len - 1; j > -1; j --) {
if( arr[ j ] < arr[ i ] ) {
tmpVar1 = arr[ i ];
arr[ i ] = arr[ j ];
arr[ j ] = tmpVar1;
}
}
i++;
}
for( i = 0; i < len; i ++ ) {
if( i == 0 ) {
tmpVar1 = arr[ i ];
}
tmpVar2 = arr[ i + 1 ];
arr[ i + 1 ] = tmpVar1;
tmpVar1 = tmpVar2;
if( i == (len - 1) ) {
arr[ 0 ] = arr[ len ];
}
}
return true;
}
Bench for sortArray: executes, by average, 28.01 times/ms.
1
3
6
7
7
9
10
12
70
90
Uhhm, but I tried benchmarking it and it works. which method did you use to benchmark it?
|
new tick = GetTickCount();
for(new i = 0; i < 7; i ++)
{
new array[] = {30, 100, 20, 40, 5, 23, 56, 21, 2000, 40, 60, 20, 70, 80, 290, 5000};
sortArray(array);
}
printf("%d", GetTickCount() - tick);
sortArray( arr[ ], len = sizeof arr ) {
new tmpVar1, tmpVar2, i;
while(i < len) {
for( new j = len - 1; j > -1; j --) {
if( arr[ j ] < arr[ i ] ) {
tmpVar1 = arr[ i ];
arr[ i ] = arr[ j ];
arr[ j ] = tmpVar1;
}
}
i++;
}
for( i = 0; i < len; i ++ ) {
if( i == 0 ) {
tmpVar1 = arr[ i ];
}
tmpVar2 = arr[ i + 1 ];
arr[ i + 1 ] = tmpVar1;
tmpVar1 = tmpVar2;
if( i == (len - 1) ) {
arr[ 0 ] = arr[ len ];
}
}
return true;
}
main( )
{
new array[ 10 ], tick;
for( new i; i < 10; i ++ ) {
array[ i ] = random( 10000 ) - random( 2000 );
}
tick = GetTickCount();
for(new i = 0; i < 50000; i ++) {
sortArray(array);
}
printf("sortArray: %dms", GetTickCount() - tick);
tick = GetTickCount();
for(new i = 0; i < 50000; i ++) {
SortValuesInArray(array);
}
printf("SortValuesInArray: %dms", GetTickCount() - tick);
return true;
}
stock SortValuesInArray(array[], size_of = sizeof(array))
{
new temp;
for(new i = 1, j = 0; i < size_of; i ++)
{
for(j = i; j != 0; j --)
{
if(array[j] < array[j - 1])
{
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
return 1;
}
sortArray( arr[ ], len = sizeof arr ) {
new tmpVar1, tmpVar2, i;
while(i < len) {
for( new j = len - 1; j > -1; j --) {
if( arr[ j ] < arr[ i ] ) {
tmpVar1 = arr[ i ];
arr[ i ] = arr[ j ];
arr[ j ] = tmpVar1;
}
}
i++;
}
for( i = 0; i < len; i ++ ) {
if( i == 0 ) {
tmpVar1 = arr[ i ];
}
tmpVar2 = arr[ i + 1 ];
arr[ i + 1 ] = tmpVar1;
tmpVar1 = tmpVar2;
if( i == (len - 1) ) {
arr[ 0 ] = arr[ len ];
}
}
return true;
}
sortArray: 1822ms SortValuesInArray: 641ms
sortArray( arr[ ], len = sizeof arr ) {
new tVar, i;
while(i <= len) {
for( new j = len ; j > -1; j --) {
if( arr[ j ] < arr[ i ] ) {
tVar = arr[ i ];
arr[ i ] = arr[ j ];
arr[ j ] = tVar;
}
}
i++;
}
return true;
}
-183,0,756,1748,2740,5751,5980,6320,6493,7657 sortArray: 1957ms SortValuesInArray: 637ms
sortArray( arr[ ], len = sizeof arr ) {
new tVar;
for( new i; i <= len; i++ ) {
for( new j = ( len - 1 ) ; j > ( i - 1 ); j --) {
if( arr[ j ] < arr[ i ] ) {
tVar = arr[ i ];
arr[ i ] = arr[ j ];
arr[ j ] = tVar;
}
}
}
return true;
}
before sorting: 9,12,14,19,7,17,15,0,17,19 after sorting: 0,7,9,12,14,15,17,17,19,19 sortArray: 854ms SortValuesInArray: 641ms
stock SortValuesInArray(array[], size_of = sizeof(array))
{
new temp;
for(new i = size_of, j = 0; i != 0; i --)
{
for(j = (size_of - i); j != 0; j --)
{
if(array[j] < array[j - 1])
{
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
return 1;
}
sortArray( arr[ ], len = sizeof arr ) {
new tVar, i, j;
for( i = len - 1; i > -1; i -- ) {
for( j = 0; j < i; j ++ ) {
if( arr[ i ] < arr[ j ] ) {
tVar = arr[ i ];
arr[ i ] = arr[ j ];
arr[ j ] = tVar;
}
}
}
return true;
}
main( )
{
new array[ 10 ];
for( new i; i < 10; i ++ ) {
array[ i ] = random( 200 );
}
for(new j; j < 10; j ++) {
new tick = GetTickCount();
for( new i; i < 50000; i++ ) sortArray( array );
printf("Test No. %i: Time taken to execute \"sortArray\" 50000 times is: %ims", j + 1, GetTickCount() - tick );
}
for(new j; j < 10; j ++) {
new tick = GetTickCount();
for( new i; i < 50000; i++ ) SortValuesInArray( array );
printf("Test No. %i: Time taken to execute \"SortValuesInArray\" 50000 times is: %ims", j + 1, GetTickCount() - tick );
}
return true;
}
sortArray( arr[ ], len = sizeof arr ) {
new tVar, i, j;
for( i = len - 1; i > -1; i -- ) {
for( j = 0; j < i; j ++ ) {
if( arr[ i ] < arr[ j ] ) {
tVar = arr[ i ];
arr[ i ] = arr[ j ];
arr[ j ] = tVar;
}
}
}
return true;
}
stock SortValuesInArray(array[], size_of = sizeof(array))
{
new temp;
for(new i = size_of, j = 0; i != 0; i --)
{
for(j = (size_of - i); j != 0; j --)
{
if(array[j] < array[j - 1])
{
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
return 1;
}
Test No. 1: Time taken to execute "sortArray" 50000 times is: 629ms Test No. 2: Time taken to execute "sortArray" 50000 times is: 620ms Test No. 3: Time taken to execute "sortArray" 50000 times is: 621ms Test No. 4: Time taken to execute "sortArray" 50000 times is: 620ms Test No. 5: Time taken to execute "sortArray" 50000 times is: 617ms Test No. 6: Time taken to execute "sortArray" 50000 times is: 614ms Test No. 7: Time taken to execute "sortArray" 50000 times is: 622ms Test No. 8: Time taken to execute "sortArray" 50000 times is: 622ms Test No. 9: Time taken to execute "sortArray" 50000 times is: 616ms Test No. 10: Time taken to execute "sortArray" 50000 times is: 617ms Test No. 1: Time taken to execute "SortValuesInArray" 50000 times is: 655ms Test No. 2: Time taken to execute "SortValuesInArray" 50000 times is: 656ms Test No. 3: Time taken to execute "SortValuesInArray" 50000 times is: 661ms Test No. 4: Time taken to execute "SortValuesInArray" 50000 times is: 659ms Test No. 5: Time taken to execute "SortValuesInArray" 50000 times is: 652ms Test No. 6: Time taken to execute "SortValuesInArray" 50000 times is: 657ms Test No. 7: Time taken to execute "SortValuesInArray" 50000 times is: 670ms Test No. 8: Time taken to execute "SortValuesInArray" 50000 times is: 658ms Test No. 9: Time taken to execute "SortValuesInArray" 50000 times is: 660ms Test No. 10: Time taken to execute "SortValuesInArray" 50000 times is: 656ms
stock SortValuesInArray(array[], size_of = sizeof(array))
{
for(new i = (size_of - 1), j, temp; i != 0; i --)
{
for(j = 0; j < i; j ++)
{
if(array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
return 1;
}