var arr1=[-3,8,7,6,5,-4,3,2,1]; var arr2=[]; var min=arr1[0]; var pos; max=arr1[0]; for (i=0; iarr1[j]) { min=arr1[j]; pos=j; } } } arr2[i]=min; arr1[pos]="x"; min=max; } console.log(arr2);