functionSwaping3 (A, I, j) {if((i = = = j) | | (i = = = j + 1)){
if (i >= j) {return; }
//Debugger; if(a[i]%2 = = 1){ if(a[j]%2 = = 1) {swaping3 (A,++I, J); }Else{swaping3 (A,++i,--j); } }Else{ if(a[j]%2 = = 1){ vartemp; Temp=A[i]; A[i]=A[j]; A[J]=temp; Swaping3 (A,++i,--j); }Else{swaping3 (A, I,--j); } }//return A;}functionARR (num, max) {if(Arguments.length < 2) {alert ("Not Enough parameter ~"); return; } varAA = []; for(vari = 0; i<num; i++) {Aa.push (Math.ceil (Math.random ()*max)); } returnAA;//var i = 0, j = num-1;//swaping (this. AA, I, j);//return ("Swap finished ~")}varOO =NewObject (); OO.A= ARR (30, 100); Swaping3.call (Oo, oo. A,0, 29);
The above is the code, very simple, the following is the result of execution:
oo. A = ARR (30, 100); [7, Ten, Ten, ten, Notoginseng, Bayi, $,, 5, the same as,,,,,,,,,,,,,,,,,,,,, and, Ping3.call (Oo, oo. A, 0, +); undefinedoo.a[85, 95, 81, 49, 37, 81, 97, 89, 67, 7, 73, 5, 83, 41, 33, 15, 27, 93, 73, 59, 89, 42, 80, 96, 100, , 70]oo, ten, A. A = ARR (50, 10000); [2124, 5000, 5082, 5998, 7476, 962, 5368, 9555, 3593, 5932, 2321, 8518, 2118, 186, 1496, 5914, 1605, 4569, 162, 7487, 4689 , 2445, 3122, 8395, 8659, 2368, 1123, 2680, 673, 5098, 3679, 858, 9779, 50, 6818, 823, 8880, 8461, 1496, 942, 2316, 6356, 4691, 2397, 3756, 9451, 3103, 9179, 3108, 9892]swaping3.call (Oo, oo. A, 0, +); undefinedoo.a[9179, 3103, 9451, 2397, 4691, 8461, 823, 9555, 3593, 9779, 2321, 3679, 673, 1123, 8659, 8395, 1605 , 4569, 2445, 7487, 4689, 162, 3122, 5914, 1496, 2368, 186, 2680, 2118, 5098, 8518, 858, 5932, 50, 6818, 5368, 8880, 962, 1496, 942, 2316, 6356, 7476, 5998, 3756, 5082, he, 2124, 3108, 9892]
Function OK ~ complexity also satisfies O (N) ~
An array, by swapping all the odd numbers in the first half of all the even numbers in the second half, the Complexity O (N).