InsertionSort.java :  » App » dr11absap » fer » sortko » com » Android Open Source

Android Open Source » App » dr11absap 
dr11absap » fer » sortko » com » InsertionSort.java
package fer.sortko.com;

public class InsertionSort extends Algorithm {

  private AlgorithmPosition lastChangePosition = null;
  private AlgorithmPosition positionToReturn = null;
  private static int helpVariableIndex = 8; 
  private boolean positionReturned = false;
  private int switchCount = 0;
  private int[] A = null;
  private int i = 0;
  private int j = 0;
  private int N = super.getNumbersCopy().length;
  
  public InsertionSort (int numberOfElements){
    super(numberOfElements);
    super.NEEDS_HELP_VARIABLE = true;
  }

  public AlgorithmPosition findSwitch(){
    A = super.getNumbersCopy();
    switchCount = 0;
    this.help = 0;
    i = 0;
    j = 0;
    
    this.lastChangePosition = new InsertionSortPosition(0, 0, A, 0, 0);
    this.positionToReturn = new InsertionSortPosition(0, 0, A, 0, 0);
    this.positionToReturn.setPreviousAlgorithmPosition(lastChangePosition);

    for (i = 1; i < N; i++){
      SetAlgorithmPosition(i, helpVariableIndex, i);
      this.help = A[i];
      for (j = i; j >= 1 && A[j-1] > help; j--){
        SetAlgorithmPosition(j-1, j, i);
        A[j] = A[j-1];
      }
      SetAlgorithmPosition(helpVariableIndex, j, i);
      A[j] = help;
    }
    return this.positionToReturn;
  }
  private void SetAlgorithmPosition(int i, int j, int outerLoopIndex){
    switchCount++;
    if (super.switchNumber == switchCount){
      positionToReturn = new InsertionSortPosition(i, j, this.A.clone(), this.help, outerLoopIndex);
      positionToReturn.setPreviousAlgorithmPosition(this.lastChangePosition);
      positionReturned = true;
    }
    if(!positionReturned){
      this.lastChangePosition = new InsertionSortPosition(i, j, this.A.clone(), this.help, outerLoopIndex);
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.