Sortier Algorithmus



  • Hi
    ich nutze einen QuickSort Algorithmus um die Werte in einem Array zu sortieren.
    Nun habe ich das Problem das er wenn ich ihn auf eine Spalte anwende "das Array zerstört" also z.B. er sortiert Zelle[1][2] auf [1][5] ...

    dadurch passen aber die entsprechenden werte nicht mehr zusammen da zwar die eine Spalte sortiert ist aber nicht die tabelle...

    ich möchte das die "ganze" Tabelle entsprechend der einen Spalte sortiert wird und nicht nur die eine spalte, ich finde aber leider kein schönes tutorial wo beschrieben steht wie geht...

    hier mal mein beispiel:
    (c++11)

    <code>

    #include <string>
    #include <iterator>
    #include <iostream>
    #include <algorithm>
    #include <array>

    void quickSort(int array[], int min, int max)
    {
    int i = min, j = max;
    int tmp;
    int pivot = array[((min + max) / 2)];

    while (i <= j)
    {
    while (array[i] < pivot)
    {
    i++;
    }

    while (array[j] > pivot)
    {
    j--;
    }

    if (i <= j)
    {
    tmp = array[i];
    array[i] = array[j];
    array[j] = tmp;
    i++;
    j--;
    }
    }

    if (min < j)
    {
    quickSort(array, min, j);
    }
    if (i< max)
    {
    quickSort(array, i, max);
    }
    }

    int main()
    {
    int paket_info[3][12]= {{ 424,290,295,129,176,131,121,934,385,323,287,350 },
    { 1,1,2,1,3,2,1,1,1,3,2,2 },
    { 8,5,9,1,2,2,4,8,5,7,4,5 }};

    int max=sizeof(paket_info[2])/sizeof(int);

    int x,y;
    x=2;

    std::cout << "Ausgangswerte:(Spalte3) " << std::endl;

    for(int i=0;i<12;i++)
    {
    std::cout<<paket_info[2][i]<<" " ;
    }
    std::cout << std::endl;
    std::cout << "-----------------" << std::endl;

    quickSort(paket_info[2], 0, max);

    for(y=0;y<12;y++)
    {
    for(x=0;x<3;x++)
    {
    std::cout << paket_info[x][y]<<" ; ";
    }
    std::cout << std::endl;
    }
    }

    </code>



  • sry hab unabsichtlich die falschen codetags... 😞

    #include <string>
    #include <iterator>
    #include <iostream>
    #include <algorithm>
    #include <array>
    
    void quickSort(int array[], int min, int max)
     {
    	int i = min, j = max;
    	int tmp;
    	int pivot = array[((min + max) / 2)];
    
    	while (i <= j)
    	{
    		while (array[i] < pivot)
    		{
    			i++;
    		}
    
    		while (array[j] > pivot)
    		{
    			j--;
    		}
    
    		if (i <= j)
    		{
    			tmp = array[i];
    			array[i] = array[j];
    			array[j] = tmp;
    			i++;
    			j--;
    		}
    	}
    
    	if (min < j)
    	{
    		quickSort(array, min, j);
    	}
    	if (i< max)
    	{
    		quickSort(array, i, max);
    	}
    }
    
    int main()
    {
    	int paket_info[3][12]= {{ 424,290,295,129,176,131,121,934,385,323,287,350 },
    							{ 1,1,2,1,3,2,1,1,1,3,2,2 },
    							{ 8,5,9,1,2,2,4,8,5,7,4,5 }};
    
    	int max=sizeof(paket_info[2])/sizeof(int);
    
        int x,y;
        x=2;
    
        std::cout << "Ausgangswerte:(Spalte3) " << std::endl;
    
    	for(int i=0;i<12;i++)
    	{
    		std::cout<<paket_info[2][i]<<" " ;
    	}
    	std::cout << std::endl;
    	std::cout << "-----------------" << std::endl;
    
    	quickSort(paket_info[2], 0, max);
    
    	for(y=0;y<12;y++)
    	{
    		for(x=0;x<3;x++)
    		{
    			std::cout << paket_info[x][y]<<" ; ";
    		}
    		std::cout << std::endl;
    	}
    }
    


  • Dann implementiere selber einen Sortieralgo, den Du auch verstehst. Den kannste ganz leicht anpassen, sodaß er ganze Zeilen anhand eines Attributs sortiert.
    Und dann kannste das aus einem C-Tut gecopypastete Quicksort auch anpassen.

    Und das ist wohl die gemeine Zeile im Code:

    void void quickSort(int array[3][], int min, int max)
    


  • volkard schrieb:

    Dann implementiere selber einen Sortieralgo, den Du auch verstehst. Den kannste ganz leicht anpassen, sodaß er ganze Zeilen anhand eines Attributs sortiert.
    Und dann kannste das aus einem C-Tut gecopypastete Quicksort auch anpassen.

    Und das ist wohl die gemeine Zeile im Code:

    void void quickSort(int array[3][], int min, int max)
    

    jop bin gerade am "verstehen" des Sortier Algorithmus hatte mit so etwas vorher noch nix zutun ...
    bin nur nicht weiter gekommen weil ich das anders versucht hatte... 🙄

    vielen dank ich versuchs gleich mal... 🙂



  • ich habe das jetzt angepasst, aber jetzt sortiert er nicht mehr, wieso das?
    was hab ich falsch gemacht? 😕

    void quickSort(int array[3][12], int min, int max)
     {
    	int i = min, j = max;
    	int tmp;
    	int pivot = array[3][((min + max) / 2)];
    
    	while (i <= j)
    	{
    		while (array[3][i] < pivot)
    		{
    			i++;
    		}
    
    		while (array[3][j] > pivot)
    		{
    			j--;
    		}
    
    		if (i <= j)
    		{
    			tmp = array[3][i];
    			array[3][i] = array[3][j];
    			array[3][j] = tmp;
    			i++;
    			j--;
    		}
    	}
    
    	if (min < j)
    	{
    		quickSort(array, min, j);
    	}
    	if (i< max)
    	{
    		quickSort(array, i, max);
    	}
    }
    


  • [2]

    und dann musste das tauschen zum großtausch machen.



  • volkard schrieb:

    [2]

    und dann musste das tauschen zum großtausch machen.

    jo ich glaube ich habs gelöst... meinst du in etwa so?

    ich hab jetzt einfach da wo er die werte "tauscht" die anderen werte des arrays mitgetauscht... eig total logisch wenn ich so darüber nachdenke... 🙂
    viel dank so wie es aussieht passt das so 😃 ...

    void quickSort(int array[3][12], int min, int max)
     {
    	int i = min, j = max;
    	int tmp;
    	int tmp2;
    	int tmp3;
    	int pivot = array[2][((min + max) / 2)];
    
    	while (i <= j)
    	{
    		while (array[2][i] < pivot)
    		{
    			i++;
    		}
    
    		while (array[2][j] > pivot)
    		{
    			j--;
    		}
    
    		if (i <= j)
    		{
    			tmp = array[2][i];
    			array[2][i] = array[2][j];
    			array[2][j] = tmp;
    
    			tmp2 = array[1][i];
    			array[1][i] = array[1][j];
    			array[1][j] = tmp2;
    
    			tmp3 = array[0][i];
    			array[0][i] = array[0][j];
    			array[0][j] = tmp3;
    
    			i++;
    			j--;
    
    		}
    	}
    
    	if (min < j)
    	{
    		quickSort(array, min, j);
    	}
    	if (i< max)
    	{
    		quickSort(array, i, max);
    	}
    }
    


  • problem gelöst... 😃
    thema kann geschlossen werden ... 🙂

    vielen dank für die hilfe 🙂 ...

    lg



  • Witzig, hab gestern Abend genau das selbe gebraucht 😃

    Falls es dich noch interessiert:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    int main() {
    	using namespace std;
    	vector<vector<int>> p = {{ 3, 4, 5 },
                                { 1, 2 },
                                { 8, 7, 8 }};
    
    	auto p_cmp = [](vector<int> const &lhs, vector<int> const &rhs) { return lhs[0] < rhs[0]; };
    	sort(begin(p), end(p), p_cmp); // Sortiert nach der ersten Spalte
    
    	for (auto const &i : p) {
    		for (auto const &j : i) 
    			cout << j << ' '; 
    		cout << '\n';
    	}
    }
    


  • happystudent schrieb:

    Witzig, hab gestern Abend genau das selbe gebraucht 😃

    Falls es dich noch interessiert:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    int main() {
    	using namespace std;
    	vector<vector<int>> p = {{ 3, 4, 5 },
                                { 1, 2 },
                                { 8, 7, 8 }};
    	
    	auto p_cmp = [](vector<int> const &lhs, vector<int> const &rhs) { return lhs[0] < rhs[0]; };
    	sort(begin(p), end(p), p_cmp); // Sortiert nach der ersten Spalte
    	
    	for (auto const &i : p) {
    		for (auto const &j : i) 
    			cout << j << ' '; 
    		cout << '\n';
    	}
    }
    

    jo doch 🙂 vielen dank ... 🙄



  • Schoen zu sehen, dass immer mehr aktuelles C++11 eingesetzt wird, schliesslich ist der "neue" Standard ja schon wieder ein paar Jahre alt und der naechste steht vor der Tuer.



  • qwertzuiop schrieb:

    Schoen zu sehen, dass immer mehr aktuelles C++11 eingesetzt wird, schliesslich ist der "neue" Standard ja schon wieder ein paar Jahre alt und der naechste steht vor der Tuer ist auch schon seit Sommer draußen.

    FTFY.


Anmelden zum Antworten