Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 最小n-m,以便对整个数组进行排序_Java_Algorithm - Fatal编程技术网

Java 最小n-m,以便对整个数组进行排序

Java 最小n-m,以便对整个数组进行排序,java,algorithm,Java,Algorithm,在一次采访中,我被问到以下问题: Given an array of integers, write a method to find indices m and n such that if you sorted elements m through n, the entire array would be sorted. Minimize n-m. i.e. find smallest sequence. 请在下面找到我的答案,并对解决方案进行评论。谢谢 我终于找到了问题的解决方案,请随

在一次采访中,我被问到以下问题:

Given an array of integers, write a method to find indices m and n such that 
if you sorted elements m through n, the entire array would be sorted. Minimize
n-m. i.e. find smallest sequence.

请在下面找到我的答案,并对解决方案进行评论。谢谢

我终于找到了问题的解决方案,请随意评论

让我们举一个例子:

int a[] = {1,3,4,6,10,6,16,12,13,15,16,19,20,22,25}
现在,如果我将其放入图形(X坐标->数组索引和Y坐标->数组值),那么图形将如下所示:

现在,如果我们看到这张图,有两个地方发生倾斜,一个是在10点之后,另一个是在16点之后。现在在Z字形部分,如果我们看到最小值是6,最大值是16。因此,我们应该对整个数组进行排序的部分介于(6,16)之间。请参阅下图:

现在我们可以很容易地将数组分成三部分。中间部分是我们要排序的部分,这样整个数组都会被排序。请提供宝贵的意见。我尽力向我的标签解释得最好,如果我想解释更多,请让我知道。等待有价值的投入

下面的代码实现了上述逻辑:

public void getMN(int[] a)
{
    int min = Integer.MAX_VALUE; int max = Integer.MIN_VALUE;
    for(int i=1; i<a.length; i++)
    {
        if(a[i]<a[i-1])
        {
            if(a[i-1] > max)
            {
                max = a[i-1];
            }
            if(a[i] < min)
            {
                min = a[i];
            }
        }
    }
    if(max == Integer.MIN_VALUE){System.out.println("Array already sorted!!!");}
    int m =-1, n =-1;
    for(int i=0; i<a.length; i++)
    {
        if(a[i]<=min)
        {
            m++;
        }
        else
        {
            m++;
            break;
        }
    }
    for(int i=a.length-1; i>=0; i--)
    {
        if(a[i]>=max)
        {
            n++;
        }
        else
        {
            n++;
            break;
        }
    }
    System.out.println(m +" : "+(a.length-1-n));
    System.out.println(min +" : "+max);
}
public void getMN(int[]a)
{
int min=Integer.MAX\u值;int MAX=Integer.min\u值;

对于(inti=1;i实际上,我想到了这样的东西:

public static void sortMthroughN(int[] a)
{   
    int m = -1;
    int n = -1;
    int k = -1;
    int l = -1;
    int biggest;
    int smallest;
    // Loop through to find the start of the unsorted array.
    for(int i = 0; i < a.length-1; i++)
        if(a[i] > a[i+1]) {
            m = i;
            break;
        }
    // Loop back through to find the end of the unsorted array.
    for(int i = a.length-2; i > 0; i--)
        if(a[i] > a[i+1]) {
            n = i;
            break;
        }
    biggest = smallest = a[m];
    // Find the biggest and the smallest integers in the unsorted array.
    for(int i = m+1; i < n+1; i++) {
        if(a[i] < smallest)
            smallest = a[i];
        if(a[i] > biggest)
            biggest = a[i];
    }

    // Now, let's find the right places of the biggest and smallest integers. 
    for(int i = n; i < a.length-1; i++)
        if(a[i+1] >= biggest) {
            k = i+1;      //1
            break;
        }

    for(int i = m; i > 0; i--)
        if(a[i-1] <= smallest) {                               
            l = i-1;    //2
            break;
        }
            // After finding the right places of the biggest and the smallest integers
            // in the unsorted array, these indices is going to be the m and n.
    System.out.println("Start indice: " + l);
    System.out.println("End indice: " + k);

}
这些是什么?哪些是指数

谢谢

编辑:通过添加标记为1的位置:

            if(a[i+1] == biggest) {
                k = i;
                break;
            }
和2:

        if(a[i+1] == smallest) {
            l = i;
            break;
         }

更好。

从数组末尾开始查找最大值更容易:

public void FindMinSequenceToSort(int[] arr)
{
    if(arr == null || arr.length == 0) return;

    int m = 0, min = findMinVal(arr);
    int n = arr.length - 1, max = findMaxVal(arr);

    while(arr[m] < min)
    {
        m ++;
    }

    while(arr[n] > max)
    {
        n --;
    }

    System.out.println(m);
    System.out.println(n);
}

private int findMinVal(int[] arr)
{
    int min = Integer.MAX_VALUE;
    for(int i = 1; i < arr.length; i++)
    {
        if(arr[i] < arr[i-1] && arr[i] < min)
        {
            min = arr[i];
        }
    }

    return min;
}

private int findMaxVal(int[] arr)
{
    int max = Integer.MIN_VALUE;
    for(int i = arr.length - 2; i >= 0; i--)
    {
        if(arr[i] >= arr[i+1] && arr[i] > max)
        {
            max = arr[i];
        }
    }

    return max;
}
public void FindMinSequenceToSort(int[]arr)
{
if(arr==null | | arr.length==0)返回;
int m=0,min=findMinVal(arr);
int n=arr.length-1,max=findMaxVal(arr);
同时(arr[m]最大值)
{
n--;
}
系统输出打印项次(m);
系统输出println(n);
}
专用int findMinVal(int[]arr)
{
int min=整数最大值;
对于(int i=1;i=0;i--)
{
如果(arr[i]>=arr[i+1]&arr[i]>最大值)
{
max=arr[i];
}
}
返回最大值;
}

实际上,您可以有两个指针,最后一个指针向后移动以检查最短未排序序列的开始索引。它有点像O(N2),但更干净

public static int[] findMinUnsortedSequence(int[] array) {
        int firstStartIndex = 0;
        int startIndex = 0;
        int endIndex = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < i; j++) {
                if (array[j] <= array[i]) {
                    startIndex = j + 1;
                } else {
                    endIndex = i;
                    if (firstStartIndex == 0) {
                        firstStartIndex = startIndex;
                    }
                }
            }
        }
        return new int[]{firstStartIndex, endIndex};
    }
public静态int[]findMinUnsortedSequence(int[]数组){
int firstStartIndex=0;
int startIndex=0;
int-endIndex=0;
for(int i=0;iif(数组[j]请随意回答您自己的问题!但请将它们分开。我建议您在此问题的答案中添加一个答案,然后将该问题编辑为您的问题。我无法理解您的观点。抱歉。问题应仅为问题。请编辑您的问题,使其仅包含您的问题。然后,您可以使用
您的答案
框正常回答您自己的问题。@Telthien我已经完成了!!!很好!有趣的解决方案,顺便说一下!4是排序开始的起始索引,它是10的位置,最后一个索引是9,即当前的15位。6是未排序数组的最小数量,16是最大数量对于未排序的数组。希望它能澄清问题!!足够公平。:)3是开始标志,10是结束标志。我想,我对等式有一个问题。顺便说一下,这个解决方案不错。。)我从0开始数组索引。从我的算法来看,最小值是6,最大值是16,因为我必须最小化n-m,所以我忽略了分别位于10和3处的6和16。因此,排序子部分从4开始到9,两者都包括在内。很清楚,还是我遗漏了什么?你与我的解决方案同步还是不同意?正如我们所说的那样我正在同时学习。你的答案是正确的。在编辑我的代码之前,我发现答案是3,10;因为我没有注意相等部分。如果相等,则增加索引(或减少索引)是正确的方法。我通过添加这两部分代码修复了问题。添加注释,以便新手也能轻松理解。
public static int[] findMinUnsortedSequence(int[] array) {
        int firstStartIndex = 0;
        int startIndex = 0;
        int endIndex = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < i; j++) {
                if (array[j] <= array[i]) {
                    startIndex = j + 1;
                } else {
                    endIndex = i;
                    if (firstStartIndex == 0) {
                        firstStartIndex = startIndex;
                    }
                }
            }
        }
        return new int[]{firstStartIndex, endIndex};
    }