当前位置: 首页>>代码示例>>Java>>正文


Java StdRandom.shuffle方法代码示例

本文整理汇总了Java中edu.princeton.cs.introcs.StdRandom.shuffle方法的典型用法代码示例。如果您正苦于以下问题:Java StdRandom.shuffle方法的具体用法?Java StdRandom.shuffle怎么用?Java StdRandom.shuffle使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在edu.princeton.cs.introcs.StdRandom的用法示例。


在下文中一共展示了StdRandom.shuffle方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Reads in a sequence of strings from standard input; quicksorts them; 
 * and prints them to standard output in ascending order. 
 * Shuffles the array and then prints the strings again to
 * standard output, but this time, using the select method.
 */
public static void main(String[] args) {
    String[] a = StdIn.readAllStrings();
    Quick.sort(a);
    show(a);

    // shuffle
    StdRandom.shuffle(a);

    // display results again using select
    StdOut.println();
    for (int i = 0; i < a.length; i++) {
        String ith = (String) Quick.select(a, i);
        StdOut.println(ith);
    }
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:22,代码来源:Quick.java

示例2: dag

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Returns a random simple DAG containing <tt>V</tt> vertices and <tt>E</tt> edges.
 * Note: it is not uniformly selected at random among all such DAGs.
 * @param V the number of vertices
 * @param E the number of vertices
 * @return a random simple DAG on <tt>V</tt> vertices, containing a total
 *     of <tt>E</tt> edges
 * @throws IllegalArgumentException if no such simple DAG exists
 */
public static Digraph dag(int V, int E) {
    if (E > (long) V*(V-1) / 2) throw new IllegalArgumentException("Too many edges");
    if (E < 0)                  throw new IllegalArgumentException("Too few edges");
    Digraph G = new Digraph(V);
    SET<Edge> set = new SET<Edge>();
    int[] vertices = new int[V];
    for (int i = 0; i < V; i++) vertices[i] = i;
    StdRandom.shuffle(vertices);
    while (G.E() < E) {
        int v = StdRandom.uniform(V);
        int w = StdRandom.uniform(V);
        Edge e = new Edge(v, w);
        if ((v < w) && !set.contains(e)) {
            set.add(e);
            G.addEdge(vertices[v], vertices[w]);
        }
    }
    return G;
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:29,代码来源:DigraphGenerator.java

示例3: wheel

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Returns a wheel graph on <tt>V</tt> vertices.
 * @param V the number of vertices in the wheel
 * @return a wheel graph on <tt>V</tt> vertices: a single vertex connected to
 *     every vertex in a cycle on <tt>V-1</tt> vertices
 */
public static Graph wheel(int V) {
    if (V <= 1) throw new IllegalArgumentException("Number of vertices must be at least 2");
    Graph G = new Graph(V);
    int[] vertices = new int[V];
    for (int i = 0; i < V; i++) vertices[i] = i;
    StdRandom.shuffle(vertices);

    // simple cycle on V-1 vertices
    for (int i = 1; i < V-1; i++) {
        G.addEdge(vertices[i], vertices[i+1]);
    }
    G.addEdge(vertices[V-1], vertices[1]);

    // connect vertices[0] to every vertex on cycle
    for (int i = 1; i < V; i++) {
        G.addEdge(vertices[0], vertices[i]);
    }

    return G;
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:27,代码来源:GraphGenerator.java

示例4: regular

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Returns a uniformly random <tt>k</tt>-regular graph on <tt>V</tt> vertices
 * (not necessarily simple). The graph is simple with probability only about e^(-k^2/4),
 * which is tiny when k = 14.
 * @param V the number of vertices in the graph
 * @return a uniformly random <tt>k</tt>-regular graph on <tt>V</tt> vertices.
 */
public static Graph regular(int V, int k) {
    if (V*k % 2 != 0) throw new IllegalArgumentException("Number of vertices * k must be even");
    Graph G = new Graph(V);

    // create k copies of each vertex
    int[] vertices = new int[V*k];
    for (int v = 0; v < V; v++) {
        for (int j = 0; j < k; j++) {
            vertices[v + V*j] = v;
        }
    }

    // pick a random perfect matching
    StdRandom.shuffle(vertices);
    for (int i = 0; i < V*k/2; i++) {
        G.addEdge(vertices[2*i], vertices[2*i + 1]);
    }
    return G;
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:27,代码来源:GraphGenerator.java

示例5: select

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Rearranges the array so that a[k] contains the kth smallest key;
 * a[0] through a[k-1] are less than (or equal to) a[k]; and
 * a[k+1] through a[N-1] are greater than (or equal to) a[k].
 * @param a the array
 * @param k find the kth smallest
 */
public static Comparable select(Comparable[] a, int k) {
    if (k < 0 || k >= a.length) {
        throw new IndexOutOfBoundsException("Selected element out of bounds");
    }
    StdRandom.shuffle(a);
    int lo = 0, hi = a.length - 1;
    while (hi > lo) {
        int i = partition(a, lo, hi);
        if      (i > k) hi = i - 1;
        else if (i < k) lo = i + 1;
        else return a[i];
    }
    return a[lo];
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:22,代码来源:Quick.java

示例6: path

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Returns a path digraph on <tt>V</tt> vertices.
 * @param V the number of vertices in the path
 * @return a digraph that is a directed path on <tt>V</tt> vertices
 */
public static Digraph path(int V) {
    Digraph G = new Digraph(V);
    int[] vertices = new int[V];
    for (int i = 0; i < V; i++) vertices[i] = i;
    StdRandom.shuffle(vertices);
    for (int i = 0; i < V-1; i++) {
        G.addEdge(vertices[i], vertices[i+1]);
    }
    return G;
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:16,代码来源:DigraphGenerator.java

示例7: binaryTree

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Returns a complete binary tree digraph on <tt>V</tt> vertices.
 * @param V the number of vertices in the binary tree
 * @return a digraph that is a complete binary tree on <tt>V</tt> vertices
 */
public static Digraph binaryTree(int V) {
    Digraph G = new Digraph(V);
    int[] vertices = new int[V];
    for (int i = 0; i < V; i++) vertices[i] = i;
    StdRandom.shuffle(vertices);
    for (int i = 1; i < V; i++) {
        G.addEdge(vertices[i], vertices[(i-1)/2]);
    }
    return G;
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:16,代码来源:DigraphGenerator.java

示例8: cycle

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Returns a cycle digraph on <tt>V</tt> vertices.
 * @param V the number of vertices in the cycle
 * @return a digraph that is a directed cycle on <tt>V</tt> vertices
 */
public static Digraph cycle(int V) {
    Digraph G = new Digraph(V);
    int[] vertices = new int[V];
    for (int i = 0; i < V; i++) vertices[i] = i;
    StdRandom.shuffle(vertices);
    for (int i = 0; i < V-1; i++) {
        G.addEdge(vertices[i], vertices[i+1]);
    }
    G.addEdge(vertices[V-1], vertices[0]);
    return G;
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:17,代码来源:DigraphGenerator.java

示例9: bipartite

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Returns a random simple bipartite graph on <tt>V1</tt> and <tt>V2</tt> vertices,
 * containing each possible edge with probability <tt>p</tt>.
 * @param V1 the number of vertices in one partition
 * @param V2 the number of vertices in the other partition
 * @param p the probability that the graph contains an edge with one endpoint in either side
 * @return a random simple bipartite graph on <tt>V1</tt> and <tt>V2</tt> vertices,
 *    containing each possible edge with probability <tt>p</tt>
 * @throws IllegalArgumentException if probability is not between 0 and 1
 */
public static Graph bipartite(int V1, int V2, double p) {
    if (p < 0.0 || p > 1.0)
        throw new IllegalArgumentException("Probability must be between 0 and 1");
    int[] vertices = new int[V1 + V2];
    for (int i = 0; i < V1 + V2; i++) vertices[i] = i;
    StdRandom.shuffle(vertices);
    Graph G = new Graph(V1 + V2);
    for (int i = 0; i < V1; i++)
        for (int j = 0; j < V2; j++)
            if (StdRandom.bernoulli(p))
                G.addEdge(vertices[i], vertices[V1+j]);
    return G;
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:24,代码来源:GraphGenerator.java

示例10: path

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Returns a path graph on <tt>V</tt> vertices.
 * @param V the number of vertices in the path
 * @return a path graph on <tt>V</tt> vertices
 */
public static Graph path(int V) {
    Graph G = new Graph(V);
    int[] vertices = new int[V];
    for (int i = 0; i < V; i++) vertices[i] = i;
    StdRandom.shuffle(vertices);
    for (int i = 0; i < V-1; i++) {
        G.addEdge(vertices[i], vertices[i+1]);
    }
    return G;
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:16,代码来源:GraphGenerator.java

示例11: binaryTree

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Returns a complete binary tree graph on <tt>V</tt> vertices.
 * @param V the number of vertices in the binary tree
 * @return a complete binary tree graph on <tt>V</tt> vertices
 */
public static Graph binaryTree(int V) {
    Graph G = new Graph(V);
    int[] vertices = new int[V];
    for (int i = 0; i < V; i++) vertices[i] = i;
    StdRandom.shuffle(vertices);
    for (int i = 1; i < V; i++) {
        G.addEdge(vertices[i], vertices[(i-1)/2]);
    }
    return G;
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:16,代码来源:GraphGenerator.java

示例12: cycle

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Returns a cycle graph on <tt>V</tt> vertices.
 * @param V the number of vertices in the cycle
 * @return a cycle graph on <tt>V</tt> vertices
 */
public static Graph cycle(int V) {
    Graph G = new Graph(V);
    int[] vertices = new int[V];
    for (int i = 0; i < V; i++) vertices[i] = i;
    StdRandom.shuffle(vertices);
    for (int i = 0; i < V-1; i++) {
        G.addEdge(vertices[i], vertices[i+1]);
    }
    G.addEdge(vertices[V-1], vertices[0]);
    return G;
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:17,代码来源:GraphGenerator.java

示例13: star

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Returns a star graph on <tt>V</tt> vertices.
 * @param V the number of vertices in the star
 * @return a star graph on <tt>V</tt> vertices: a single vertex connected to
 *     every other vertex
 */
public static Graph star(int V) {
    if (V <= 0) throw new IllegalArgumentException("Number of vertices must be at least 1");
    Graph G = new Graph(V);
    int[] vertices = new int[V];
    for (int i = 0; i < V; i++) vertices[i] = i;
    StdRandom.shuffle(vertices);

    // connect vertices[0] to every other vertex
    for (int i = 1; i < V; i++) {
        G.addEdge(vertices[0], vertices[i]);
    }

    return G;
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:21,代码来源:GraphGenerator.java

示例14: sort

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
/**
 * Rearranges the array in ascending order, using the natural order.
 * @param a the array to be sorted
 */
public static void sort(Comparable[] a) {
    StdRandom.shuffle(a);
    sort(a, 0, a.length - 1);
}
 
开发者ID:fracpete,项目名称:princeton-java-algorithms,代码行数:9,代码来源:Quick.java

示例15: sortImpl

import edu.princeton.cs.introcs.StdRandom; //导入方法依赖的package包/类
@Override
public void sortImpl(Comparable[] arr) {
    StdRandom.shuffle(arr);
    sort(arr, 0, arr.length - 1);
}
 
开发者ID:brettryan,项目名称:algs4-work,代码行数:6,代码来源:AppSorting.java


注:本文中的edu.princeton.cs.introcs.StdRandom.shuffle方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。