在Java中,如何从循环列表中获取最小的数字?

在Java中,如何从循环列表中获取最小的数字?,java,Java,让我们假设我有一个类似这样的循环列表 circularList = [12, 62, 76, 92, 3, 7, 12, 19]. 从上述列表中获取最小数字的最有效方法是什么。浏览列表,记住并比较第一项,记住迄今为止最小的数字,回到列表的开头时返回 你可以看到它是如何完成的 int findMinimum(Node* start) { int min = start -> data; Node* t = start -> link; while(t

让我们假设我有一个类似这样的循环列表

circularList = [12, 62, 76, 92, 3, 7, 12, 19].

从上述列表中获取最小数字的最有效方法是什么。

浏览列表,记住并比较第一项,记住迄今为止最小的数字,回到列表的开头时返回

你可以看到它是如何完成的

int findMinimum(Node* start) 
{ 
    int min = start -> data; 
    Node* t = start -> link; 
    while(t != start) 
    { 
        if(t -> data < min) 
            min = t -> data; 
        t = t -> link; 
    } 
    return min; 
}  
int findminium(节点*开始)
{ 
int min=开始->数据;
节点*t=开始->链接;
while(t!=开始)
{ 
如果(t->datadata;
t=t->link;
} 
返回最小值;
}  

除了@No Idea For Name:
Collections.min(yourCollection)
一般列表的单线程解决方案是:

int minValue= Integer.MAX_VALUE ;
for(int e: circularList )
    if( e < minValue ) minValue= e ;
int threadCount= 8 ;
int nextStart= 0 ;
MinimumFindingThread[] threads= MinimumFindingThread[threadCount] ;
while( threadCount > 0 ) {
    count= ( circularList.size() - nextStart ) / threadCount ;
    threads[threadCount-1]= new MinimumFindingThread(circularList,nextStart,nextStart+count) ;
    threads[threadCount-1].start();
    nextStart= nextStart+count ;
    threadCount--;
}
int threadCount= 8 ;
int minValue= Integer.MAX_VALUE ;
while( threadCount > 0 ) {
    threads[threadCount-1].join();
    theadMin= threads[threadCount-1].getResult() ;
    if( theadMin < minValue ) minValue= theadMin ;
    threadCount--;
}
class MinimumFindingThread extends Thread {
    int[] list;
    int from;
    int to;
    int minValue;
    public MinimumFindingThread(int[] list,int from,int to) {
        this.list= list ;
        this.from= from ;
        this.to= this.to ;
    }
    public void run() {
        int minValue= Integer.MAX_VALUE ;
        for(int e: this.list[this.from:this.to] )
            if( e < minValue ) minValue= e ;
        this.minValue= minValue ; ;
    }
    public int getResult() {
        return this.minValue ;
    }
}
对于最后一个元素位置已知的基于数组的排序循环列表:

minValue= circularList[firstElement] ;
if( lastElement+1 >= circularList.length )
    minValue= circularList[0] ;
else
    minValue= circularList[lastElement+1] ;
对于第一个和最后一个元素位置未知的基于数组的排序循环列表:

int minValue= Integer.MAX_VALUE ;
int prevValue= Integer.MIN_VALUE ;
for(int e: circularList ) {
    if( e < minValue )
        minValue= e ;
    if( e < prevValue ) {
        break;
    }
    prevValue= e ;
}
int minValue=Integer.MAX\u值;
int prevValue=Integer.MIN_值;
for(int e:循环列表){
if(e
通用列表的多处理器友好解决方案是:

int minValue= Integer.MAX_VALUE ;
for(int e: circularList )
    if( e < minValue ) minValue= e ;
int threadCount= 8 ;
int nextStart= 0 ;
MinimumFindingThread[] threads= MinimumFindingThread[threadCount] ;
while( threadCount > 0 ) {
    count= ( circularList.size() - nextStart ) / threadCount ;
    threads[threadCount-1]= new MinimumFindingThread(circularList,nextStart,nextStart+count) ;
    threads[threadCount-1].start();
    nextStart= nextStart+count ;
    threadCount--;
}
int threadCount= 8 ;
int minValue= Integer.MAX_VALUE ;
while( threadCount > 0 ) {
    threads[threadCount-1].join();
    theadMin= threads[threadCount-1].getResult() ;
    if( theadMin < minValue ) minValue= theadMin ;
    threadCount--;
}
class MinimumFindingThread extends Thread {
    int[] list;
    int from;
    int to;
    int minValue;
    public MinimumFindingThread(int[] list,int from,int to) {
        this.list= list ;
        this.from= from ;
        this.to= this.to ;
    }
    public void run() {
        int minValue= Integer.MAX_VALUE ;
        for(int e: this.list[this.from:this.to] )
            if( e < minValue ) minValue= e ;
        this.minValue= minValue ; ;
    }
    public int getResult() {
        return this.minValue ;
    }
}
int threadCount=8;
int nextStart=0;
MinimumFindingThread[]线程=MinimumFindingThread[threadCount];
而(线程数>0){
count=(circularList.size()-nextStart)/threadCount;
threads[threadCount-1]=新的MinimumFindingThread(循环列表、nextStart、nextStart+count);
线程[threadCount-1]。开始();
nextStart=nextStart+计数;
线程数--;
}
int threadCount=8;
int minValue=Integer.MAX_值;
而(线程数>0){
线程[threadCount-1]。连接();
theadMin=threads[threadCount-1].getResult();
如果(theadMin
根据存储这些元素的数据结构类型,您可以:

1) 遍历列表并将每个值与当前最小值进行比较:

int compareValue = Integer.MAX_VALUE;
for(int value: circularList){
    if(value < compareValue);
        compareValue = value;
}

基本上只有一种算法可以从任意列表中获取最小元素:

  smallest = unset
  for each element in list:
      if smallest is unset OR element less than smallest:
          smallest = element
如何映射到代码取决于特定的列表数据结构,甚至特定的编程语言也可能有所不同。但是基本结构是一样的,没有特别有效的方法来实现。(复杂性为
O(N)
,其中
N
是列表长度。)

唯一可以改进这一点的方法是约束列表。例如,如果列表已经以最小的优先顺序排序,那么获取最小的元素只是获取第一个元素的问题


(但对列表进行排序的成本将至少是
O(N)
…而且可能是
O(NlogN)
。排序以获得最小值的效率将低于上述迭代和测试。)

在回答之前,我必须说:

首先,你说的和你的例子不符<代码>[12、62、76、92、3、7]
是循环排序数组。然而,你的例子是:

[12, 62, 76, 92, 3, 7, 12, 19]
不是一个循环,它只是两个排序数组的组合

第二,如果你谈论抽象的方法。您需要修改一些搜索算法。该方法的效率取决于您的案例。因此,该算法将有其最坏情况、最佳情况和平均情况性能。所以,您所谓的“最有效的方法”是您需要在自己的测试用例中尝试的东西

然后我的答案是,如果你需要从两个排序数组的组合中得到最小的数字,我有一种比粗体比较更有效的方法:

int a;
int b = 0;\\your minimum possible value
//l= your array
for(int i=1; i<l.size;i++){
  b=l[i-1];
  a=l[i];
  if(b>a) // find the break point of the two sorted arrays
    if(a<l[0])
      return a;
    else 
      return l[0];
}
return l[0]; // in case the array already sorted(break point in 0 and l.size)
inta;
int b=0\\你的最小可能值
//l=你的数组
for(int i=1;ia)//找到两个排序数组的断点

如果(ait看起来像一个平面数组?那是一个列表还是一个数组?你能确定你想要处理的对象的类型吗?我想知道有效的算法来完成这项工作我不是问答案是编码形式,所以它不是一个重复的问题。你能再解释一下吗?特别是,你想知道如何实现一个新的迭代吗(与“库”相反)循环列表类型?如果是,您是否对链接的或基于数组的类型感兴趣?完全正确,但我认为他所说的是抽象的方法…对基本类型不起作用,这似乎是事实。但我不同意(“for Name”是您的姓,对吗?:-)):我们不知道OP在考虑什么样的抽象。或者,也许恰恰相反:他想知道如何实现循环列表(基于列表或数组)的迭代.
Node*
->
?这看起来不像Java。您知道的任何正确的算法都可以这样做这里的意思是:最小值=unset我没有理解您,我正在寻找解决方案Java@JavaProgrammer-如果你是一名Java程序员……你不需要其他人为你写这篇文章。此外,你还没有向我们展示你的“循环列表”数据结构。这是伪代码。“minimate=unset”表示名为“minimate”的变量开始时的状态不包含有效值。根据类型和编程语言,这将以各种方式实现。如果您需要有人用Java为您编码,我建议您访问“rent-a-coder”“网站并为此付费