swapping every 2 numbers with each other in an array


/ Published in: ActionScript 3
Save to your folder(s)

Looking forward for the most optimized solution for the same


Copy this code and paste it in your HTML
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
  3. <mx:Script>
  4. <![CDATA[
  5. // second highest element in an array
  6. public var arr:Array = new Array("1", "2", "3", "4", "5", "6", "7","8"); // ("2", "1", "4", "3", "6", "5", "8", "7");
  7. public var swapArr:Array = new Array();
  8. public function init():void{
  9. for(var i=0;i<arr.length;i++)
  10. {
  11. txt.text += "\n hi " +i;
  12. var a = arr[i];
  13. var b = arr[i+1];
  14.  
  15. a = a^b;
  16. b = a^b;
  17. a = a^b;
  18. txt.text += "\n "+a+" ???? "+b;
  19.  
  20. swapArr.push(a);
  21. swapArr.push(b);
  22. if(i<arr.length){
  23. i++;
  24. }
  25. }
  26.  
  27. for(var k=0;k<swapArr.length;k++){
  28. txt.text += "\n "+swapArr[k];
  29. }
  30. }
  31. ]]>
  32. </mx:Script>
  33. <mx:TextArea x="118" y="57" width="553" height="412" id="txt"/>
  34. </mx:Application>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.