Advertisement
89yoyos

Coin-Flip Sort Java Code 2: This Time, It's Personal!

Oct 23rd, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. package coinFlipSort;
  2.  
  3. public class coinFlipSort {
  4.     public static void main ( String[] args ){
  5.        
  6.        
  7.         int flips = 1000;
  8.         String s = "";
  9.         int[] arr  = new int [flips+2];
  10.         int[] arr2 = new int [flips+2];
  11.        
  12.         for (int i = 0; i<flips; i++){
  13.             s = s + Integer.toString(((int) Math.ceil(((Math.random()*10)%2)-1))).replace("0", "H").replace("1", "T");
  14.         }
  15.        
  16.         System.out.println(s);
  17.        
  18.         for (int i = flips; i>0; i--){
  19.  
  20.             String s2 = "";
  21.             String s3 = "";
  22.            
  23.             for (int x = 1; x <= i; x++){
  24.                 s2 = s2 + "H";
  25.                 s3 = s3 + "T";
  26.             }
  27.            
  28.             int count = 0;
  29.            
  30.             while (s.indexOf(s2)>=0){
  31.                
  32.                 count++;
  33.                 s=s.replaceFirst(s2, Integer.toString(i));
  34.                
  35.             }
  36.            
  37.             arr[i]=count;
  38.            
  39.             count = 0;
  40.            
  41.             while (s.indexOf(s3)>=0){
  42.                
  43.                 count++;
  44.                 s=s.replaceFirst(s3, Integer.toString(i));
  45.                
  46.             }
  47.            
  48.             arr2[i]=count;
  49.            
  50.         }
  51.        
  52.         //Print
  53.        
  54.         System.out.println("-----------------------------------------");
  55.         System.out.println("#\t|\tHeads\t|\tTails\t|");
  56.         System.out.println("-----------------------------------------");
  57.        
  58.         for (int i = flips; i>0; i--){
  59.             if(arr[i]>0){
  60.                 System.out.println(Integer.toString(i) + "\t|\t" + Integer.toString(arr[i]) + "\t|\t" + Integer.toString(arr2[i]) + "\t|");
  61.             }
  62.         }
  63.        
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement