Created
February 22, 2017 01:15
-
-
Save basekays/0622f832074a3c0eb7433c86329cd24e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var findSmallest = function(array) { | |
| var smallest = array[0]; | |
| for (var i = 1; i < array.length; i++) { | |
| if (smallest > array[i]) { | |
| smallest = array[i]; | |
| } | |
| } | |
| return smallest; | |
| } | |
| var findLargest = function(array) { | |
| var largest = array[0]; | |
| for (var i = 1; i < array.length; i++) { | |
| if (largest < array[i]) { | |
| largest = array[i]; | |
| } | |
| } | |
| return largest; | |
| } | |
| var bubbleSort = function(array) { | |
| debugger; | |
| for (var i = 0; i < array.length; i++) { | |
| var targetElement = array[i]; | |
| if (targetElement > array[i + 1]) { | |
| array[i] = array[i + 1]; | |
| array[i + 1] = targetElement; | |
| } | |
| } | |
| var isSmallestInPosition = (array[0] === findSmallest(array)); | |
| var isLargestInPosition = (array[array.length - 1] === findLargest(array)); | |
| if (isSmallestInPosition && isLargestInPosition) { | |
| return array; | |
| } else { | |
| bubbleSort(array); | |
| } | |
| return array; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment