实现随机选择算法

时间:2015-12-05 23:04:26

标签: java algorithm

我正在尝试实现返回数组中第K个最大元素的随机选择算法。当pivot始终设置为等于数组中的第一个元素时,下面代码中的算法有效。如何让代码工作,以便使用随机生成的轴心点找到最大的K?

import java.util.Random;

public class RandomizedKSelection {
private static Random generator = new Random();

public static int partition(int[] A, int start, int end) {
    // start = generator.nextInt(end); This Line breaks the code
    int pivot = A[start];
    int pivotPosition = start++;
    while (start <= end) {
        // scan for values less than the pivot
        while ((start <= end) && (A[start] < pivot)) {
            start++;
        }

        // scan for values greater than the pivot
        while ((end >= start) && (A[end] >= pivot)) {
            end--;
        }    
        if (start > end) {
            // swap the end uncoformed 
            // element with the pivot
            swap(A, pivotPosition, end); 
        }
        else {
            // swap unconformed elements:
            // start that was not lesser than the pivot 
            // and end that was not larger than the pivot
            swap(A, start, end);
        }
    }
    return end;
}

@SuppressWarnings("unused")

// iterative version
private static int orderStatistic(int[] A, int k, int start, int end) {
    int pivotPosition = partition(A, start, end);

    while (pivotPosition != k - 1) {
        if (k - 1 < pivotPosition) {
            end = pivotPosition - 1;
        }
        else {
            start = pivotPosition + 1;
        }

        pivotPosition = partition(A, start, end);
    }

    return A[k - 1];
}

public static int kthLargest(int[] A, int k) {
    return orderStatistic(A, A.length - k + 1, 0, A.length - 1);
}

public static void swap(int[] A, int i, int j){
    int temp = A[i];
    A[i]= A[j];
    A[j] = temp;
}

}

1 个答案:

答案 0 :(得分:0)

我从wikibooks获得了一个`分区实现。我将您的代码更改为使用基于0的索引(您可以更轻松地找到基于0的索引的示例),如果您愿意,可以将它们包装起来(请参阅kthLargest1Based)。小随机测试论证了算法的有效性。

public class DirectoryServer {


public static void main(String[] args)
{
    //Initiates Directory called UniversityName.txt***********
    String DirectoryName = "UniversityDirectory.txt";
    //String outFile = "tmp.txt";
    Directory dir = new Directory(DirectoryName);
    //Directory out = new Directory(outFile);
    //*********************************************************


    Scanner stdin = new Scanner(System.in);

    //Introductory Message
    System.out.println("Welcome to the program user\n");
    System.out.println("The actions that can be taken are:\n");
    System.out.println("find    (Last Name)\nadd    (UCID)\ndelete  (UCID)\n");
    System.out.println("What do you want to do?");



  public class Directory {

final int max = 1024;
Person directory[] = new Person[max];//Create a directory of type Person
String position,UCID,first,last,major,email,dept,office;

Scanner inFile=null;
File outFile=null;
Scanner inFileData=null;
Scanner data=null;



Directory(String DirectoryFileName)//Constructor***************
{
    try{
    inFile = new Scanner(new FileInputStream(DirectoryFileName));
    }
    catch(FileNotFoundException e)
    {
        System.out.println("File not found exiting program\n");
        System.exit(0);
    }

    int directoryposition=0;
    while(inFile.hasNext());
    {
        data = new Scanner(inFile.nextLine());



        position = data.next();
        position=position.toLowerCase();


        switch(position)//do something