krycek
11-19-2002, 10:19 PM
Right, I have two arrays, which are associated. One array stores some string data, and the other stores the frequency of the strings.
Now, what I want to do is sort the arrays by frequency, so to do that I sort the second array, and whatever I do to the second, I do to the first, yeah?
Now this is easy using a bubble sort, because the whole thing relies on comparisons and swaps. Hence, if I am doing one swap, I can simply tell it to do two.
However, a bubble sort is painfully slow when dealing with large amount of data (I am currently testing this on a 160Kb string). Threfore, I am changing my method to use a radix sort, which is the quickest method I know for sorting numbers in this scenario. (Compared to bubble sort, quick sort, and merge sort, which are the other algorithms I have available.)
My problem is that, although I can sort my second array (or the first!) with ease, I cannot keep the association between arrays!
Here is my standard radix sort function:
//----- Radix Sort Array (base 2) --------------------------------------------------------------------------------------
Array.prototype.rsort = function(bits) {
if (!bits) bits = 32
var tempArray = this.copy() // Copy the current array
var b0 = new Array() // Bin for 0 digits
var b1 = new Array() // Bin for 1 digits
for (var i = 0; i < bits; i++) {
var mask = 1 << i // Digit (2^i)
var biggest = 2 << i // If all of tempArray is smaller, we're done
var zeros = 0 // Number of elements in b0, b1
var ones = 0
var found = 0 // Any digits past i?
for (var j = 0; j < tempArray.length; j++) { // Sort into bins b0, b1
if ((tempArray[j] & mask) == 0) {
b0[zeros++] = tempArray[j]
} else {
b1[ones++] = tempArray[j]
}
if (tempArray[j] >= biggest) // Any more digits to sort on?
found = 1
}
for (j = 0; j < zeros; j++) {
tempArray[j] = b0[j] // Concatenate b0, b1 back to tempArray
}
for (j = 0; j < ones; j++) {
tempArray[j + zeros] = b1[j]
}
if (!found) break
}
return tempArray
}
...any ideas how I can change this so that the order of the data array reflects the sorted order of the numbers array? i.e. keeps the association.
::] krycek [::
Now, what I want to do is sort the arrays by frequency, so to do that I sort the second array, and whatever I do to the second, I do to the first, yeah?
Now this is easy using a bubble sort, because the whole thing relies on comparisons and swaps. Hence, if I am doing one swap, I can simply tell it to do two.
However, a bubble sort is painfully slow when dealing with large amount of data (I am currently testing this on a 160Kb string). Threfore, I am changing my method to use a radix sort, which is the quickest method I know for sorting numbers in this scenario. (Compared to bubble sort, quick sort, and merge sort, which are the other algorithms I have available.)
My problem is that, although I can sort my second array (or the first!) with ease, I cannot keep the association between arrays!
Here is my standard radix sort function:
//----- Radix Sort Array (base 2) --------------------------------------------------------------------------------------
Array.prototype.rsort = function(bits) {
if (!bits) bits = 32
var tempArray = this.copy() // Copy the current array
var b0 = new Array() // Bin for 0 digits
var b1 = new Array() // Bin for 1 digits
for (var i = 0; i < bits; i++) {
var mask = 1 << i // Digit (2^i)
var biggest = 2 << i // If all of tempArray is smaller, we're done
var zeros = 0 // Number of elements in b0, b1
var ones = 0
var found = 0 // Any digits past i?
for (var j = 0; j < tempArray.length; j++) { // Sort into bins b0, b1
if ((tempArray[j] & mask) == 0) {
b0[zeros++] = tempArray[j]
} else {
b1[ones++] = tempArray[j]
}
if (tempArray[j] >= biggest) // Any more digits to sort on?
found = 1
}
for (j = 0; j < zeros; j++) {
tempArray[j] = b0[j] // Concatenate b0, b1 back to tempArray
}
for (j = 0; j < ones; j++) {
tempArray[j + zeros] = b1[j]
}
if (!found) break
}
return tempArray
}
...any ideas how I can change this so that the order of the data array reflects the sorted order of the numbers array? i.e. keeps the association.
::] krycek [::