CodeSOD: Sorting Cabinets |
Sorting. Its a well-studied class of problem, with a number of well-understood solutions. These days, pretty much any time you need to sort a collection, theres a language-or-framework-provided function that handles it for you. Sure, a Better Idiot™ might try and implement their own sorting algorithm from scratch, but your Regular
Idiot™ just has to call .sort
- its Idiot Proof™, right
Well, David S. found a better idiot.
public List getAllCabinets() {
try {
List m = new Vector();
List cabinets = SpecificObjectManager.getAllPrograms();
Iterator it = cabinets.iterator();
while (it.hasNext()) {
CabinetAjax ca = new CabinetAjax();
SearchProgramShell cabinet = (SearchProgramShell) it.next();
ca.setId(cabinet.getCabinetId());
ca.setTitle(cabinet.getCabinetTitle());
Collections.sort(m, new CabinetAjaxTitleComparator());
m.add(ca);
}
return m;
} catch (Exception e) {
log.error(e.toString(), e);
} finally {
HibernateSessionFactory.closeSession();
}
return null;
}
The goal here is to return sorted list of Cabinet
details- which youll note is just an ID and Title, meaning this could just be a map instead of a class, but thats barely a WTF. No, its the call to sort
every time. According to David, there are 12,000 Cabinet objects, so thats 12,000 sorts, with one more element each time. I leave the total Big-O for this implementation up to the reader.
Комментировать | « Пред. запись — К дневнику — След. запись » | Страницы: [1] [Новые] |