Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Java Insertion sort with array

$
0
0
I am having trouble making an insertion sort method. Can someone guide me through the steps

Here is my sortingClass

/*
  SortableArrayListWithBubbleSort implements the abstract operation
  sortSublist using the Bubble Sort Algorithm
  
  author: James W. Benham
  date:   27 January 2013
*/
class SortableArrayListWithBubbleSort<T extends Comparable<T>>
      extends SortableArrayList<T>
{

  public SortableArrayListWithBubbleSort(int capacity)
  {
     super(capacity);
  }// sortarraylistwith
  // Sorts the sublist from lowIndex to highIndex using Bubble Sort
  // Uses recursion to sort smaller sublists
  protected void sortSublist(int lowIndex, int highIndex)
  {



	  for(int i = 1; i < listItem.length; i++){
          int j = i;
          T value = listItem[i];
          while((j > 0) && value.compareTo(listItem[j-1]) < 0){
              listItem[j] = listItem[j-1];
              j--;
          }
          listItem[j] = value;
      }
  
	  
	  
	  
	  
	  
	  /*//selection sort 
		if (lowIndex <= highIndex){					
			T lowestValue = listItem[lowIndex];
			int lowestIndex = lowIndex;
			
			
			for(int i = lowIndex; i <= highIndex; i++){
				if (listItem[i].compareTo(lowestValue) == -1){

					lowestIndex = i;
					lowestValue = listItem[i];
					
				} // end if 
			
			}//end for
			T tempValue;
			tempValue = listItem[lowIndex]; 
			listItem[lowIndex] = listItem[lowestIndex];
			listItem[lowestIndex] = tempValue;
			
			sortSublist(lowIndex+1,highIndex);
			} // end if 
		*/
  } //end sort sublist
  
}//end class 
	  




Here is also the main and sortableArrayList class

main

import java.util.Random;


public class SortableDriver {
	
	
	public static void main(String args[]){
		int capacity = Integer.parseInt(args[0]);
		
		SortableArrayList<Integer> aBag  = new SortableArrayListWithBubbleSort<Integer>(capacity);
		Integer[] RandAry = new Integer[capacity];
		
	    System.out.println("List before sorting ");
	    //Creates array of random integers
		Random r = new Random();
		for(int i = 0; i<capacity; i++){
			
			RandAry[i]=new Integer(r.nextInt(1000));
			//Prints RandAry
			System.out.println(RandAry[i]);
		
		}
		for(int index = 0; index < RandAry.length; index++)
		{
			
	//	aBag.append(RandAry[index]);
		
		}
		//Adds contents of RandAry to bag
		//testAdd(aBag, RandAry);
		//Call sort method
		aBag.sort();
		//After list is sorted print sorted list
		System.out.println(" ");
		System.out.println("Sorted list: ");
		System.out.println(aBag.toString());
		
	}
	//Test the add method
	/*
	private static void testAdd(SortableArrayList<Integer> aBag, Integer[] i){
		for(int index = 0; index<i.length; index++){
			aBag.append(i[index]);
		}
				
	}
	*/

}




class


abstract class SortableArrayList<T extends Comparable<T>>
{
    protected T[] listItem;
	
	protected int itemCount;
	
	private static int DEFAULT_CAPACITY = 50;
	
	private boolean isSorted;
	
	// Constructs a list with the specified capacity
	public SortableArrayList(int capacity)
	{
	   @SuppressWarnings("unchecked")
	   T[] tempList = (T[]) new Comparable[DEFAULT_CAPACITY];
		listItem = tempList;
		itemCount = 0;
		isSorted = false;
   }

   // Constructs a list with the default capacity	
	public SortableArrayList()
	{
	   this(DEFAULT_CAPACITY);
	}
	
   /* Adds item to end of list
	   pre-contition: itemCount < listItem.length() - 1
		post-contition: itemCount == itemCount@pre + 1
		                item added to end of list
	*/	
	public void append(T newItem)
	{
	  listItem[itemCount] = newItem;
	  itemCount++;
	  isSorted = false;  //List may no longer be sorted
	}
	
	/* Return item in given position
	   If position is out of range, return null
	*/
	public T getItem(int position)
	{
	   if (0 <= position && position < itemCount)
		  return listItem[position-1];
		else
		  return null;
	}
	
	// Return length of list
	public int getLength()
	{
	  return itemCount;
	}
	
	// Sort list
	public void sort()
	{
	  sortSublist(0,itemCount-1);
	}
	
	// Sort sublist from lowIndex to highIndex
	abstract protected void sortSublist(int lowIndex, int highIndex);
	
	/*
	  Returns the position of the item with the given value.
	  If the item is not in the list, returns -1.
	*/
	public int search(T searchValue)
	{
	   if (isSorted)
		{
		  return binarySearch(searchValue,0,itemCount-1);
		}
		else
		{
		  return linearSearch(searchValue);
		}
	}
	
	/*
	  Searches for the index of the given value using the binary search algorithm
	*/ 
	private int binarySearch(T searchValue, int lowIndex, int highIndex)
	{
	   if (highIndex < lowIndex)
		{
		  return -1; // not in list
		}
		else
		{
		  int midIndex = lowIndex + (highIndex-lowIndex)/2;
		
		  if (listItem[midIndex].equals(searchValue))
		  {
		    return midIndex;  //found it -- position is one more than index
		  }
		  else if (listItem[midIndex].compareTo(searchValue) > 0)
		  {
		    // searchValue comes before midIndex in array
		    return binarySearch(searchValue,lowIndex,midIndex-1);
		  }
		  else
		  {
		     // listItem[midIndex] comes after midIndex in array
		     return binarySearch(searchValue,midIndex+1,highIndex);
		  }
		}
	}
	
	// Searches for the index of the given value using linear search
	private int linearSearch(T searchValue)
	{
	   int index = itemCount-1;
		
		while (index >= 0 && !listItem[index].equals(searchValue))
		{
		   index--;
		}
		
		return index;
	}
	
	// Returns a string of the items in the list -- 
	public String toString()
	{
	   StringBuilder returnStringBuilder = new StringBuilder();
		
		for (int i = 0; i < itemCount; i++)
		{
		   returnStringBuilder.append(listItem[i].toString() + "\n");
	   }
		
		return returnStringBuilder.toString();
	}	  
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>