PDA

View Full Version : When sort the lists?



SkripT
13th April 2006, 23:58
Hi all, In my app I use a QTabWidget with 6 tab widgets. Each of them consists in a QListWidget. I fill these lists with the thumbnailed images from a list of files (assigning a list using the image format/type). I want all the QListWidgets to be sorted in ascending order using the name of the image file. Also, I want to set in the text of each tab the name of the format/type of the images that its list contains and the number of images in the list in parenthesis. My question is: when do you think that could be better/cleaner to sort the lists and set the text of the tabs?
A) After inserting each image as QListWidgetItem in the correct QListWidget on the loop that reads each file from the list of files to insert in the lists.
B) Once all the images in the list of files have been inserted in the lists.
In the first case, every time that a new image is inserted in a QListWidget, this list is sorted (which could be inefficient) and fixed the text of the tab. But, differing from the second option, it's not necesary to loop through all the tabs at the end to do it. What do you think that's better?

jacek
14th April 2006, 00:21
Sort that list of files or insert items in the Right Place(tm).

SkripT
14th April 2006, 00:28
Sort that list of files or insert items in the Right Place(tm).

Sorry I mean sort the QListWidgets with QListWidget::sortItems in ascending order

SkripT
14th April 2006, 11:50
Any suggestion, please?

wysota
14th April 2006, 12:00
I think Jacek's answer still stands :) If you insert them in a proper order, you won't have to sort them.

SkripT
14th April 2006, 14:15
I think Jacek's answer still stands :) If you insert them in a proper order, you won't have to sort them.

Yes you both are right. But suppose the case that the list is not empty and you insert new items (at the end of the list). In this case what's the best way?

wysota
14th April 2006, 16:30
A) After inserting each image as QListWidgetItem in the correct QListWidget on the loop that reads each file from the list of files to insert in the lists.

This is exactly the same as inserting the item in the proper place. The cost of such operation depends on the way you insert those items. In the worst case you have to make (x-1) comparisons for each item and you are inserting n items, so it gives O(n^2).


B) Once all the images in the list of files have been inserted in the lists.
This is a single sort operation with O(nlgn) cost.

B is faster, A is better if you update the list during the fill, because you always have a sorted list.