搜索和排序未排序的数组

时间:2015-10-05 17:05:17

标签: c++ arrays sorting search linear

我正在研究一个程序,它使用气泡或选择排序对数组进行排序然后搜索,然后使用二进制或线性搜索。它应该询问用户用户想要使用哪种排序和搜索功能。它应始终显示搜索或排序数组所花费的步数。它是我必须使用的固定(常量)数组。我有一个为此而制定的程序的基础,但它给了我一些错误。如果你们有机会,请让我知道我做错了什么。我认为这是一个简单的错误,但我不确定。我是编程新手,所以请给我你的想法!

#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>

using namespace std;
int answer;
int answer2;
int size;
void bubbleSort( int[], int );
void selectionSort( int[], int );
int value;

int unsorted[] =
{
    85, 64, 16, 18, 1, 88, 48, 63, 54, 83, 79, 50, 0, 55,
    17, 99, 69, 53, 65, 22, 93, 86, 9, 37, 56, 23, 21, 52,
    78, 29, 14, 58, 35, 47, 68, 87, 3, 34, 5, 74, 4, 45,
    41, 49, 67, 89, 92, 60, 72, 20, 8, 15, 42, 71, 31, 36,
    90, 84,70, 51, 28, 32, 81, 10, 82, 40, 57, 24, 25, 91,
    44, 66, 30, 62, 94, 6, 7, 46, 43,38, 75, 11, 39, 80,
    98, 27, 12, 76, 96, 2, 77, 19, 26, 59, 33, 73, 13,
    61, 95, 97
};

int main()
{
    cout <<
        "Which algorithm should be used to sort?" <<
        "(0: Bubble Sort, 1: Selection Sort)" << endl;

    cin >> answer;
    if ( answer == 0 )
    {
        bubbleSort;
    }
    else
    {
        selectionSort;
    }

    // Bubble Sort Fun
    void bubbleSort( int unsorted[], int size )
    {
        bool swap;
        int temp;

        do
        {
            swap = false;
            for ( int count = 0; count < ( size - 1 ); count++ )
            {
                if ( unsorted[ count ] > unsorted[ count - 1 ]; count++ )
                {
                    temp = unsorted[ count ];
                    unsorted[ count ] = unsorted[ count + 1 ];
                    unsorted[ count + 1 ] = temp;
                    swap = true;
                }
            }
        }
        while ( swap );
    }
    // Selection Sort Function
    void selectionSort( int unsorted[], int size )
    {
        int startScan, minIndex, minValue;
        for ( startScan = 0; startScan <( size - 1 ); startScan++ )
        {
            minIndex = startScan;
            minValue = unsorted[ startScan ];
            for ( int index = startScan + 1; index < size; index++ )
            {
                minValue = unsorted[ index ];
                minIndex = index;
            }
            unsorted[ minIndex ] = unsorted[ startScan ];
            unsorted[ startScan ] = minValue;
        }
    }

    // Sorting completed
    cout <<
        "Which algorithm should be used to search?" <<
        "(0: Linear Search, 1: Binary Search)" << endl;

    cin >> answer2;
    if ( answer == 0 )
    {
        answer == linearSearch;
    else
        answer == binarySearch;
    }
    // Linear Search Function
    int linearSearch( int unsorted[], int numElements, int value )
    {
        int index = 0;                      // Subscript to search array
        int pos = -1;                       // Record postion of search value
        bool found = false;                 // To indicate if value was found

        while ( index < numElements && !found )
        {
            if ( unsorted[ index ] == value ) // If value was found
            {
                found = true;
                pos = index; // record value
            }
            index++; // Move onto next element in array
        }
        return pos; // Return the postion, or -1
    }


    // Binary Search Function
    int binarySearch( int unsorted[], int size, int value )
    {
        int first = 0,
            last = size - 1,
            middle,
            position = -1;
        bool found = false;

        while ( !found && first + last ) / 2; // Calcualte the middle element
        {
            if ( unsorted[ middle ] == value ) // If value is the middle element
            {
                found = true;
                position = middle;
            }
            else if ( unsorted[ middle ] > value ) // If value is in lower part
                last = middle - 1;
            else
                first = middle - 1; // If value is in upper part
        }
        return position;
    }
    return 0;
}

3 个答案:

答案 0 :(得分:0)

这会运行,但你需要检查它,因为我不知道有什么作用。你的二进制搜索功能也需要修复,因为它目前是除以奇数。整个地方都有错误,我会尝试在原始代码中指出它们。

#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>

using namespace std;

int answer;
int answer2;
int size;
int value;

void bubbleSort(int[], int);
void selectionSort(int[], int);
int linearSearch(int unsorted[], int numElements, int value);
int binarySearch(int unsorted[], int size, int value);



int unsorted[100] = {   85, 64, 16, 18, 1,  88, 48, 63, 54, 83, 79, 50, 0, 55,  17, 99, 69, 53, 65,
                        35, 47, 68, 87, 3,  34, 5,  74, 4,  45, 41, 49, 67, 89, 92, 60, 72, 20, 8,
                        15, 42, 71, 31, 36, 90, 84, 70, 51, 28, 32, 81, 10, 82, 40, 57, 24, 25, 58,
                        91, 44, 66, 30, 62, 94, 6,  7,  46, 43, 38, 75, 11, 39, 80, 98, 27, 12, 76,
                        96, 2,  77, 19, 26, 59, 33, 73, 13, 61, 95, 97, 22, 93, 86, 9,  37, 56, 14,
                        23, 21, 52, 78, 29
                    };

void bubbleSort(int unsorted[], int size);
void selectionSort(int unsorted[], int size);

int main()
{
    cout << "Which algorithm should be used to sort? (0: Bubble Sort,1:Selection Sort) " << endl;
    cin >> answer;
    if (answer == 0)
    {
        bubbleSort(unsorted, 100);
    }
    else
    {
        selectionSort(unsorted, 100);
    }

    // Sorting completed
    cout << "Sorting completed" << endl << endl;
    cout << "Which algorithm should be used to search? (0:Linear Search, 1: Binary Search) " << endl;
    cin >> answer2;

    if (answer2 == 0)
    {
        answer2 = linearSearch(unsorted, 100, 4);
        cout << "Binary search result: " << answer2 << endl;
    }

    else
    {
        //answer2 = binarySearch(unsorted, 100, 4);             // binarySearch() you'll have to fix
        //cout << "Binary search result: " << answer2 << endl;  // binarySearch() you'll have to fix
    }

}

// Bubble Sort Fun
void bubbleSort(int unsorted[], int size)
{
    bool swap;
    int temp;

    do
    {
        swap = false;
        for (int count = 0; count < (size - 1); count++)
        {
            if (unsorted[count] > unsorted[count + 1])
            {

                temp = unsorted[count];
                unsorted[count] = unsorted[count + 1];
                unsorted[count + 1] = temp;
                swap = true;
                count++;
            }
        }
    } while (swap);
}

// Selection Sort Function
void selectionSort(int unsorted[], int size)
{
    int startScan, minIndex, minValue;

    for (startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        minValue = unsorted[startScan];

        for (int index = startScan + 1; index < size; index++)
        {
            minValue = unsorted[index];
            minIndex = index;
        }
        unsorted[minIndex] = unsorted[startScan];
        unsorted[startScan] = minValue;
    }
}

// Linear Search Function
int linearSearch(int unsorted[], int numElements, int value)
{
    int index = 0;                      // Subscript to search array
    int pos = -1;                       // Record position of search value
    bool found = false;                 // To indicate if value was found

    while (index < numElements && !found)
    {
        if (unsorted[index] == value)   // If value was found
        {
            found = true;
            pos = index;            // record value
        }
        index++;                    // Move onto next element in array
    }
    return pos;                     // Return the position, or -1
}



//You're going to have to fix this, this will end up dividing odd numbers by two, I don't think that'll work.

//// Binary Search Function
//int binarySearch(int unsorted[], int size, int value)
//{
//      int first = 0,
//          last = size - 1,
//          middle = 0,
//          position = -1;
//      bool found = false;
//
//      while (!found) 
//      {
//          middle = (first + last) / 2;        // Calculate the middle element
//          
//          if (unsorted[middle] == value)      // If value is the middle element
//          {
//              found = true;
//              position = middle;
//          }
//          else if (unsorted[middle] > value)  // If value is in lower part
//              last = middle - 1;
//          else
//              first = middle + 1;             // If value is in upper part
//      }
//      return position;
//  
//  return 0;
//}

答案 1 :(得分:0)

我无法评论,所以我必须在这里回答。 你的代码肯定很长,所以我会在初看时告诉我所知道的。

  1. 您必须声明所有功能。你没有调用二进制搜索功能。

  2. 您定义的函数和原型函数不一样。它必须是一样的。例如:

    int foo(int a); //这是原型 int foo(int a)//这是你定义的函数 { int b; 返回b = a + 5; }

  3. 您的错误很少是未使用的变量,没关系,但这会让您的代码变得复杂。

  4. 调用函数时,必须正确调用。以上面的例子为例,如果我调用了一个函数,我将调用:cout << foo(5);// cout << foo; doesn't work.

  5. 是的,先解决这些问题,剩下的只是小问题。 确保您了解使用教科书的用途。它可以使用,但如果不理解,你就无法找到错误。

答案 2 :(得分:0)

好的,我已经评论了你的代码,事情到处都是。

#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>

using namespace std;
int answer;
int answer2;
int size;
void bubbleSort(int[], int);
void selectionSort(int[], int);

// int binarySearch(int unsorted[], int size, int value)  <--- You need to prototype/declare this function before it is called
// int linearSearch(int unsorted[], int numElements, int value)  <--- Same here

int value;

int unsorted[] =
{
    85, 64, 16, 18, 1, 88, 48, 63, 54, 83, 79, 50, 0, 55,
    17, 99, 69, 53, 65, 22, 93, 86, 9, 37, 56, 23, 21, 52,
    78, 29, 14, 58, 35, 47, 68, 87, 3, 34, 5, 74, 4, 45,
    41, 49, 67, 89, 92, 60, 72, 20, 8, 15, 42, 71, 31, 36,
    90, 84,70, 51, 28, 32, 81, 10, 82, 40, 57, 24, 25, 91,
    44, 66, 30, 62, 94, 6, 7, 46, 43,38, 75, 11, 39, 80,
    98, 27, 12, 76, 96, 2, 77, 19, 26, 59, 33, 73, 13,
    61, 95, 97
};

int main()
{
    cout <<
        "Which algorithm should be used to sort?" <<
        "(0: Bubble Sort, 1: Selection Sort)" << endl;

    cin >> answer;
    if (answer == 0)
    {
        bubbleSort;         // Saying bubbleSort doesn't do anything.  bubbleSort() is a function that takes two arguments
    }
    else
    {
        selectionSort;      // Same here, it should be selectionSort(arg1, arg2)
    }



// Bubble Sort Fun
void bubbleSort(int unsorted[], int size)
{
    bool swap;
    int temp;

    do
    {
        swap = false;
        for (int count = 0; count < (size - 1); count++)
        {
            if (unsorted[count] > unsorted[count - 1]; count++)
            {
                temp = unsorted[count];
                unsorted[count] = unsorted[count + 1];
                unsorted[count + 1] = temp;
                swap = true;
            }
        }
    } while (swap);
}

// Selection Sort Function
void selectionSort(int unsorted[], int size)
{
    int startScan, minIndex, minValue;
    for (startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        minValue = unsorted[startScan];
        for (int index = startScan + 1; index < size; index++)
        {
            minValue = unsorted[index];
            minIndex = index;
        }
        unsorted[minIndex] = unsorted[startScan];
        unsorted[startScan] = minValue;
    }
}


// __________________________________ This whole section should be in main()_____________________________________//

    // Sorting completed
    cout <<
        "Which algorithm should be used to search?" <<
        "(0: Linear Search, 1: Binary Search)" << endl;

    cin >> answer2; 
    if (answer == 0)    // <----- This should be answer2, not answer.
    {
        answer == linearSearch;     // <---- == is wrong.  == is when checking equality, like in the if brackets.  Here you want answer2 = linearSearch(arg1, arg2, arg3);  // Also you'll need to close the bracket to the if code before the "else".  thanks to user4581301 for pointing this out
    else
        answer == binarySearch;     //  Same here.
    }
// ______________________________________________________________________________________________________________//


    // Linear Search Function
int linearSearch(int unsorted[], int numElements, int value)
{
    int index = 0;                      
    int pos = -1;                       
    bool found = false;                 

    while (index < numElements && !found)
    {
        if (unsorted[index] == value) 
        {
            found = true;
            pos = index; 
        }
        index++; 
    }
    return pos; 
}




//  The binary search function might not work, because once you increment or decrement one from middle, 
//  next time you'll be dividing an odd number by two, I think that's a bad idea. 

// Binary Search Function
int binarySearch(int unsorted[], int size, int value)
{
    int first = 0,
        last = size - 1,
        middle,
        position = -1;
    bool found = false;

    while (!found && first + last) / 2;  // <--- I don't think this makes sense.  In the while brackets all you want is !found.
    {
        if (unsorted[middle] == value) 
        {
            found = true;
            position = middle;
        }
        else if (unsorted[middle] > value) 
            last = middle - 1;
        else
            first = middle - 1; 
    }
    return position;
}
return 0;
}