Dimenticare icon

Sorting strings and integers and things

Dimenticare | PRO | 03/08/13 02:20:23 AM UTC | 0 ⭐ | 533 👁️ | Never ⏰ | []
Java |

2.03 KB

|

None

|

0 👍

/

0 👎

package APCS;
 
import Development.Randomness;
 
import java.util.Random;
 
public class Organization {
    public static Random r = new Random();
 
    public static void sortIntegers(int amount, int limit) {
        int[] set = new int[amount];
        int storage;
        boolean ordered = false;
        for (int i = 0; i < amount - 1; i++) {
            set[i] = r.nextInt(limit);
        }
 
        for (int i = 0; i < set.length; i++)
            System.out.print(set[i] + ", ");
 
        System.out.println();
 
        while (!ordered) {
            ordered = true;
            for (int i = 0; i < set.length - 1; i++) {
                if (set[i] < set[i + 1]) {
                    storage = set[i];
                    set[i] = set[i + 1];
                    set[i + 1] = storage;
                    ordered = false;
                }
            }
        }
 
        for (int i = 0; i < set.length; i++)
            System.out.print(set[i] + ", ");
    }
 
    public static void sortStrings(int amount, int stringLength) {
        // Case insensitive
        String[] set = new String[amount];
        boolean ordered = false;
        String storage;
        for (int i = 0; i < set.length; i++) {
            // There are many ways to get random strings
            set[i] = Randomness.nextString(stringLength);
            System.out.println(set[i]);
        }
        
        System.out.println();
 
        while (!ordered) {
            ordered=true;
            for (int i = 0; i < set.length - 1; i++) {
                if (set[i].compareTo(set[i + 1]) > 0) {
                    storage=new String(set[i]);
                    set[i] = new String(set[i + 1]);
                    set[i + 1] = new String(storage);
                    ordered=false;
                }
            }
        }
        
        for (int i=0; i<set.length; i++)
            System.out.println(set[i]);
    }
 
    public static void main(String[] pie) {
        // sortIntegers(20, 100);
        sortStrings(20, 10);
    }
}
 
/*
package Development;
 
import java.util.Random;
 
public class Randomness {
    public static String nextString(int length) {
        Random r = new Random();
        String a = "";
        char[] set = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
                'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
                'x', 'y', 'z' };
        for (int i = 0; i < 40; i++) {
            a += set[r.nextInt(26)];
        }
        return a;
    }
}
 */

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎