![]() |
alfabetical order in array
I have dinamic array.
All the elements are diffrent. Question ? how to sort them in alfabetical order. Of course you can put data into sheet, sort and reload the array but it takes time so maybe somebody knows better solutions. Tks |
alfabetical order in array
This this post by Keith Willshaw was meant for you:
There's a nice quicksort routine on Stephen Bullen's page http://www.bmsltd.co.uk/Excel/SBXLPage.asp Keith -- Regards, Tom Ogilvy "Konrad" wrote in message ... I have dinamic array. All the elements are diffrent. Question ? how to sort them in alfabetical order. Of course you can put data into sheet, sort and reload the array but it takes time so maybe somebody knows better solutions. Tks |
alfabetical order in array
QuickSort is usually a fast algorithm, but can degrade to O(N^2)
behavior (i.e., require time proportional to the square of the number of items sorted) under certain conditions. The QuickSort on Steve Bullen's page does not contain the optimization (median of three partitioning) needed to make such misbehavior unlikely. QuickSort is also unstable -- sorting on each field of a record will lose any ordering based on previous fields. MergeSort is almost as fast as QuickSort, but is guaranteed always to sort in O(N log N) time, no matter what. Furthermore, it's stable; if you sort on field A and then field B, records with the same value of field B will remain sorted as to field A. Because of its stability and consistent performance, MergeSort is often used for system sorts. Its one drawback is the need for an extra array for the merge operations. The following code will sort an array of variants into ascending order. It works by dividing the array into short runs, sorting them by InsertionSort (the most efficient sort for short lists) and then merging pairs of runs until only a single run is left. It's long but fast, and less complicated than it looks. Dave Ring Sub MergeSort(A()) Dim B(), Length&, nRuns&, Stack() As Long Dim I&, L&, R&, LP&, RP&, OP&, TMP Length = UBound(A) ReDim B(1 To Length) nRuns = 1 'Divide the array into short runs While Length 20 Length = Length / 4 nRuns = nRuns * 4 Wend ReDim Stack(1 To nRuns) For I = 1 To nRuns - 1 Stack(I) = 1 + (Length * CDbl(I)) Next I Stack(nRuns) = Length 'Sort the short runs by InsertionSort L = 1 For I = 1 To nRuns R = Stack(I) For RP = L + 1 To R TMP = A(RP) For LP = RP - 1 To L Step -1 If TMP < A(LP) Then A(LP + 1) = A(LP) Else Exit For Next LP A(LP + 1) = TMP Next RP L = R + 1 Next I 'Merge pairs of runs until only one is left While nRuns 1 'Forward merge from array A to auxiliary array B R = 0 For I = 2 To nRuns Step 2 LP = R + 1 OP = LP L = Stack(I - 1) RP = L + 1 R = Stack(I) Do If A(LP) <= A(RP) Then B(OP) = A(LP) OP = OP + 1 LP = LP + 1 If LP L Then Do B(OP) = A(RP) OP = OP + 1 RP = RP + 1 Loop Until RP R Exit Do End If Else B(OP) = A(RP) OP = OP + 1 RP = RP + 1 If RP R Then Do B(OP) = A(LP) OP = OP + 1 LP = LP + 1 Loop Until LP L Exit Do End If End If Loop Stack(I \ 2) = R Next I nRuns = nRuns \ 2 'Backward merge from auxiliary array B to A R = 0 For I = 2 To nRuns Step 2 LP = R + 1 OP = LP L = Stack(I - 1) RP = L + 1 R = Stack(I) Do If B(LP) <= B(RP) Then A(OP) = B(LP) OP = OP + 1 LP = LP + 1 If LP L Then Do A(OP) = B(RP) OP = OP + 1 RP = RP + 1 Loop Until RP R Exit Do End If Else A(OP) = B(RP) OP = OP + 1 RP = RP + 1 If RP R Then Do A(OP) = B(LP) OP = OP + 1 LP = LP + 1 Loop Until LP L Exit Do End If End If Loop Stack(I \ 2) = R Next I nRuns = nRuns \ 2 Wend End Sub Konrad wrote: I have dinamic array. All the elements are diffrent. Question ? how to sort them in alfabetical order. Of course you can put data into sheet, sort and reload the array but it takes time so maybe somebody knows better solutions. Tks |
All times are GMT +1. The time now is 01:20 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com