package com.mmnaseri.cs.clrs.ch02.s2;

import com.mmnaseri.cs.clrs.common.ArrayUtils;
import com.mmnaseri.cs.clrs.common.Sorter;
import com.mmnaseri.cs.qa.annotation.Quality;
import com.mmnaseri.cs.qa.annotation.Stage;

import java.util.Comparator;

/**
 * @author Mohammad Milad Naseri (m.m.naseri@gmail.com)
 * @since 1.0 (5/26/15, 2:49 AM)
 */
@Quality(Stage.TESTED)
public class SelectionSorter<E extends Comparable<E>> implements Sorter<E> {

    private final Comparator<E> comparator;

    public SelectionSorter(Comparator<E> comparator) {
        this.comparator = comparator;
    }

    @Override
    public void sort(E[] items) {
        for (int i = 0; i < items.length; i ++) {
            int localMinimum = -1;
            for (int j = i; j < items.length; j ++) {
                if (localMinimum < 0 || comparator.compare(items[j], items[localMinimum]) < 0) {
                    localMinimum = j;
                }
            }
            ArrayUtils.swap(items, i, localMinimum);
        }
    }

}

@@ -122,16 +122,65 @@
 Sorter;%0A
+import com.mmnaseri.cs.qa.annotation.Complexity;%0A
 import c
@@ -213,24 +213,24 @@
 on.Quality;%0A
-
 import com.m
@@ -408,11 +408,15 @@
 age.
-TES
+DOCUMEN
 TED)
@@ -647,17 +647,346 @@
     
-@Override
+/**%0A     * This method implements the %22selection%22 sort algorithm by considering the shrinking tail of the array (which contains%0A     * the full array at the start) and then finding the local minimum and placing it at the beginning of the array.%0A     * @param items    the items to be sorted%0A     */%0A    @Override%0A    @Complexity(%22O(n%5E2)%22)
 %0A   

add documentation and complexity analysis to all classes and methods