Skip to content

Commit b72706f

Browse files
Merge pull request #8 from anasmak04/master
sort an array without functions
2 parents 1bf5f21 + 3af1c9a commit b72706f

File tree

2 files changed

+21
-1
lines changed

2 files changed

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

0 commit comments

Comments
 (0)