我正在處理一個程序,該程序對數組進行排序,然後使用氣泡或選擇排序搜索數組,然後使用二進制或線性搜索。它應該詢問用戶將要使用哪個排序和搜索功能。它應該始終顯示搜索或排序數組所需的步驟。它是一個我必須使用的固定(常量)數組。我有一個爲此製作的程序的基礎,但它給了我一些錯誤。如果你們有機會,請讓我知道我做錯了什麼。我認爲這是一個簡單的錯誤,但我不確定。我是編程新手,請給我你的想法!搜索並排序未排序的數組
#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;
}
什麼樣的錯誤? –
'bubbleSort;'看起來你錯過了一些重要的東西,可能是*參數*? – PaulMcKenzie
一些作業的複製粘貼代碼(你沒有做)的味道。使用未聲明的變量......這只是一般的混亂。 –