Skip to content

Commit ec4c993

Browse files
committed
solve sort array without functions
1 parent 2f0fe6f commit ec4c993

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
| Problem - 43 | [ Fibonacci ](https://github.com/anasmak04/Problem-solving-with-JavaScript/blob/master/problem-43.js) |
4848
| Problem - 44 | [ swap ](https://github.com/anasmak04/Problem-solving-with-JavaScript/blob/master/problem-44.js) |
4949
| Problem - 45 | [ max two numbers ](https://github.com/anasmak04/Problem-solving-with-JavaScript/blob/master/problem-45.js) |
50+
| Problem - 46 | [ Sort an Array without using any built-in functions ](https://github.com/anasmak04/Problem-solving-with-JavaScript/blob/master/problem-46.js) |
5051

5152

52-
<!-- | Problem - 46 | | -->
53+
<!-- | Problem - 47 | | -->

problem-46.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Sort an Array You must solve the problem without using any built-in functions
2+
let sortArray = function(nums) {
3+
do {
4+
swapp = false
5+
for (let i = 0; i < nums.length; i++) {
6+
if (nums[i] > nums[i + 1]) {
7+
let temp = nums[i];
8+
nums[i] = nums[i + 1];
9+
nums[i + 1] = temp;
10+
swapp = true
11+
}
12+
}
13+
} while (swapp);
14+
return nums
15+
};
16+
17+
18+
console.log(sortArray([5,9,2,-1,3,-126])) // [-126 , -1 , 2 ,3 , 5 , 9]

0 commit comments

Comments
 (0)