not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.2 KiB

  1. package net.jrtechs.www.Sorting;
  2. import net.jrtechs.www.DataStructures.Lists.ArrayList.ArrayBackedList;
  3. /**
  4. * Created an array list which is able to sort
  5. * itself upon request.
  6. *
  7. * @param <E>
  8. * @author Jeffery Russell
  9. */
  10. public class SortableList<E extends Comparable> extends ArrayBackedList<E>
  11. {
  12. /** Constant for an increasing list */
  13. public final int INCREASING_ORDER = 1;
  14. /** Constant for a decreasing list */
  15. public final int DECREASING_ORDER = -1;
  16. /**
  17. * Creates a new list of default size
  18. */
  19. public SortableList()
  20. {
  21. super();
  22. }
  23. /**
  24. * Creates a new list of non-default size
  25. */
  26. public SortableList(int size)
  27. {
  28. super(size);
  29. }
  30. /**
  31. * Sorts the list using insertion sort.
  32. *
  33. * @param sortOrder constant corresponding to sort type
  34. */
  35. public void insertionSort(int sortOrder)
  36. {
  37. for(int i = 1; i < this.size(); i++)
  38. {
  39. int reverseIndex = i;
  40. while(reverseIndex > 0 && this.get(reverseIndex-1)
  41. .compareTo(this.get(reverseIndex)) == sortOrder)
  42. {
  43. this.swap(reverseIndex, reverseIndex-1);
  44. reverseIndex--;
  45. }
  46. }
  47. }
  48. }