我正在尝试创建气泡排序算法,并且不断遇到错误。我在第一个for循环中经常遇到错误,它声称这是一个不合格的ID。我还遇到了一些错误,其中一个变量需要在for循环中再次声明类型,这会导致整个代码出现问题。
bubble-sort.cpp:12:1: error: expected unqualified-id before ‘for’
12 | for (int hop=0; hop <= 10; hop++){
| ^~~
bubble-sort.cpp:12:17: error: ‘hop’ does not name a type
12 | for (int hop=0; hop <= 10; hop++){
| ^~~
bubble-sort.cpp:12:28: error: ‘hop’ does not name a type
12 | for (int hop=0; hop <= 10; hop++){
| ^~~
bubble-sort.cpp:28:1: error: expected declaration before ‘}’ token
28 | };
| ^
我从这段代码中得到了这些错误。
#include <iostream>
using namespace std;
int array [5] = {19,3,90,1,9};
class BubSor {
public:
int temp;
for (int hop=0; hop <= 10; hop++){
while (hope >=10 ){
for (int j=0; j <=5; j++;){
if (array[j]>array[j+1]){
array [j] = temp;
array [j+1] = array [j];
temp = array [j+1];
}
}
}
}
};
};
int main (){
BubSor object;
object ;
return 0;
}
答案 0 :(得分:-1)
可以帮助您改进原始代码的评论:
// avoid making global variables. Only constants that are used throughout your program should be made global.
//Also when making an array like this instead of using 5 do the following
// const int SIZE = 5;
// int array[SIZE] = {19,3,90,1,9};
// this way you can use the size later on
int array [5] = {19,3,90,1,9};
class BubSor {
public:
// you have to put this in a function
int temp;
// bounding it by 10 is not correct because this number will change
// and you also the most passes you will ever have to make will be the size of the array
for (int hop=0; hop <= 10; hop++){
// you do not need this while loop
// also hope should be hope
while (hope >=10 ){
// this should be bound by the size of the array -1, because you are comparing to j + 1, otherwise you will go over the size of the array
for (int j=0; j <=5; j++;){
if (array[j]>array[j+1]){
// here you should place it into temp instead
array [j] = temp;
// this should be done in reverse order
array [j+1] = array [j];
// swap the next line too
temp = array [j+1];
}
}
}
}
}; // you do not need the extra ; here
}; // this one is necessary because it is a class
int main (){
BubSor object;
// here you need to call the function that is inside the class
object ;
return 0;
}
我上面给出的建议的可能实现版本:
// no global variable array
class BubSor {
public:
// the code is wrapped in a reusable function called sort which takes
// as parameters an array and its size N
void sort(int* array, const int N)
{
int temp;
bool swapped = true; // if you haven't made any swaps then the array is already sorted
// bound the for loop by the actual size of the array
for (int hop = 0; hop < N && swapped; hop++) {
swapped = false;
// inner for loop goes only to the size -1 because
// we are comparing j to j + 1, otherwise we go over the size
// of the array
for (int j = 0; j < N-1; j++) {
// if unordered swap
if (array[j] > array[j + 1]) {
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
swapped = true;
}
}
}
}
};
int main() {
// good practice to place it in a const
const int N = 13;
int arr[N] = { 19,3,1,2,3,11,10,9,3,4,90,1,9 };
BubSor object;
// use the sort function to sort the array
object.sort(arr, N);
// print the array to see if the function is working
for (int x : arr)
{
cout << x << " ";
}
cout << "\n";
}