RE: Insert Item in Sorted CListCtrl

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



There is no stub in MFC which do binary search.

Binary Search and insert is proper way and I use it in my programs.

Following code is mine which use Data as a CString.

int BinarySearch(CString szSearchData, int nLeft, int nRight, int*
pnRetInsertIndex)
{
int nMiddle;

while( nLeft <= nRight) {
nMiddle = (nLeft + nRight) / 2;

int nCompare = stricmp(szSearchData, m_aryList.GetAt(nMiddle));

if(nCompare == 0) {
return nMiddle;
} else if(nCompare > 0) {
nLeft = nMiddle + 1;
} else {
nRight = nMiddle - 1;
}
}

*pnRetInsertIndex = nLeft;
//printf("Binary Search Return -1 : Left, Right, Middle, %d %d %d\n",
nLeft, nRight, nMiddle);

return -1;
}


--
WebSite :
Realization of Dream { imagine your dream} - http://rodream.net

WebMail :
rodream@xxxxxxxxx


"Nick Meyer" wrote:

Hi,

I have a CListCtrl in report view which I'm sorting by calling SortItems in
response to the HDN_ITEMCLICK notification. I pass my own comparison
function and a struct that contains the index of the sorted column and a
sorting direction (ascending or descending) to SortItems.

Since MFC has no knowledge of my sorting scheme, what's the easiest / most
efficient way to determine the proper place to insert a new item in the
control when the items are already sorted? Am I stuck doing a binary search
myself, is there some functionality built into MFC?

Thanks,
Nick Meyer
.



Relevant Pages

  • Re: Shuffle algorithm
    ... public static void Sort(ref ArrayList tabel, SortOrder s, ColumnType t, int ... private static void QuickSort (ref ArrayList szArray, ref SortOrder s, ref ... while (nLeft <= nRight) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [C++] Binary Search Confusion
    ... This is a SIMPLE binary search which I am currently learning about ... int binarySearch ... I have a 15 element array, ... How do you find an entry in a phone book? ...
    (alt.comp.lang.learn.c-cpp)
  • cp(1) fails to copy file from /proc
    ... I'm not sure wheter this is a bug or a feature... ... int in, out; ... int nread, nwritten, nleft; ... bufp += nwritten; ...
    (Debian-User)
  • Re: bit shifts across array elements
    ... int n = 0; ... a binary search as above. ... Use a "big bite" linear search to reduce T to a convenient ...
    (comp.lang.c)
  • Re: CurrencyManager Relations
    ... > "IndexOf" for a given primary key field value. ... > a genic one, something like int FindKey. ... then you could even do a binary search to find the index of the row. ...
    (microsoft.public.dotnet.languages.csharp)