c# - Simultaneous Sorting/Removing data from a List -
currently have list of integers. list contains index values point "active" objects in another, larger list. if smaller list of "active" values becomes large, triggers loop iterates through small list , removes values have become inactive. currently, removes them ignoring inactive values , adding them second list (and when second list gets full again same process repeated, placing them first list , on).
after trigger occurs, list sorted using quicksort implementation. fine , dandy.
-------question---------
however, see potential gain of speed. imagining combining removal of inactive values while sorting taking place. unfortunately, cannot find way implement quicksort in way. because quicksort works pivots, means if values removed list, pivot try access slot in list not exist, etc etc.. (unless i'm thinking wrong).
so, ideas on how combine 2 operations? can't seem find sorting algorithms fast quicksort handle this, or perhaps i'm not seeing how implement quicksort... hints appreciated!
code better understanding of whats going on: (current conditions: values can range 0 2 million, no 2 values same, , in general sorted, since sorted every often)
if (deactive > 50000)//if number of inactive coordinates greater 50k { (int = 0; < activecoords1.count; i++) { if (largearray[activecoords[i]].active == true)//if coordinate active, readd list { activecoords2.add(activecoords1[i]); } } //clears old list future use activecoords1.clear(); deactive = 0; //sorts new list quicksort(activecoords2, 0, activecoords2.count() - 1); } static void quicksort(list<int> elements, int left, int right) { int = left, j = right; int pivot = elements[(left + right) / 2]; while (i <= j) { // p < pivot while (elements[i].compareto(pivot) < 0) { i++; } while (elements[j].compareto(pivot) > 0) { j--; } if (i <= j) { // swap int tmp = elements[i]; elements[i] = elements[j]; elements[j] = tmp; i++; j--; } } // recursive calls if (left < j) { quicksort(elements, elements, left, j); } if (i < right) { quicksort(elements, elements, i, right); } }
it sounds might benefit using red-black tree (or balanced binary tree), search, insert , delete time o(log n). tree sorted there no 1 off big hits incurred re-sort.
what split in terms of types of access (search, insert, delete) , constraints reach?
Comments
Post a Comment