Nochmal Bubblesort



  • Hallo!! Ich komme absolut nicht klar mit dem Bubblesort!Kann mir mal jemand den Bubblesort hier in meinem Quelltext genau erklären warum das nicht funktioniert!! Ich komme absolut nicht kalr und dieses teil will mir nciths so richtig ordnen!! Danke für eure Hilfe!!Hier mein Quelltext:

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

    int main()
    {
    int x[4], temp, a, b, i;

    time_t timer; //time_t Integertyp in Headerdatei time.h definiert
    srand((unsigned)time(&timer)); // srand() initialisiert Zufallszahlengenerator

    //Das Array mit 100 zufälligen Zahlen füllen
    cout<<"3 Zahlen unsortiert:\n"<<endl;
    for (i=0; i<4; i++)
    {
    x[i] = rand()% 20; // Wählt eine Zufalsszahl zwischen 1 und 20
    cout<<x[i];
    cout.width(4);
    }

    cout<<"\n\n3 Zahlen sortiert:\n"<<endl;

    for (i=0; i<4; i++)

    /{
    cout<<x[i];
    cout.width(4);
    }
    /
    if (x[i] > x[i+1])

    {
    temp = x[i];
    x[i] = x[i+1];
    x[i+1] = temp;
    cout<<x[i];
    cout.width(4);
    }

    getch();

    }



  • hier ist nochmal dein code mit anmerkungen...

    #include <stdlib.h> // rand() Zufallszahlengenerator
    #include <time.h>
    #include <iostream>
    #include <conio.h> // srand() Initialisierung
    using namespace std;
    
    int main()
    {
    int x[4], temp, a, b, i;         
    
    time_t timer; //time_t Integertyp in Headerdatei time.h definiert
    srand((unsigned)time(&timer)); // srand() initialisiert Zufallszahlengenerator
    
    //Das Array mit 4 zufälligen Zahlen füllen
    cout<<"4 Zahlen unsortiert:\n"<<endl;
    for (i=0; i<4; i++)
    {
    x[i] = rand()% 20; // Wählt eine Zufalsszahl zwischen 1 und 20
    cout<<x[i];
    cout.width(4);
    }
    
    cout<<"\n\n4 Zahlen sortiert:\n"<<endl;
    for(int j = 0; j <4; j++)                //du musst deine sortierung natürlich öfter als 1 mal durchlaufen... 
    for (i=0; i<3; i++)               //i sollte maximal nur =2 sein, da du sonst irgendwo im speicher rumstocherst..... denn i+1 ist maximal 3 im array! du hattest immer i+1 = 4!
    {
    	if (x[i] >= x[i+1])
    	{
    		temp = x[i];
    		x[i] = x[i+1];
    		x[i+1] = temp;
    	}
    }
    for (i=0; i<4; i++)            //die ausgabe der reihenfolge wurde ans ende verlagert...
    {
    cout<<x[i];
    cout.width(4);
    }
    
    getch();
    
    }
    

    nun sollt es funktionieren... schönen abend noch...



  • Vielen Vieln Dank!! Ich werde mir das jetzt nochmal genau anschauen!! Danke!!



  • Hab deinen Code mal so geändert wie ich das etwa machen würde
    So macht er auch nur soviele Durchläufe wie wirklich nötig sind kann ja sein das nur zwei Zahlen getausche werden müssen und das Array ist schon sortiert.

    #include <stdlib.h> // rand() Zufallszahlengenerator 
    #include <time.h> 
    #include <iostream> 
    using namespace std; 
    
    void bubbleSort(int* values,int size)
    {
    	bool hadtoSwitch=false;
    	do
    	{	
    		// Merker zurücksetzen
    		hadtoSwitch=false;
    		// Gehe durchs ganze Array
    		for(int i=0;i<size-1;i++)
    		{
    			if(values[i]>values[i+1])
    			{
    				// Ist das aktuelle Element grösser als das nächste Element im Array 
    				// tausche die zwei Elemente
    				int tmp=values[i];
    				values[i]=values[i+1];
    				values[i+1]=tmp;
    				// Merk dir das
    				hadtoSwitch=true;
    			}
    		}
    	}while(hadtoSwitch); // Mach das solange bis nicht mehr getauscht werden musste
    }
    
    int main() 
    { 
    	int x[4]; 
        const int xSize=4;
    	time_t timer; //time_t Integertyp in Headerdatei time.h definiert 
    	srand((unsigned)time(&timer)); // srand() initialisiert Zufallszahlengenerator 
    
    	//Das Array mit xSize zufälligen Zahlen füllen 
    	cout<< xSize << " Zahlen unsortiert:\n"<<endl; 
    	for (int i=0; i<xSize; i++) 
    	{ 
    		x[i] = rand()% 20; // Wählt eine Zufalsszahl zwischen 1 und 20 
    		cout<<x[i]; 
    		cout.width(4); 
    	} 
    
    	// Das Array sortieren
    	bubbleSort(x,xSize);
    
    	cout<<"\n\n" << xSize << " Zahlen sortiert:\n"<<endl; 
    
    	for (int i=0; i<xSize; i++) 
    	{ 
    		cout<<x[i]; 
    		cout.width(4); 
    	}
    
    }
    

Anmelden zum Antworten