quicksort zum sortieren eines mehrdimensionalen Arrays gesucht
jweller
- javascript
Hallo,
ich benötige den code von einem quicksort welcher mir ermöglicht, ein mehrdimensionales Array alphabetich nach einer bestimmten Spalte zu sortieren.
Leider habe ich nur den code von einem quicksort gefunden, welcher normale Arrays sortiert.
Deshalb wollte ich hier Anfragen, ob mir jemand diesen umschreiben könnte, damit man mehrdimensionale Arrays nach einer bestimmten Spalte ab- bzw. aufwährts sortieren kann.
Oder ob jemand schon solchen Code zu Hand hätte und mir diesen zusenden könnte.
Viele Grüße JWeller
--- code ---
function myInsSort(arr,length) {
var j, i, v;
for (i=1, j=i, v=arr[i]; i<length; arr[j+1]=v, i++, j=i, v=arr[i])
while(j-- >= 0 && arr[j] > v)
arr[j+1]=arr[j];
}
function myQuickSortSub(arr, first, last) {
// The value of lim in the following line determines the minimum length of sequences to be sorted.
// Shorter sequences are left for Insertion Sort to deal with later. This speeds up sorting.
var i, j, m, p, temp, lim=8, lim1=lim + 1;
while(first>=0) {
i=first - 1; j=last;
// This block finds the median of the first, last and middle value, and uses it as a pivot value (p)
m=Math.floor(first + (last-first)/2);
if( arr[m] < arr[first]) {
temp=arr[m]; arr[m]=arr[first]; arr[first]=temp;
}
if( arr[last] < arr[m]) {
temp=arr[m]; arr[m]=arr[last]; arr[last]=temp;
}
if( arr[m] < arr[first]) {
temp=arr[m]; arr[m]=arr[first]; arr[first]=temp;
}
p=arr[m];
// Do the actual sorting
while ( ++i < j)
if ( arr[i] > p) {
while(arr[j] > p && --j > i);
if( j > i) {
temp=arr[j]; arr[j]=arr[i]; arr[i]=temp;
}
}
// The sorting has divided the array into two parts, where all the elements in the first part
// are smaller than the elements in the second part. Now, sort the shortest part of the array
// with a recursive call to this routine, and the longest part with another iteration. If we
// would use recursion for both parts, the worst case stack depth is O(n), now it's O(log n).
// If a part is shorter than lim, just leave it unsorted -- Insertion Sort will take care of
// it after Quick Sort has completed.
if( --i - first > last - i - 1) {
if( last > i + lim1)
myQuickSortSub(arr, i + 1, last);
if( i > first + lim)
last=i;
else
first=-1; // Signal to end function
} else {
if( i > first + lim)
myQuickSortSub(arr, first, i);
if( last > i + lim1)
first=i+1;
else
first=-1; // Signal to end function
}
}
}
function myQuickSort(arr, length) {
myQuickSortSub( arr, 0, length - 1);
myInsSort(arr, length);
}
Hallo,
wenn Du Spaß am Programmieren hast, kannst Du den Quicksort natürlich selbst schreiben. Aber Javascript kann auch Sortieren.
Ist A ein 1D-Array, kann es mit A.sort() sortiert werden.
Ist A ein 2D-Array, musst du der Sortierfunktion mitteilen, nach welcher Größe, in diesem Fall nach welcher Spalte sortiert werden soll:
A.sort(vglFkt);
und
function VglFkt(a,b) {
var ta=a[spaltennummer],tb=b[spaltennummer];
if (ta>tb) return 1;
else if (ta<tb) return -1;
else return 0;
}
}
Gruß, Jürgen