Interview Question: Without using .sort(), write a function to numerically sort an array of integers.
Created
August 22, 2013 16:24
-
-
Save KatieK2/6309503 to your computer and use it in GitHub Desktop.
Manual Array Sorter
This file contains 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
<h1>Manual Array Sorter</h1> | |
<p>Without using .sort(), write a function to numerically sort an array of integers. Example: <code>var sortThis[2,3,1,5,4];</code></p> | |
<h2>h2</h2> |
This file contains 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 sortThis = [5,2,4,3,1]; | |
var a, b; | |
for (var j=0; j<sortThis.length; j++) | |
{ for (var i=0; i<sortThis.length; i++) | |
{ | |
a = sortThis[i]; | |
b = sortThis[i+1]; | |
if ( a > b && b ) | |
{ sortThis[i] = b; sortThis[i+1] = a; } | |
// Debug: $("body").append("<p>" + j + "." + i + ": " + sortThis + "</p>"); | |
} | |
} | |
$("h2").text(sortThis); |
This file contains 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
h1, h2, p { margin: 1rem; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment