Kesden's Source Code for Several Common Sorts


public void bubbleSort(int[] numbers)
{
     /*traverse the array (or a subset of the array) length-1 times*/
     for (int highest_unsorted=numbers.length-1; highest_unsorted != 0; highest_unsorted--)
     {    
      	/*makes the necessary comparisons for one pass through the array*/	
      	for (int best_so_far=0; best_so_far < highest_unsorted; best_so_far++)
      	{
        		/*compare adjacent items and swap them if necessary*/
        		if (numbers[best_so_far] > numbers[best_so_far+1])
          			swapNumbers (best_so_far, best_so_far+1);
      	}
     }
}
public void selectionSort(int[] numbers)
{
    /*For every index in the array, with the exception of the last one,*/
    for(int searcherIndex=0; searcherIndex < numbers.length-1; searcherIndex++)
    {
      	/*Assume that the number is where it's supposed to be*/
      	int correctIndex = searcherIndex;
      	
      	/*Try out other candidate indexes*/
      	for (int candidateIndex=searcherIndex+1; candidateIndex < numbers.length; candidateIndex++)
      	{
      		/*If you find a smaller number, make it's index the correct index*/
      		if(numbers[candidateIndex] < numbers[correctIndex])
        			correctIndex = candidateIndex;
      	}
      	/*At this point correctIndex really will be the correct index*/
      	swapNumbers (searcherIndex, correctIndex);
    }
}
 public void insertionSort(int[] numbers)
  {
    /*going through all of the items in the array*/
    for (int insertMe=1; insertMe < numbers.length; insertMe++)
    {
      /*find the correct index for the item*/
      for (int newPosn=0; newPosn < insertMe; newPosn++)
      {
        /*stop when you come to an item greater than the item in question*/
        if (numbers[insertMe] < numbers[newPosn])
        {
          /*put the item in question somewhere for safe keeping*/
	  int temp = numbers[insertMe];
	  
          /*move everything after the correct index down to make room*/
          for (int shift=insertMe; shift > newPosn; )
            numbers[shift] = numbers[--shift];

	  /*put the item in its correct index*/
          numbers[newPosn] = temp;

	  /*You've found the right index for the item and it's time to stop*/
          break;
        }
      }
    }
  }
/*
 *quickSort() calls sortPartition()
 */
public void quickSort(int[] numbers)
{
    sortPartition (0, numbers.length-1);
}

/*
 * This is a helper method which is used by findPartition() to 
 * find the "pivot point" - the place to divide the partition.
 */
private int findPivot (int left, int right)
{
    return ((right + left)/2);
}

/*
 * This is a helper method called by sortNumbers(). It sorts
 * an individual partition about the given pivot point.
 */
private int partition (int left, int right, int pivot)
{
    do
    {
      while (numbers[++left] < numbers[pivot]);
      while ((right !=0) && (numbers[--right] > numbers[pivot]));
      swapNumbers (left, right);
    } while (left < right);

    swapNumbers (left, right);

    return left;
}

/*
 * This is a helper method called by sortNumbers(). It recursively 
 * calls itself on subpartitions to sort the numbers. The actual
 * sorting within the partition is done by sortPartition(), which
 * is iterative.
 */
private void sortPartition (int left, int right)
{
    int pivot = findPivot (left, right);
    swapNumbers (pivot, right);
    int newpivot= partition (left-1, right, right);
    swapNumbers (newpivot, right);

    if ((newpivot-left) > 1) sortPartition (left, newpivot-1));
    if ((right - newpivot) > 1) sortPartition (newpivot+1, right));
}