Dimenticare

Sorting strings and integers and things

Mar 7th, 2013
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. package APCS;
  2.  
  3. import Development.Randomness;
  4.  
  5. import java.util.Random;
  6.  
  7. public class Organization {
  8.     public static Random r = new Random();
  9.  
  10.     public static void sortIntegers(int amount, int limit) {
  11.         int[] set = new int[amount];
  12.         int storage;
  13.         boolean ordered = false;
  14.         for (int i = 0; i < amount - 1; i++) {
  15.             set[i] = r.nextInt(limit);
  16.         }
  17.  
  18.         for (int i = 0; i < set.length; i++)
  19.             System.out.print(set[i] + ", ");
  20.  
  21.         System.out.println();
  22.  
  23.         while (!ordered) {
  24.             ordered = true;
  25.             for (int i = 0; i < set.length - 1; i++) {
  26.                 if (set[i] < set[i + 1]) {
  27.                     storage = set[i];
  28.                     set[i] = set[i + 1];
  29.                     set[i + 1] = storage;
  30.                     ordered = false;
  31.                 }
  32.             }
  33.         }
  34.  
  35.         for (int i = 0; i < set.length; i++)
  36.             System.out.print(set[i] + ", ");
  37.     }
  38.  
  39.     public static void sortStrings(int amount, int stringLength) {
  40.         // Case insensitive
  41.         String[] set = new String[amount];
  42.         boolean ordered = false;
  43.         String storage;
  44.         for (int i = 0; i < set.length; i++) {
  45.             // There are many ways to get random strings
  46.             set[i] = Randomness.nextString(stringLength);
  47.             System.out.println(set[i]);
  48.         }
  49.        
  50.         System.out.println();
  51.  
  52.         while (!ordered) {
  53.             ordered=true;
  54.             for (int i = 0; i < set.length - 1; i++) {
  55.                 if (set[i].compareTo(set[i + 1]) > 0) {
  56.                     storage=new String(set[i]);
  57.                     set[i] = new String(set[i + 1]);
  58.                     set[i + 1] = new String(storage);
  59.                     ordered=false;
  60.                 }
  61.             }
  62.         }
  63.        
  64.         for (int i=0; i<set.length; i++)
  65.             System.out.println(set[i]);
  66.     }
  67.  
  68.     public static void main(String[] pie) {
  69.         // sortIntegers(20, 100);
  70.         sortStrings(20, 10);
  71.     }
  72. }
  73.  
  74. /*
  75. package Development;
  76.  
  77. import java.util.Random;
  78.  
  79. public class Randomness {
  80.     public static String nextString(int length) {
  81.         Random r = new Random();
  82.         String a = "";
  83.         char[] set = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
  84.                 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
  85.                 'x', 'y', 'z' };
  86.         for (int i = 0; i < 40; i++) {
  87.             a += set[r.nextInt(26)];
  88.         }
  89.         return a;
  90.     }
  91. }
  92.  */
Advertisement
Add Comment
Please, Sign In to add comment