-Поиск по дневнику

Поиск сообщений в rss_thedaily_wtf

 -Подписка по e-mail

 

 -Постоянные читатели

 -Статистика

Статистика LiveInternet.ru: показано количество хитов и посетителей
Создан: 06.04.2008
Записей:
Комментариев:
Написано: 0


CodeSOD: Sorting Cabinets

Четверг, 24 Сентября 2015 г. 13:30 + в цитатник

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.

http://thedailywtf.com/articles/sorting-cabinets

Метки:  

 

Добавить комментарий:
Текст комментария: смайлики

Проверка орфографии: (найти ошибки)

Прикрепить картинку:

 Переводить URL в ссылку
 Подписаться на комментарии
 Подписать картинку