【问题标题】:C++ Bubble Sort Using Swap Function and Pointers使用交换函数和指针的 C++ 冒泡排序
【发布时间】:2016-10-07 05:50:20
【问题描述】:

我在找出冒泡排序代码出错的地方时遇到了问题。我几乎肯定它在排序算法中。这是我需要我的代码来完成的:

-初始化一个数组并用随机数填充它(我使用 getNumbers() 来完成此操作)

-将第一个元素与所有后面的元素进行比较。如果第一个元素较大,则交换它们。

- 将第二个元素与所有后面的元素一一进行比较。如果第二个元素较大,则交换它们。

-继续比较和交换操作,直到倒数第二个元素。

-打印出排序后的数组

这是我的代码:

#include <iostream>
#include <cstdlib>

using namespace std;

void swap(int *, int *);

int *getNumbers(int);

int main()
{
    //Get the size of the array from keyboard
    int arraySize;
    cout << "How many integers would you like to declare: ";
    cin >> arraySize;

    //Initialize array  
    int *array;
    array = getNumbers(arraySize); //getNumbers should return a pointer
    //Print out original array
    cout << "Original array" << endl;
    for(int count = 0; count < arraySize; count++)
    {
        cout << *(array + count) << " ";
    }
    //Sort array using the swap function
    //Have a for loop to swap numbers one by one from min to max
        //Compare values using a second for loop
            //Swap values if former value is larger
            //swap(&array[i],&array[j]);
    for(int i = 0; i < arraySize; i++)
    {
        for(int j = 0; j < (arraySize - 1); j++)
        {
            if(array[j] > array[j + 1])
            {
                swap(&array[i], &array[j]);
            }
        }
    }
    //Print out sorted array
    cout << "\nSorted Array" << endl;
    for(int count = 0; count < arraySize; count++)
    {
        cout << *(array + count) << " ";
    }
    return 0;
}

void swap(int *num1, int *num2)
{
    //Keep record of original value of num1
    int tempNum;
    tempNum = *num1;
    *num1 = *num2;  //num1 value has been changed
    *num2 = tempNum;    //Fetch the original value of num1 and assign it to num2
}

int *getNumbers(int size)
{
    int *array;

    array = new int[size];

    srand(time(0));

    for(int i = 0; i < size; i++)
    {
        array[i] = rand() % 100;
    }
    return array;
}

【问题讨论】:

    标签: c++ arrays sorting


    【解决方案1】:

    这是正确的代码。

    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    void swap(int *, int *);
    
    int *getNumbers(int);
    
    int main() {
      //Get the size of the array from keyboard
      int arraySize;
      cout << "How many integers would you like to declare: ";
      cin >> arraySize;
    
      //Initialize array
      int *array;
      array = getNumbers(arraySize); //getNumbers should return a pointer
      //Print out original array
      cout << "Original array" << endl;
      for (int count = 0; count < arraySize; count++) {
        cout << *(array + count) << " ";
      }
      //Sort array using the swap function
      //Have a for loop to swap numbers one by one from min to max
      //Compare values using a second for loop
      //Swap values if former value is larger
      //swap(&array[i],&array[j]);
      for (int i = 0; i < arraySize; i++) {
        for (int j = 0; j < (arraySize - 1); j++) {
          if (array[j] > array[j + 1]) {
            /*********** This line was changed ***********/
            swap(&array[j+1], &array[j]); // You were earlier swapping ith and jth entries.
            /*********************************************/
          }
        }
      }
      //Print out sorted array
      cout << "\nSorted Array" << endl;
      for (int count = 0; count < arraySize; count++) {
        cout << *(array + count) << " ";
      }
      return 0;
    }
    
    void swap(int *num1, int *num2) {
      //Keep record of original value of num1
      int tempNum;
      tempNum = *num1;
      *num1 = *num2;  //num1 value has been changed
      *num2 = tempNum;    //Fetch the original value of num1 and assign it to num2
    }
    
    int *getNumbers(int size) {
      int *array;
    
      array = new int[size];
    
      srand(time(0));
    
      for (int i = 0; i < size; i++) {
        array[i] = rand() % 100;
      }
      return array;
    }
    

    您在第 32 行中将 array[i]array[j] 交换。array[j]array[j+1] 应该交换。此外,正如 dd2 所指出的,您的循环界限并不严格。该代码仍然可以正常工作,但需要更多步骤。您可以将绑定更改为j &lt; (arraySize - i - 1)

    【讨论】:

      【解决方案2】:

      您的循环边界不正确,交换也是错误的。

      for(int i = 0; i < arraySize; i++)
      {
          for(int j = 0; j < (arraySize - i - 1); j++)
          {
              if(array[j] > array[j + 1])
              {
                  swap(&array[j], &array[j+1]);
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-17
        • 2018-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多