使用函数实现选择排序。

时间:2013-09-21 20:25:59

标签: c++

我正在实施选择排序算法步骤。

我做了两个函数:一个用于查找最小值的索引,另一个用于selection_sort。

输出不正确。我弄清楚了这个错误,但我不知道如何解决这个问题。 错误发生在find_min函数中。我想我要更新索引?

请帮助。

int find_min(int ar[], int size)
{
    int index = 0;
    bool found = false;
    for(int i = 1; i < size; i++)
    {
        if(ar[index] > ar[i])
        {
            index = i;
            found = true;
        }
    }

    if (found)
    {
        return index;
    }
    else
        return -1;

}

void selection_sort(int arr[], int size)
{
    int loc;
    for(int count = 0; count < size; count++)
    {
        loc = find_min(arr, size - count);
        if (loc >= 0)
        {
            exchange(arr[count], arr[loc]);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

请考虑以下代码:

#include <iostream>

using namespace std;

// Exchanges two values in array arr, specified by indices i and j
void exchange(int arr[], int i, int j)
{
    int t = arr[i];
    arr[i] = arr[j];
    arr[j] = t;
}

// Prints an array arr of length len
void print_array(int arr[], int len)
{
    for(int i=0; i<len; i++)
    {
        cout << arr[i] << "  ";
    }
    cout << endl;
}

// Finds the index of the minimum value in array arr of length len between indices
//    init and the last element (len-1)
int find_min(int arr[], int len, int init)
{
    int index = init;

    for(int i = init; i < len; i++)
    {
        if(arr[index] > arr[i])
        {
            index = i;
        }
    }

    return index;
}

// Sort the array using the selection sort algorithm
void selection_sort(int arr[], int len)
{
    int loc;
    // We don't need the last iteration when there is only a single element left
    for(int pos = 0; pos < (len-1); pos++)
    {
        cout << "Before iteration " << pos << " array is: ";
        print_array(arr, len);

        loc = find_min(arr, len, pos);
        cout << "  Minimum between indices " << pos << " and " << (len-1) << " is: ";
        cout << arr[loc] << " at index " << loc << endl;

        if (loc != pos)
        {
            cout << "  Exchanging indices " << loc << " and " << pos << endl;
            exchange(arr, pos, loc);
        }
        else
        {
            cout << "  No exchange necessary this iteration. " << endl;
        }

        cout << "After iteration " << pos << " array is: ";
        print_array(arr, len);

        // Print an extra line newline, space things out a bit
        cout << endl;
    }
}


int main(void)
{
    int arr[] = {2,6,7,3,1,8};

    selection_sort(arr, 6);
}

输出:

Before iteration 0 array is: 2  6  7  3  1  8
  Minimum between indices 0 and 5 is: 1 at index 4
  Exchanging indices 4 and 0
After iteration 0 array is: 1  6  7  3  2  8

Before iteration 1 array is: 1  6  7  3  2  8
  Minimum between indices 1 and 5 is: 2 at index 4
  Exchanging indices 4 and 1
After iteration 1 array is: 1  2  7  3  6  8

Before iteration 2 array is: 1  2  7  3  6  8
  Minimum between indices 2 and 5 is: 3 at index 3
  Exchanging indices 3 and 2
After iteration 2 array is: 1  2  3  7  6  8

Before iteration 3 array is: 1  2  3  7  6  8
  Minimum between indices 3 and 5 is: 6 at index 4
  Exchanging indices 4 and 3
After iteration 3 array is: 1  2  3  6  7  8

Before iteration 4 array is: 1  2  3  6  7  8
  Minimum between indices 4 and 5 is: 7 at index 4
  No exchange necessary this iteration.
After iteration 4 array is: 1  2  3  6  7  8

答案 1 :(得分:0)

修复您的find_min功能

int find_min(int ar[],int s, int size) // s denotes the start index
{
    int index = s-1; //take min pos as start
    bool found = false;
    for(int i = s; i < size; i++)
    {
        if(ar[index] > ar[i])
        {
            index = i;
            found = true;
        }
    }

    if (found)
    {
        return index;
    }
    else
        return -1;
}

现在使用: loc = find_min(arr, count+1,size);

直到count arr已经排序

请参阅HERE