for example: [1, 2, 3, 4, 5]
after sorting: 1, 3, 5, 2, 4
Note: does not create a new array , that is, it is changed on the basis of the original array.
for example: [1, 2, 3, 4, 5]
after sorting: 1, 3, 5, 2, 4
Note: does not create a new array , that is, it is changed on the basis of the original array.
if the order between odd numbers and even numbers does not need to be consistent with the original, you can refer to the idea of fast row
A pointer searches from left to right, a pointer searches from right to left, when the left finds an even number, and when the right finds an odd number, swap two numbers, and then continue to search until the two pointers are the same
Bubble sort
let arr = [1, 2, 3, 4, 5, 7, 9, 10, 13, 18];
for (let i = 0; i < arr.length - 1; iPP) {
for (let j = 0; j < arr.length - i - 1; jPP) {
let tmp = arr[j];
if (tmp % 2 === 0 && arr[j + 1] % 2 !== 0) {
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
console.log('arr', arr);//[ 1, 3, 5, 7, 9, 13, 2, 4, 10, 18 ]
at first I thought sort
would create a new array, but not
let arr = [1, 2, 3, 4, 5, 7, 9, 10, 13, 18];
arr.sort((a, b) => {
if (a % 2 === 0) {
if (b % 2 !== 0) {
return 1;
}
return 0;
} else
return -1
})
console.log('arr', arr);//[ 1, 3, 5, 7, 9, 13, 2, 4, 10, 18 ]
tested that sort
is really fast, so it looks like you don't need to use native ones in the future. Array.prototype.sort sorting algorithm
[1, 2, 3, 4, 5] .sort (function (a, b) {/ does not include /})
arr.sort (a = > a% 2 = 0); but this kind of sorting is unstable because fast arrangement is used internally in sort, which is unstable.
merge or bubble these if you need stable sorting, and you can change the judgment conditions to
Previous: React-native APP to achieve document customization function?
Next: How does vue-select get the current selection and get the property values of the selection?
encounters a javascript algorithm problem, which requires that find out all prime factors of a number algorithm I designed: function primeFactors(n){ var factors = []; var divisor = 3; if (n % 2==0) factors.push(2); while(n>5){ ...
for example, if you want to move from 0 to 100 1px at this speed, you can move x coordinates + 1 per frame. What if you want to move obliquely from 0 ~ 0 to 100 ~ ~ 20 at this speed? ...
Let the bullets be fired according to the direction of the mouse click, just like those who are good at fishing ...
find out how many zeros there are between 1 and 1 million ...
encounters an algorithm; similar to var a = [1 br > how to deal with it into a two-digit array [[1], [2], [4Power4], [5Power5]] < beacon > I wrote it myself: uh. how to deal with it into a two-digit array [[1], [2], [4parry 4], [5pjre 5]]. Loop twice, n...
has a select drop-down box with five option elements with value values from 1 to 5 <select> <option value="1">1< option> <option value="2">2< option> <option value="3">3< ...
given a non-empty array of integers, each element appears twice except for an element that appears only once. Find the element that only appears once. example 1: : [2,2,1] : 1 example 2: : [4,1,2,1,2] : 4 the answer with the highest running effici...
let arrs=[102,233,45,350,130,220,195,240] I want arrs to split into two groups, but I want the sum of the two groups to be as close as possible. The smaller the difference, the better. My own idea is to re-sort, from small to large, and then judge the s...
I want to output all possible routes, but in the end, I only output one route. I haven t figured out Orz for a long time. the following is the source code: ma mbmb.fill(0); 0 1 (x0,y0) position=[] direction=[[-1,0],[0,-1],[1,0],[0,1...
use JavaScript to build a segment tree, and recursion is used in the construction process. This should make the call stack overflow. I wonder if there is any other way to solve this problem. < H1 > Code < H1 > ...
data A: [ { "id":340, "name":"", "sub":[ { "id":341, "name":"" }, { &q...
php algorithm-- catching water droplets A novice PHP programmer, who has been on the job for half a year and is still hovering in endless additions, deletions, modifications and data optimization, has recently been brought into the algorithm pit by th...
directory tree initialization initially returns only a two-tier structure. [ { name: , path: , id: 1, childs: 2, children: [ { name: 01 , path: 01 , id: 2, ...
there are now such data objects arr = [ {] start: 1, end: 12 }, { start: 2, end: 5 }, { start: 6, end: 10 }, . ] how to determine whether the start and end of each object are continuous for example, the above situation is incorrect, because 1-12 c...
What is the data structure corresponding to Object in js? What is its logical structure and physical structure? ...
topic description as shown in the question, if you know the GPS coordinates of five points, how to find the sixth point and minimize the sum of the distances from the other five points, and find out his GPS coordinates would like to ask if you have ...