Hilfe mit Bubblesort!!!



  • Hallo!

    Ich studiere seit Anfang seit einem halbem Jahr Telekommmunikationsinformatik und wir sollen in C+++ ein programm schreibne welches Zufallszahlen erstellt und die dann mit dem Bubblesort wieder ordnet. Das erstellen von den Zufallszahlen klappt einwandfrei aber ich bekomem den bubblesort einfach nicht hin. Ich weiß auch nicht so rechtwo der hin muss und wie und was. Ich weiß nur das er zahlen vergleicht und die kleinste davon immer nach vorne schiebt... kann mirwer helfen? Hier mein Quelltext zum erzeugen der Zufallszahlen:

    --------------------------------------------------------------------

    #include <stdlib.h> // rand() Zufallszahlengenerator
    #include <time.h>
    #include <iostream>
    #include <conio.h> // srand() Initialisierung
    using namespace std;

    int main()
    {
    int x[100], temp, a, b, i;
    time_t timer;
    srand((unsigned)time(&timer));

    cout<<"100 Zahlen unsortiert:\n"<<endl;
    for (i=0; i<100; i++)
    {
    x[i] = rand()% 100;
    cout<<x[i];
    cout.width(4);

    }
    cout<<"\n\n100 Zahlen sortiert:\n"<<endl;
    getch();

    }



  • Hallo

    Bubblesort ist einfach zu finden, zum Beispiel in der Wikipedia.

    bis bald
    akari



  • Hallo!!!

    ja ich weiß das man ihn leicht findet aber ich weiß halt nicht wie ich ihn in meine quelltext einbinde... Das funktioniert bei mir nie...



  • Hallo

    Um bei dem Beispiel aus Wiki zu bleiben

    #include <stdlib.h> // rand() Zufallszahlengenerator
    #include <time.h>
    #include <iostream>
    #include <conio.h> // srand() Initialisierung
    using namespace std; 
    
    void bubblesort(int *ptr, int n)
    {
       for (int i = 1; i <= n-1; ++i)
          for (int j = 0; j < n-i; ++j)
             if (ptr[j] > ptr[j+1])
             {
                 int temp = ptr[j];
                 ptr[j]   = ptr[j+1];
                 ptr[j+1] = temp;
             }
    }
    
    int main()
    {
    int x[100], temp, a, b, i;
    time_t timer;
    srand((unsigned)time(&timer));
    
    // Hier sortieren
    bubblesort(&x[1], 100); // Adresse des Anfangs vom Array und die Anzahl der Elemente übergeben
    
    cout<<"100 Zahlen unsortiert:\n"<<endl;
    for (i=0; i<100; i++)
    {
    x[i] = rand()% 100;
    cout<<x[i];
    cout.width(4);
    
    }
    cout<<"\n\n100 Zahlen sortiert:\n"<<endl;
    getch();
    }
    

    bis bald
    akari



  • Mh so richtig geht das aber auch ncith wenn ich das compiliere.. Nur fehler meldungen...



  • Hallo

    Compilerfehler sollten da nicht kommen, nur ist es noch ein bißchen sinnfrei.
    So muß es Compilieren und auch korrekte Werte anzeigen

    #include <stdlib.h> // rand() Zufallszahlengenerator
    #include <time.h>
    #include <iostream>
    #include <conio.h> // srand() Initialisierung
    using namespace std;
    
    void bubblesort(int *ptr, int n)
    {
       for (int i = 1; i <= n-1; ++i)
          for (int j = 0; j < n-i; ++j)
             if (ptr[j] > ptr[j+1])
             {
                 int temp = ptr[j];
                 ptr[j]   = ptr[j+1];
                 ptr[j+1] = temp;
             }
    }
    
    int main()
    {
    int x[100], temp, a, b, i;
    time_t timer;
    srand((unsigned)time(&timer));
    
    cout<<"100 Zahlen unsortiert:\n"<<endl;
    for (i=0; i<100; i++)
    {
    x[i] = rand()% 100;
    cout<<x[i];
    cout.width(4);
    
    }
    
    // Hier sortieren
    bubblesort(&x[0], 100); // Adresse des Anfangs vom Array und die Anzahl der Elemente übergeben
    
    cout<<"\n\n100 Zahlen sortiert:\n"<<endl;
    for (i=0; i<100; i++)
    {
    cout<<x[i];
    cout.width(4);
    
    }
    
    getch();
    }
    

    bis bald
    akari



  • conio ist ein non-standard-header, srand befindet sich ebenfalls in stdlib.h bzw. cstdlib.



  • zeig mal dein bisherien code -> taikahn


Anmelden zum Antworten