Skip to content

Instantly share code, notes, and snippets.

@grisza12
Last active March 9, 2016 11:46
Show Gist options
  • Save grisza12/01f8a7c1bd66921f1e49 to your computer and use it in GitHub Desktop.
Save grisza12/01f8a7c1bd66921f1e49 to your computer and use it in GitHub Desktop.
mergeSort
<body>
<header>
<form id="frm">
Liczby do posortowania: <input type="text" name="doSortowania" value="6,2,25,7,8,9,11,13">
</form>
</header>
<div class="wrapper">
<div class="btn1 btn">
<button onclick="myFunction()">MergeSort</button>
<p>
Merge Sort <br />
Złożoność algorytmu: O(n*ln(n))
</p>
</div>
<div class="btn2 btn">
<button onclick="myFunction()">MergeSort</button>
</div>
<div class="btn3 btn">
<button onclick="myFunction()">MergeSort</button>
</div>
<br />
</div>
<p id="demo"></p>
</body>
function myFunction() {
var x = document.getElementById("frm").elements[0].value.split(",");
var i = 0;
while (i != x.length) {
x[i] = parseInt(x[i]);
i++;
}
var start = new Date().getTime();
x = merge(x);
var stop = new Date().getTime();
var czas = stop - start;
document.getElementById("demo").innerHTML = x + "<br /> czas wykonania : " + czas + "s";
}
function merge(doSortowania) {
if (doSortowania.length < 2)
return doSortowania;
var srodek = parseInt(doSortowania.length / 2);
var lewa = doSortowania.slice(0, srodek);
var prawa = doSortowania.slice(srodek, doSortowania.length);
return laczenie(merge(lewa), merge(prawa));
}
function laczenie(lewa, prawa) {
var result = [];
while (lewa.length && prawa.length) {
if (lewa[0] <= prawa[0]) {
result.push(lewa.shift());
}
else {
result.push(prawa.shift());
}
}
while (lewa.length)
result.push(lewa.shift());
while (prawa.length)
result.push(prawa.shift());
return result;
}
body {
width: 600px;
margin: 0 auto;
}
.wrapper {
display: flex;
flex-flow: row wrap;
}
form {
padding: 10px;
margin: 10px;
}
.btn {
flex: 1 33%;
text-align:center;
}
button{
clear:both;
}
p{
word-wrap: break-word;
}
.btn1 { order: 1; }
.btn2 { order: 2; }
.btn3 { order: 3; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment