Advertisement
boomzilla

insertion sort drawing

Sep 6th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1.     private static boolean drawColumn (int[][] colorArray, int[] current, int column)
  2.     {
  3.         if (column >= colorArray.length)
  4.         {
  5.             //array is full
  6.             return true;
  7.         }
  8.        
  9.         for (int n = 0; n < current.length; n++)
  10.         {
  11.             colorArray[column][n] = current[n];
  12.             //System.out.println(initial[n]);
  13.         }
  14.         return false;
  15.     }
  16.    
  17.     private static int[][] insertionSort(int length, int[] current)
  18.     {
  19.         //do image for insertion sort
  20.     //length is the desired number of columns to draw
  21.  
  22.     //current is an array of ints which represent colors
  23.     //initially, current is already sorted
  24.        
  25.         int presentColumn = 0;              //track the traversal through the image array
  26.         int rowCount = current.length;      //number of items to sort/number of rows in the
  27.                         //image
  28.  
  29.         //shuffle the sorted array so we can do our own sorting
  30.     current = randomShuffle(current);
  31.        
  32.         //array to be returned:
  33.     //2d matrix of ints which represent colors (8-bit RGB by default)
  34.         int[][] colorArray = new int[length][rowCount];
  35.        
  36.         boolean swapped;
  37.         boolean fullArray = false;  //track whether colorArray is full
  38.  
  39.         //draw unsorted column
  40.         fullArray = drawColumn(colorArray, current, presentColumn);
  41.         presentColumn++;
  42.        
  43.         //do insertion sort
  44.         for (int n = 1; n < (current.length); n++)
  45.         {
  46.             int elem = current[n];
  47.             int indexHole = n;
  48.            
  49.             while(indexHole > 0 && current[indexHole - 1] > elem)
  50.             {
  51.                 fullArray = drawColumn(colorArray, current, presentColumn);
  52.                 presentColumn++;
  53.                 current[indexHole] = current[indexHole - 1];
  54.                 indexHole--;
  55.             }
  56.             fullArray = drawColumn(colorArray, current, presentColumn);
  57.             presentColumn++;
  58.             current[indexHole] = elem;
  59.         }
  60.        
  61.         //the elements are now sorted, but keep drawing until the end of the array is reached
  62.         while (!fullArray)
  63.         {
  64.             fullArray = drawColumn(colorArray, current, presentColumn);
  65.             presentColumn++;
  66.         }
  67.         return colorArray;
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement