#1 29. Juni 2007 Sortier-Alghoritmus Hi Leute! Ich hab ein Feld dist (dist > 0, i geht von 0 bis zu j) jetzt soll er dist[0] bis j nach größe sortien (von klein nach groß) Noch dazu: zu jedem dist kommt ein fx, fy und ein dir zu... die soll er dann auch richtig sortieren.... beispiel: dist[0] = 60 - fx[0] = 4 - ... dist[1] = 20 - fx[1] = 8 - ... dist[2] = 70 - fx[2] = 2 - ... dist[3] = 10 - fx[3] = 1 - ... dist[4] = 80 - fx[4] = 4 - ... dist[5] = 30 - fx[5] = 2 - ... soll sortiert werden nach: dist[0] = 10 - fx[0] = 1 - ... dist[1] = 20 - fx[1] = 8 - ... dist[2] = 30 - fx[2] = 2 - ... dist[3] = 60 - fx[3] = 4 - ... dist[4] = 70 - fx[4] = 2 - ... dist[5] = 80 - fx[5] = 4 - ... wichtig sind dabei eigentlich nur dist[0 -2], denn rest brauch ich nicht mehr... meinetwegen kann er sie auch in irgendeine andere variable speichern... und das muss ich in C programieren... kann mir da jemand helfen? mfg Thrake7 + Multi-Zitat Zitieren
#2 29. Juni 2007 AW: Sortier-Alghoritmus Code: #include <iostream> using namespace std; int main(void) { int dist[] = { 1, 4, 7, 9, 2, 66, 13, 97, 45, 49, 894, 68, 87 }; int fx[] = { 65, 4, 56, 87, 23, 75, 234, 74, 26, 54, 34, 562}; int tempd; int tempf; for(int i = 0; i < sizeof(dist)/4; i++) // sizeof(...) / 4, da int 4 bytes groß ist.... { for(int j = 0; j < (sizeof(dist)/4)-1; j++) // -1, damit nicht beim letzten Elemten versucht wird auf nicht reservierten Speicher { // zuzugreifen if((dist[j] - fx[j]) > (dist[j+1] - fx[j+1])) { tempd = dist[j]; tempf = fx[j]; dist[j] = dist[j+1]; fx[j] = fx[j+1]; dist[j+1] = tempd; fx[j+1] = tempf; } } for(int j = 0; j < sizeof(dist)/4; j++) { cout << "dist[" << j << "] = " << dist[j] << "\t fx[" << j << "] = " << fx[j] << "\t " << dist[j] << " - " << fx[j] << " = \t" << dist[j] - fx[j] << endl; } cout << endl; } cout << "ferdisch" << endl; system("PAUSE"); return EXIT_SUCCESS; } hf & gl damit! falls fragen sind pm! mfg thecoolman4rr + Multi-Zitat Zitieren
#3 29. Juni 2007 AW: Sortier-Alghoritmus Hallo, hab mal schnell BubbleSort für int's gecodet: Code: void BubbleSortInt(int *data, int count) { bool vertauscht; int n = count - 1; do { vertauscht = false; for (int i = 0; i < n; i++) { if (data[i] > data[i + 1]) { int temp = data[i]; data[i] = data[i + 1]; data[i + 1] = temp; vertauscht = true; } } } while (vertauscht); return; } Und hier ein Verwendungsbeispiel: Code: int data[5]; srand(GetTickCount()); for (int i = 0; i < 5; i++) { data[i] = rand(); } BubbleSortInt(&data[0], 5); mfg r90 (P.S.: nicht für große Datenmengen verwenden, denn BubbleSort ist langsam!) + Multi-Zitat Zitieren
#4 29. Juni 2007 AW: Sortier-Alghoritmus "grafische Darstellung" added Code: #include <iostream> using namespace std; int main(void) { int dist[] = { 1, 5, 7, 9, 2, 66, 13, 97, 45, 49, 894, 68, 87 }; int fx[] = { 65, 4, 56, 87, 23, 75, 234, 74, 26, 54, 34, 562}; int tempd; int tempf; for(int i = 0; i < sizeof(dist)/4; i++) // sizeof(...) / 4, da int 4 bytes groß ist.... { for(int j = 0; j < (sizeof(dist)/4)-1; j++) // -1, damit nicht beim letzten Elemten versucht wird auf nicht reservierten Speicher { // zuzugreifen if((dist[j] - fx[j]) > (dist[j+1] - fx[j+1])) { tempd = dist[j]; tempf = fx[j]; dist[j] = dist[j+1]; fx[j] = fx[j+1]; dist[j+1] = tempd; fx[j+1] = tempf; } } for(int j = 0; j < sizeof(dist)/4; j++) { cout << "dist[" << j << "] = " << dist[j] << "\t fx[" << j << "] = " << fx[j] << "\t " << dist[j] << " - " << fx[j] << " = \t" << dist[j] - fx[j] << endl; } cout << endl; } /////////////////////////////////////////////////// // Ab hier an nur noch "grafische" Darstellung ^^// /////////////////////////////////////////////////// int YMass = 0; int Hoehe = 0; for(int i = 10; i < 10000; i*10) { if((dist[(sizeof(dist)/4)-1] - fx[(sizeof(dist)/4)-1]) > i) { Hoehe = (static_cast<int> (dist[(sizeof(dist)/4)-1] - fx[(sizeof(dist)/4)-1])) / i; YMass = i; break; } } bool XAchse = true; bool Strich = false; cout << " ^ Y(dist[x]-fx[x] / " << YMass << ")\n"; for(int i = (sizeof(dist)/4)-1; i > 0; i--) { cout << " |"; if((dist[i] - fx[i]) == 0 && XAchse) { XAchse = false; Strich = true; } else if((dist[i] - fx[i]) < 0 && XAchse) { XAchse = false; cout << "\b\b0|-------------------------------------> x-Achse(Anzahl der Werte)\n |"; } for(int j = 0; j < i; j++) { if(Strich) { cout << "-"; } else { cout << " "; } } cout << "x " << dist[i] - fx[i]; if(Strich) { for(int k = 0; k < (36-i); k++) { cout << "-"; } cout << ">"; } int j = YMass; while(((dist[i] - fx[i]) - ((dist[i-1] - fx[i-1]) + j)) >= 0) { cout << "\n |"; j += YMass; } Strich = false; cout << "\n"; } cout << " |x " << dist[(sizeof(dist)/4)-1] - fx[(sizeof(dist)/4)-1] << endl; system("PAUSE"); return EXIT_SUCCESS; } mfg thecoolman4rr + Multi-Zitat Zitieren