2016-10-07 78 views
0

我在解決我的氣泡排序代碼出錯的地方遇到了問題。我幾乎肯定它在排序算法中。以下是我需要我的代碼來完成:使用交換函數和指針的C++氣泡排序

-Initialize陣列與一個隨機數填充它(我用getNumbers()來實現這一點)

-compare所有後面的元素的第一要素。如果第一個元素較大,則交換它們。

- 逐個比較第二個元素和所有後面的元素。如果第二個元素較大,則交換它們。

- 繼續比較和交換操作,直到倒數第二個元素。

- 打印出數組排序

這裏是我的代碼:

#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; 
} 

回答

2

下面是正確的代碼。

#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[j]array[j]交換array[i]array[j+1]應該被交換。另外,正如dd2所指出的那樣,你的循環邊界並不嚴格。代碼將正常工作,但會採取更多步驟。您可以更改綁定爲j < (arraySize - i - 1)

3

您的循環邊界不正確,並且交換也是錯誤的。

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]); 
     } 
    } 
} 
+1

關於循環邊界的不錯點。 Upvoted。 – user3286661