1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Java:Insertion Sort

Discussion in 'Java' started by cyrow, Sep 22, 2008.

  1. cyrow

    cyrow New Member

    Joined:
    Nov 19, 2007
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    import java.text.DecimalFormat;
    
    public class Account{
    	//class Variables
    	private static int NumAccounts = 0;
    	private static final double Interest = 0.05;
    	
    	//Instant Variables
    	private  long accNum;
    	private  double balance;
    	
    	DecimalFormat x = new DecimalFormat("$00,000.00");//Format Balance
    	
    	public Account(long accN, double bal){//Constructor
    		accNum  = accN;
    		balance = bal;
    		NumAccounts++;
    	}
    	
    	public int GetNumAccounts(){
    		return NumAccounts;
    	}
    	
    	public double getBalance(){
    		return balance;
    	}
    	
    	public long getAccountNum(){
    		return accNum;
    	}
    	
    	public void debit(double amount){
    		if(balance >= amount){
    		   balance -= amount;	
    		}
    	}
    	
    	
    	public void credit(double amount){
    		balance += amount;
    	}
    	
    	
    	public String toString(){
    		String Str;
    		
    		Str ="Account Number   :" + accNum + "\n"+
    			 "Account Balance  :" + x.format(balance) + "\n";
    		return Str;
    	}
    }//End of class Account
    public class TestAccountxx{
    private static  final int Max = 1000;
    static Account[]accList= new Account[Max];
    static int u = 0;
    
    
    public static int getAccount(int accNum){//using sequential search to find and return account if exist
       for(int j = 0; j < accList.length; j++){
           if(accList[j] != null) {
               if(accList[j].getAccountNum() == accNum)
                   //return accList[j];
                   return j;
           }
       }  // end for
       return -1;
    
    }	
    
    public static void insertIntoArray(Account p)
      {//Insert Objects into account
      
    		 accList[u] = p;
    		 u++;
      }		 
    
      public static void deleteAccount(int accNum)//delete account from accList
       {
          int size = accList.length;   	
       	
           for(int i = accNum; i < size; i++){
           	  accList[i] = accList[i + 1];
           	  size--;
           }
       }
    
    
    public static void transaction(int accNum, double amount,int transType)
       {
           if (transType == 1){
            accList[accNum].debit(amount);
           }
               
           if (transType == 2){
            accList[accNum].credit(amount);
           }     
       }
       
    
     public static void insertion_srt(){
     	int lo, hi;
     	lo = 0;
     	hi = accList.length;
     	
     	for(int j = lo + 1; j <= hi; j++){
     		Account hold = accList[j];
    
     		int k = j - 1;
     		while(k >= 0 && hold.getAccountNum().compareTo(accList[k].getAccountNum())< 0){
     		accList[k+1] = accList[k];
     		--k;	
     		}//End While
     		accList[k + 1] = hold;
     	}//End For
     }//End Insertion Sort
     	
     	
     	
     	
     	
    
    
    
    
    
    	public static void main(String[] args){
    	  
    	Account	a = new Account(123456,5255.17);
    	insertIntoArray(a);
    	insertion_srt();
    	           
    	Account	b = new Account(188468,19218.20);
    	insertion_srt();
    	insertIntoArray(b);
    	
    	Account	c = new Account(217794,1002.62);
    	insertIntoArray(c);
    	
    	Account	d = new Account(998283,20203.75);
    	insertIntoArray(d);
    		
    	
    
    
     //Create Transactions
     		int x = getAccount(123456);
     		transaction(x,1255.17,1);
     		
     	
     		int y = getAccount(188468);
     		transaction(y,2781.80,2);
     		
     		
     		int z = getAccount(217794);
            transaction(z,2000.00,1);
            
            
            int n = getAccount(998283);
            transaction(n,125.00,1);
            
            int w = getAccount(998283);
            deleteAccount(w);
            
            int p = getAccount(123457);
            if(p ==-1){
            	System.out.println("Account 123457 does not exist");
            }else{
            	deleteAccount(p);
            }
            
           
            
    	for(int i = 0; i < 3; i++){
    		System.out.println(accList[i].toString());
    	}
    	}//end main
    }//end class TestAccount
    
    
    What I am attempting to do is as i create the accounts and insert them into the array, I want to keep them in order using an Insertion Sort. I am not able to get it to work. Please help.
     
  2. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    Your insertion sort should be more like this:
    Code:
    public static void insertion_sort()
    {
      for( int j = 1; j < accList.length; ++j )
      {
        for( int k = j - 1;
             k >= 0 && accList[j].getAccountNum() < accList[k].getAccountNum();
             --k )
        {
          accList[k+1] = accList[k];
        }
        accList[k + 1] = accList[j];
      }
    }
    And your deleteAccount function should be more like this:
    Code:
    public static void deleteAccount( int acc_index )
    {
      for( int i = acc_index; i < accList.length - 1; ++i )
      {
        accList[i] = accList[i + 1];
      }
      // Now you must reduce the length of the accList array by one,
      // i.e., the last element must be deleted from the array object.
      // I do not know how that is done in Java.
      // So you need to do what the following line says,
      // but in the Java idiom.
      delete( accList[accList.length - 1] );
    }
     

Share This Page