Bubble Sort

The first iteration through the array puts the smallest element in the first position but also makes changes to the order of the other elements in the array. The next iteration puts the next smallest element into its correct place and so on.

We start with the element at the bottom of the array. We compare successive pairs of elements, swapping whenever the bottom element of the pair is smaller than the one above it. The smallest element "bubbles up" to the top. The next iteration puts the smallest element in the unsorted part of the array into the second position. And so on.

  
  public static void bubbleSort1 ( int[] anArray )
  {
    int idx, jdx, tmp;
    idx = 0;
    while ( idx < anArray.length - 1 )
    {
      jdx = anArray.length - 1;
      while ( jdx > idx )
      {
         if ( anArray[jdx] < anArray[jdx - 1] )
         {
           tmp = anArray[jdx];
           anArray[jdx] = anArray[jdx - 1];
           anArray[jdx - 1] = tmp;
         }
         jdx = jdx - 1;
      }
      idx = idx + 1;
    }
  }
  

Bubble sort is fairly efficient if the array is partially sorted. One can check if any pair of elements has been swapped. If no pair is swapped then the array is sorted.


  public static void bubbleSort2 ( int[] anArray )
  {
    int idx, jdx, tmp;
    boolean swapped = true;
    idx = 0;
    while ( ( idx < anArray.length - 1 ) && swapped )
    {
      jdx = anArray.length - 1;
      swapped = false;
      while ( ( jdx > idx ))
      {
         if ( anArray[jdx] < anArray[jdx - 1] )
         {
           tmp = anArray[jdx];
           anArray[jdx] = anArray[jdx - 1];
           anArray[jdx - 1] = tmp;
           swapped = true;
         }
         jdx = jdx - 1;
      }
      idx = idx + 1;
    }   
  } 
  

Insertion Sort

This is the method that most card players use to sort their cards. They keep the cards dealt so far in sorted order, and as each new card arrives they insert it into its proper relative position.

To sort the array a[] of N elements in ascending order, start with the first element a[0]. It is already sorted! Then insert a[1] to a[N - 1] to the sorted array by finding its proper place and shifting all other elements.

3 | 1 4 2
1 3 | 4 2
1 3 4 | 2
1 2 3 4 |

  public static void insertionSort1 ( int[] anArray )
  {
    for ( int i = 1; i < anArray.length; i++ )
    {
      for ( int j = i; ( j > 0 ) && anArray[j] < anArray[j-1]; j-- )
      {
        int tmp = anArray[j];
        anArray[j] = anArray[j-1];
        anArray[j-1] = tmp;
      }
    }
  }  

The second version is slightly more efficient. The element under consideration is held in the variable tmp until its proper place is found.


  public static void insertionSort2 ( int[] anArray )
  {
    int i, j, tmp;
    
    for ( i = 1; i < anArray.length; i++ )
    {
      tmp = anArray[i];
      
      for ( j = i; ( j > 0 ) && ( anArray[j - 1] > tmp ); j-- )
        anArray[j] = anArray[j-1];

      anArray[j] = tmp;
    }      
  } 
  

Sequential Search

If an array is unsorted one has to do a sequential search to find the element in the array. The sequential search returns the index of the position of the element in the array otherwise it returns -1.

public int seqSearch ( int[] a, int x )
{
  int idx = -1;

  for ( int i = 0; i < a.length; i++ )
  {
    if ( a[i] == x )
    {
      idx = i;
      break;
    }
  }

  return idx;
}