Skip to content

Instantly share code, notes, and snippets.

@fador
Created March 20, 2019 12:57
Show Gist options
  • Save fador/6081e209bfc3516a2afbeb2ac903e5ea to your computer and use it in GitHub Desktop.
Save fador/6081e209bfc3516a2afbeb2ac903e5ea to your computer and use it in GitHub Desktop.
//var input = [3, 1, 88, 6, 2, 2];
var input = [Math.floor(Math.random()*100),Math.floor(Math.random()*100),Math.floor(Math.random()*100),Math.floor(Math.random()*100),Math.floor(Math.random()*100),Math.floor(Math.random()*100)];
var output = [0, 0, 0, 0, 0, 0];
var input_temp = [input[0], input[1], input[2], 255, input[3], input[4], input[5], 255];
// Improvised merge sort
// Sort list from lowest to highest.
// Swap sort first 3 items
if (input_temp[0] > input_temp[1]) {
var temp = input_temp[1];
input_temp[1] = input_temp[0];
input_temp[0] = temp;
}
if (input_temp[0] > input_temp[2]) {
var temp = input_temp[2];
input_temp[2] = input_temp[0];
input_temp[0] = temp;
}
if (input_temp[1] > input_temp[2]) {
var temp = input_temp[2];
input_temp[2] = input_temp[1];
input_temp[1] = temp;
}
// Swap sort next 3 items
if (input_temp[4] > input_temp[5]) {
var temp = input_temp[5];
input_temp[5] = input_temp[4];
input_temp[4] = temp;
}
if (input_temp[4] > input_temp[6]) {
var temp = input_temp[6];
input_temp[6] = input_temp[4];
input_temp[4] = temp;
}
if (input_temp[5] > input_temp[6]) {
var temp = input_temp[6];
input_temp[6] = input_temp[5];
input_temp[5] = temp;
}
// Merge two subarrays
var array1 = 0;
var array2 = 4;
for (var item = 0; item < 6; item++) {
if (input_temp[array1] < input_temp[array2]) {
output[item] = input_temp[array1];
array1++;
} else {
output[item] = input_temp[array2];
array2++;
}
}
console.log(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment