H
HERC777
just paste the below into notepad, call it iqsort.html double click it
and your IE browser will run it. (neat! a universal programming
language we can all run!)
it falls out from the sort procedure. I got javascript quicksort
running, it's quick but it gets stack overflow after about 5000
elements.
Am I passing the array as a parameter alright? I think javascript
default is to pass pointers, that can be global.
Herc
<script language = "javascript">
l=0
r=0
stack=0
stuck = new Array
ARRAY_MAX = 10
function PUSH(l, r) {
stack++
stuck[stack] = l
stack++
stuck[stack] = r
}
function POP(l, r) {
r = stuck[stack]
stack--
l = stuck[stack]
stack--
}
function main()
{
i = 0
array1 = new Array
array2 = new Array
alert("The array is composed of the following values")
for(i=0; i<ARRAY_MAX; i++) {
array1 = parseInt(Math.random(1) * 100)
array2 = array1
}
alert (array1[1] + " " + array1[2] + " " + array1[3] + " " +
array1[4] + " " + array1[5])
iterative_qsort(array2, 0, ARRAY_MAX-1);
alert (array2[1] + " " + array2[2] + " " + array2[3] + " " +
array2[4] + " " + array2[5])
}
function iterative_qsort(tarray, left, right)
{
i=0
last=0
temp=0
position=0
if(left < right) {
/* Push -1's onto the stack as a signal to us in the
future that we've reached the bottom. */
PUSH(-1, -1);
PUSH(left, right);
finished = false
while (!finished) {
POP(left, right);
if((left == -1) && (right == -1)){
finished = true
}
else {
if(left < right) {
position = parseInt((left + right) / 2)
temp = tarray
and your IE browser will run it. (neat! a universal programming
language we can all run!)
it falls out from the sort procedure. I got javascript quicksort
running, it's quick but it gets stack overflow after about 5000
elements.
Am I passing the array as a parameter alright? I think javascript
default is to pass pointers, that can be global.
Herc
<script language = "javascript">
l=0
r=0
stack=0
stuck = new Array
ARRAY_MAX = 10
function PUSH(l, r) {
stack++
stuck[stack] = l
stack++
stuck[stack] = r
}
function POP(l, r) {
r = stuck[stack]
stack--
l = stuck[stack]
stack--
}
function main()
{
i = 0
array1 = new Array
array2 = new Array
alert("The array is composed of the following values")
for(i=0; i<ARRAY_MAX; i++) {
array1 = parseInt(Math.random(1) * 100)
array2 = array1
}
alert (array1[1] + " " + array1[2] + " " + array1[3] + " " +
array1[4] + " " + array1[5])
iterative_qsort(array2, 0, ARRAY_MAX-1);
alert (array2[1] + " " + array2[2] + " " + array2[3] + " " +
array2[4] + " " + array2[5])
}
function iterative_qsort(tarray, left, right)
{
i=0
last=0
temp=0
position=0
if(left < right) {
/* Push -1's onto the stack as a signal to us in the
future that we've reached the bottom. */
PUSH(-1, -1);
PUSH(left, right);
finished = false
while (!finished) {
POP(left, right);
if((left == -1) && (right == -1)){
finished = true
}
else {
if(left < right) {
position = parseInt((left + right) / 2)
temp = tarray
;
tarray
tarray
= tarray[position];
tarray[position] = temp;
last = left;
for(i=left+1; i<=right; i++)
if(tarray < tarray
tarray[position] = temp;
last = left;
for(i=left+1; i<=right; i++)
if(tarray < tarray
) {
last++;
temp = tarray[last];
tarray[last] = array;
tarray = temp;
}
temp = tarray
last++;
temp = tarray[last];
tarray[last] = array;
tarray = temp;
}
temp = tarray
;
tarray
tarray
= tarray[last];
tarray[last] = temp;
PUSH(left, last-1);
PUSH(last+1, right);
}
}
}
}
}
main()
</script>
tarray[last] = temp;
PUSH(left, last-1);
PUSH(last+1, right);
}
}
}
}
}
main()
</script>