Copy link to clipboard
Copied
i have some loop function for the app, this loop for bubble sort, here the code:
Code |
---|
function startSort():void { var temp; var temp0X; var temp1X; for(var i:int = 0; i<angka.length; i++) { trace("iterasi ke " + i + " = " + angka.value ); for(var j:int = 0; j<angka.length-1; j++) { if(angka temp = angka temp0X = angka temp1X = angka[j+1].x; angka angka angka[j+1] = temp; angka[j+1].x = temp1X; trace("value A " + j + " = " + angka trace("value B " + (j+1) + " = " + angka[j+1].value); } else { } } } trace("\n--------------"); for(var k=0; k<angka.length; k++) { trace("angka ke " + k +" hasil = " + angka } } |
the problem is, sort number, not one by one, i want to number sort one by one, after the first sort number so the next sort will run, here for some algo that i wanted
Algoritma |
---|
Sort first |
How can i do that? Please Modif my code.. Thx before..
Copy link to clipboard
Copied
Hi.
What you can do is to use a setInterval function and pass the index at each call. Here is a suggestion that shows how to use this interval approach but not to necessarily solve your sorting algorithm.
Notice that I invented some values for the angka array so the code can work.
import flash.utils.setInterval;
import flash.utils.clearInterval;
var interval:uint;
var count:uint = 0;
var angka:Array =
[
{value:0, x:100},
{value:1, x:80},
{value:2, x:75},
{value:3, x:60},
{value:4, x:110},
{value:5, x:135}
];
function startSort(index:uint):void
{
trace("iterasi ke " + index + " = " + angka[index].value);
if (angka[index].value > angka[index + 1].value)
{
angka[index] = angka[index + 1];
angka[index].x = angka[index].x;
angka[index + 1] = angka[index];
angka[index + 1].x = angka[index + 1].x;
trace("value A " + index + " = " + angka[index].value);
trace("value B " + (index + 1) + " = " + angka[index + 1].value);
}
trace("\n--------------");
trace("angka ke " + index + " hasil = " + angka[index].value + " x = " + angka[index].x);
}
interval = setInterval(function():void
{
startSort(count++);
if (count == angka.length -1)
clearInterval(interval);
}, 5000);
Please let me know if you still have any further questions.
Regards,
JC
Copy link to clipboard
Copied
Thx you very much Mr. JoãoCésar​
You really helped me. But sorry, I've applied it in my program, there is an error.
Can I ask for help to change it directly into my program?
This is my project file link: My Project Bubble Sort
And it looks like my array also looks wrong because when doing sort the final result number is still random.
Thank you before.
Copy link to clipboard
Copied
Hi.
Is the goal to randomize the squares positions in the x axis at every 5 seconds?
Copy link to clipboard
Copied
The goal to sort object based value in the x axis at every 5 seconds from random position.
Example:
Before Sort: 3,1,5,6,7,2,5,4,10,9,8 (Random)
After Sort: 1,2, 3, 4, 5, 6, 7, 8, 9 , 10
Copy link to clipboard
Copied
The best I can do is to suggest how I would do something like this. It isn't exactly what you want, but I think you can make some use of it.
AS3 code:
import flash.events.MouseEvent;
import flash.utils.setInterval;
var posXVec:Vector.<Number> = new Vector.<Number>();
var interval:uint;
function randomizeSquares():void
{
var i:int, total:int;
for (i = 0, total = squares.numChildren; i < total; i++)
posXVec = squares.getChildAt(i).x;
posXVec.sort(function(a:*, b:*):Number
{
if (Math.random() < 0.5)
return -1;
else
return 1;
});
for (i = 0; i < total; i++)
squares.getChildAt(i).x = posXVec;
}
function sortSquares():void
{
var vec:Vector.<Number> = posXVec.sort(Array.NUMERIC);
for (var i:int = 0, total:int = vec.length; i < total; i++)
squares.getChildAt(i).x = vec;
}
function start():void
{
tom_mulai.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void{sortSquares();});
randomizeSquares();
interval = setInterval(randomizeSquares, 5000);
}
start();
FLA download:
Bubble Sort JC.zip - Google Drive
Regards,
JC