2016-10-07 2 views
0

Ich habe Probleme herauszufinden, wo meine Bubble-Sort-Code falsch gelaufen ist. Ich bin fast sicher, dass es im Sortieralgorithmus ist. Hier ist, was ich meinen Code müssen erreichen:C++ Bubble Sort mit Swap-Funktion und Pointer

-initialize ein Array eine Füllung mit Zufallszahlen (ich benutze getNumbers(), dies zu erreichen)

-Vergleichung das erste Element mit allen späteren Elemente. Wenn das erste Element größer ist, tauschen Sie sie aus.

-Compare das zweite Element mit allen späteren Elementen eins nach dem anderen. Wenn das zweite Element größer ist, tauschen Sie sie aus.

-Continue Vergleichen und Austauschen von Operationen bis zum vorletzten Element.

-Print aus sortierten Array

Und hier ist mein Code:

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

Antwort

2

Hier ist der richtige Code.

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

Sie wurden Swapping array[i] mit array[j] in Zeile 32. array[j] und array[j+1] getauscht werden sollte. Wie von dd2 gezeigt, sind Ihre Schleifengrenzen nicht streng. Der Code würde trotzdem korrekt funktionieren, würde aber mehr Schritte machen. Sie können die Grenze zu j < (arraySize - i - 1)

3

ändern Ihre Loop-Grenzen sind nicht korrekt und Swapping war auch falsch.

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

Schöner Punkt über die Schleifengrenzen. Upvoted. – user3286661