2014-12-02 89 views
0

这里是我的程序中最重要的部分(数据是以逗号分隔的2列的文本文件(例如3,12),第一列值表示重量,第二列表示利润)该方法填充结构的阵列(结构有2场的重量和利润)从文本文件中的数据大小为n,然后排序项accorrding我的对比方法如下..结构和文本文件数组的时间复杂度是多少?

static void Read(ref Items[] Item) 
    { 
     string[] lines = File.ReadAllLines(@"E:\test1.txt"); 
     int i = 0; 
     string[] word; 
     for (int j=0;j<n;j++)// n is the no of items that the user want to fill the array with 
     { 
      word = (lines[j].Split(',')); 
      word = (lines[j].Split(',')); 
      Item[i].Weight = float.Parse(word[0]);// Item is the name of array 
      Item[i].Profit = float.Parse(word[1]); 

      i++; 
     } 
    Array.sort(Item,mycomparision); 
    } 

所以我想计算这种方法的时间和空间复杂度(空间复杂度=输入大小+输出大小+任何数据结构)..我真的很困惑什么是结构和文本文件的数组的确切时间和空间的复杂性。我想文件大小作为输入时,我计算空间复杂性还是仅仅是阵列?我应该计算打开文件并将数据读取到数组的时间?并且是我的代码如此高效,或者有更好和更高效的代码(例如,如果文件包含100,000个项目,但用户只想读取100个项目,那么可以先读取所有项目“string [] lines = File.ReadAllLines (@“E:\ test1.txt”);“然后只放100个数组)?请大家不要犹豫,帮助我^ _ ^。

+2

数组没有时间复杂度,阵列上的操作做。请发布您使用的代码。 – Lee 2014-12-02 13:56:17

+0

@Lee我已经编辑了我的帖子,提供了更多的细节和代码..你能阅读它并帮助我吗? – Yaman 2014-12-03 09:08:54

回答

-2
The complexity of your program is O(n+2) 

Since you have array of length n. you should need a max of n time units to find the element. 

Then to get the two values of the struct you need another time units. 

您不需要2n,因为数组的每个位置都有一个结构。

从我的分析一般阵列的时间复杂度是O(n)。您可以参考以下链接。

time-complexity-algorithms1

&

time-complexity-algorithms2

+0

*数组没有时间复杂度,数组上的操作可以。请发布您使用的代码。 - Lee * – 2014-12-02 13:57:30

+0

@shekharsuman :: http://bigocheatsheet.com/你可以参考这个链接,你可以看到数组的时间复杂度为O(n)。 – 2014-12-02 14:01:28

+0

嗨MAN,OP还没有提供要在阵列上执行的操作!你不能代表OP采取任何行动并发布答案。这是无意义的!如果有可能的话,我应该有三倍低估你的价值! – 2014-12-02 14:25:58

相关问题