Advertisement
Magic249

PanCakeSort

Mar 4th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3. import java.io.*;
  4.  
  5. public class PancakeSortNew {
  6.  
  7.     /**
  8.      * Using an existing scanner, use it to fill an array of integers.
  9.      * Returns the number of values found.
  10.      */
  11.     static int scanIntegerArray( int[] array, Scanner scan )
  12.     {
  13.         int i = 0;
  14.         while( scan.hasNextInt())
  15.             array[i++] = scan.nextInt();
  16.  
  17.         return i;
  18.     }
  19.  
  20.     // returns the index of the largest element in a range of
  21.     // array values. Assumes that last >= first.
  22.     static int findMax( int[] array, int first, int last )
  23.     {
  24.         int index = first;
  25.         int max = array[index];
  26.  
  27.         for( int i = first+1; i <= last; i++)
  28.         {
  29.             if( array[i] > max) {
  30.                 max = array[i];
  31.                 index = i;
  32.             }
  33.         }
  34.  
  35.         return index;
  36.     }
  37.  
  38.     // reverse array elements within a given range
  39.     // Assumes that last >= first.
  40.     static void flip( int[] array, int first, int last )
  41.     {
  42.         int mid = (last-first) / 2 + 1;
  43.         int temp;
  44.  
  45.         for( int i = 0; i < mid; i++ )  {          
  46.             temp = array[last-i];
  47.             array[last-i] = array[first+i];
  48.             array[first+i] = temp;
  49.         }
  50.     }
  51.  
  52.     // prints integer array in space-delimited format
  53.     static void print( int[] array)
  54.     {
  55.         int size = array.length;
  56.         for( int i = 0; i < size; i++ )
  57.             System.out.print( array[i] + " " );
  58.  
  59.         System.out.println();
  60.     }
  61.  
  62.     private static boolean Ordered(int[] array, int size){
  63.         for(int i=0; i < size-1; i++)
  64.             if(array[i] > array[i+1])
  65.                 return false;
  66.         return true;
  67.     }
  68.  
  69.     /**
  70.      * Sorts an array of integers by repeatedly reversing
  71.      * subranges within the array. Prints the flip sequence.
  72.      */
  73.     static void  sortHelper(int[] array, int i)
  74.     {  
  75.         if (i<=0) {
  76.             System.out.println ( 0 );
  77.         }
  78.         int size = array.length;
  79.         if (!Ordered(array, i)){
  80.             int j = findMax(array, 0, i);
  81.             int flipPosition;
  82.  
  83.             if( j != i )
  84.             {
  85.                 if( j != 0 ) {
  86.                     flip( array, 0, j );
  87.                     flipPosition = size-j;
  88.                     System.out.print( flipPosition + " " );
  89.                 }
  90.  
  91.                 flip( array, 0, i );
  92.                 flipPosition = size-i;
  93.                 System.out.print( flipPosition + " " );
  94.             }
  95.         }
  96.  
  97.         sortHelper(array, i-1);
  98.     }
  99.    
  100.     public static void sort (int []array){
  101.         sortHelper(array, array.length-1);
  102.     }
  103.  
  104.     public static void main(String[] args)
  105.     {
  106.         final int MAX_INPUTS = 30;
  107.         int[] array = new int[MAX_INPUTS];
  108.  
  109.         Scanner Input = new Scanner( System.in );
  110.  
  111.         while( Input.hasNextLine() )
  112.         {
  113.             String inputLine = Input.nextLine();
  114.             Scanner scan = new Scanner( inputLine );            
  115.  
  116.             // scan a string with a sequence of integers into an array
  117.             int numbers = scanIntegerArray( array, scan );  
  118.  
  119.             // Store the sequence of flips for later printing. Might be
  120.             // larger than the number of input values. Check for possible
  121.             // blank line in input file.
  122.             if( numbers > 0 )
  123.             {
  124.                 int[] output = new int[numbers];
  125.                 for (int i = 0; i < numbers ; i++){
  126.                     output[i] = array[i];}
  127.                 // print the original array
  128.                 print(output);            
  129.                 // do the flipping
  130.                 sort( output);            
  131.             }
  132.         }
  133.     }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement