如何根据给定参数找到时间复杂度

时间:2019-04-18 21:28:04

标签: java time-complexity big-o

下面的代码用于单词计数器的目的,该单词计数器给定一个文本文件,打印(总数和离散)单词的数量,并根据降序列出k个最常用的单词在使用水平上。

鉴于此,我的目标是根据以下参数计算此解决方案的最坏情况下的时间复杂度 c 总字符数< / em>), m 单词总数), n 离散单词总数), k 印刷字数)。

到目前为止,我只能弄清楚,由于我使用插入排序对包含每个离散单词使用次数的arrayList进行排序,因此需要 O(n ^ 2)这部分,用 O(k)打印结果,但我不能真正确定我得出的这些结果是否正确,或者如何处理参数 c m 。有提示吗?

public static void main(int k)throws IOException{

   //Create input stream & scanner
   FileInputStream fin = new FileInputStream("readwords.txt");
   Scanner fileInput = new Scanner(fin);

   //Create the ArrayLists
   ArrayList<String> words = new ArrayList<String>();
   ArrayList<Integer> totalCount = new ArrayList<Integer>();
   ArrayList<Integer> discreteCount = new ArrayList<Integer>();

   //Read through file and find the words
   while(fileInput.hasNext()){
       //Get the next word
       String nextWord = fileInput.next();
       //Determine if the word is in the ArrayList
       if(words.contains(nextWord)){
           int index = words.indexOf(nextWord);
           discreteCount.set(index, discreteCount.get(index) + 1);
           totalCount.add(1);
        }
        else {
            words.add(nextWord);
            discreteCount.add(1);
            totalCount.add(1);

        }
    }

    //Close
    fileInput.close();
    fin.close();

    //Sort ArrayLists
    InsertionSort.sort(discreteCount, words);

    //Print out the results
    System.out.println("This file contains " +totalCount.size()+" words, There are " +discreteCount.size()+ " discrete words.");
        int i = 1;
        while(i <=discreteCount.size() && i <= k){
            System.out.println(discreteCount.get(discreteCount.size()-i) +" "+words.get(discreteCount.size()-i));
            i ++;
        }   

}

0 个答案:

没有答案