找到子数组中最小元素的索引

时间:2018-02-24 03:12:47

标签: c++

函数FindSubArrayMinimum假设返回值之间数组中最小值的索引(左,右)。它不是应该返回1,而是返回7.

这是我的代码:

#include <iostream>
using namespace std;

class Minimum {

int *array;
int arrayLen;

public:

Minimum(int *array, int arrayLen) {
}

void swap(int &a, int &b) {
    int x;
    x = a;
    a = b;
    b = x;
}

int findMinimum(int a, int b) {
    if(a < b){
        return a;
    }else{
        return b;
    }
}

int findArrayMinimum(int a[], int arraySize) {
    int min=a[0];
    for(int i=0;i<arraySize-1;i++){
        if(a[i] < min){
            min = i;
        }
    }
    return min;
}

int findSubArrayMinimum(int a[], int arraySize, int left, int right) {
    int min = a[left];
    for(int i=left;i<right+1;i++){
        if(a[i] < min){
            cout << min << endl;
            min = i;
        }
    }
    return min;
}
int findSubArrayMinimumAndSwap(int left, int right, int swapIndex) {

}

};

int main() {

int A[5] = {47, 7, 21, -1, 11};
Minimum min(A, 5);

cout << "Minimum of 7 and 11 is " << min.findMinimum(7, 11) << endl;

int x = 5, y = 7;
min.swap(x, y);
cout << "x = " << x << ", y = " << y << endl;

cout << "Minimum value is at position " << min.findArrayMinimum(A,5) << endl;
cout << "Minimum value between [1,2] is at position " << 
min.findSubArrayMinimum(A,5,1,2)<< endl;


return 0;
}

我不能为我的生活弄清楚为什么它不会返回最低指数。从逻辑上讲,它应该有效,除非我遗漏了什么?

4 个答案:

答案 0 :(得分:1)

  

我所有的其他功能,就像我已经完成的所有功能一样。

事实并非如此。在findArrayMinimum中,循环应为

for(int i=0;i<arraySize;i++)

或者

for(int i=0;i<=arraySize-1;i++)

循环中的条件的初始值也是错误的。应为int min=0if(a[i] < a[min])

  

不是按原样返回1,而是返回7.

findSubArrayMinimum很不错,除了初始最小值int min = a[left],应为int min = left且循环中的条件应为if(a[i] < a[min])

<arraySize findSubArrayMinimum中的findSubArray是奇数,未使用。

您不需要return findSubArrayMinimum(a, 0, arraySize - 1)的单独实施。您可以在findSubArray中执行{ "complexType":"SoftLayer_Container_Product_Order_Network_Storage_AsAService", "volumeSize": 20, # 20, 40, 80, 100, 250, 500, 1000, ..., 12000 "quantity": 1, "iops": 1000, "osFormatType":{ "keyName":"LINUX", "id":12, "complexType": "SoftLayer_Network_Storage_Iscsi_OS_Type" }, 'location': 449604, # Tokyo 'packageId': 759, # Storage As A Service (StaaS) 'prices': [ {'id':189433}, # Storage As A Service {'id':189453}, # File storage {'id':189483}, # 20 - 39 GBs performance_storage_space {'id':189543} # 100 - 1000 IOPS performance_storage_iops ], }

如果要使用包含左,右,中间子阵列的0,1,多个元素的数组实现测试,您将发现代码中的所有错误。

答案 1 :(得分:1)

C ++就是不重新发明轮子。

#include <algorithm>

size_t findArrayMinimum(int a[], size_t arraySize) {
    // Returns index of minimum element
    return std::min_element(a, a + arraySize) - a;
}

答案 2 :(得分:0)

在你的findSubArrayMinumum中你需要min = a [i];而不是min = i;我是一个指数,[i]是价值。

我建议你:

int findSubArrayMinimum(int a[], int arraySize, int left, int right) {
    int index = left;
    int min = a[left];

    for(int i=left;i<right+1;i++){
        if(a[i] < min){
            min = a[min];
            index = i;
        }
    }

    return index;
}


int index = min.findSubArrayMinimum(A,5,1,3);
cout << "Minimum value between [1,2] is at position " << index << 
". and value is: " << A[index] << endl;

答案 3 :(得分:0)

int findSubArrayMinimum(int a[], int arraySize, int left, int right) {

//The min is the index of minimum value since you are returning the index only
int min = left;
  for(int i=left;i<=right;i++){
  //since we have min = indexOfMin, then we need to compare the values
  if(a[i] <= a[min]){
      min = i;
  }
}
//remember! You are returning the index of the minmum value!
return min;
}