I'm trying to write a quicksort in javascript and display the sorted output. But Whenever I run the execute() function the program hangs and stopped responding. Why is this? I'm not so familiar with javascript and I just translated this from my java code. But I just don't see why it didn't work. Here the code.
<script type="text/javascript">
function exchange(a, i, j) {
var k = a[i];
a[i] = a[j];
a[j] = k;
}
function partition(a2, lo, hi) {
var i2 = lo;
var j2 = hi + 1;
var v = a2[lo];
while (true) {
while (a2[++i2] < v) {
if (i2 == hi) {
break;
}
}
while (v < a2[--j2]) {
if (j2 == lo) {
break;
}
}
if (i2 >= j2) {
break;
}
exchange(a2, i2, j2);
}
exchange(a2, lo, j2);
return j2;
}
function sort(a3, lo2, hi2) {
var j3 = partition(a3, lo2, hi2);
sort(a3, lo2, j3 - 1);
sort(a3, j 3+ 1, hi2);
}
function sort(a4) {
sort(a4, 0, a.length - 1);
}
function execute() {
var array = document.getElementById("texts").value.split(' ');
sort(array);
for (a in array) {
document.write(array[a] + "<br>");
}
}
</script>
sort(a3, j 3+ 1, hi2);. That should bej3, right?sort(). JavaScript doesn't support overloaded methods in the usual OOP way. That's probably part of the issue. I'm guessing you're calling the second definition ofsort(). That's why you didn't catch what @Guffa is pointing out in his comment. Because that code isn't executed.