Site hosted by Angelfire.com: Build your free website today!

Sortieralgorithmen in einem Programm


Zur individuellen Anpassung im folgenden Programm bietet sich an, den Wert von
const int N zu verändern,
in der main() Funktion alle Sortierfunktionen bis auf eine auszukommentieren (wie es auch der Fall ist), mit der dann 
das Feld sortiert wird. Es ist schon bei N = 10000 oder N = 100000 deutlich zu beobachten, wie die Laufzeiten der 
verschieden Algorithmen z.T. beträchtlich auseinander differieren.
Bitte bei der Benutzung von cm_DistributionCounting die Kommentare im Quelltext beachten.
Und bei zu großen Werten für N gibt es schöne Fehler zur Laufzeit des Programms. Wie groß N dazu sein muß,
kann jeder selbst herausfinden.

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

typedef int itemType;
const int N = 20;      // N ist Konstante für Elementanzahl

void cm_Tausch(itemType feld[], int i, int j);
void cm_Selection(itemType feld[], int N);
void cm_Insertion(itemType feld[], int N);
void cm_Bubble(itemType feld[], int N);
void cm_Shellsort(itemType feld[], int N);
void cm_DistributionCounting(itemType feld[], int M, int N);
void cm_quicksort(itemType feld[], int l, int r);


int main() {
     itemType array[N+1];
     itemType spezial[20+1]={0, 3, 5, 1, 1, 5, 3, 1, 1, 2, 3, 2, 5, 1, 2, 3, 3, 4, 4, 1, 5};

     srand( (unsigned)time( NULL ) );
     for(int counter=1; counter<=N; ++counter) {
         array[counter] = rand();
         cout << "   " << array[counter];
     }

     // cm_Selection(array, N);
     // cm_Insertion(array, N);
     // cm_Bubble(array, N);
     // cm_Shellsort(array, N);
     // cm_DistributionCounting(spezial, 6, 20);
     cm_quicksort(array, 1, N); 

     cout << "\n\n";
     for(counter=1; counter <=N; ++counter) {     // bei Distribution Counting N durch 20
         cout << "    " << array[counter];      // und array durch spezial ersetzen
     }
     return 0;
}


void cm_Tausch(itemType feld[], int i, int j) {
     itemType temp;
     temp = feld[i];
     feld[i] = feld[j];
     feld[j] = temp;
}


void cm_Selection(itemType feld[], int N) {
     int i, j, min;
     for(i=1; i<N; ++i) {
         min = i;
         for(j=i+1; j<=N; ++j) {
             if(feld[j] < feld[min]) {
                   min = j;
             }
         }
         cm_Tausch(feld, min, i);
     }
}


void cm_Insertion(itemType feld[], int N) {
     int i, j;
     itemType wert;
     for(i=2; i<=N; ++i) {
         wert = feld[i];
         j = i;
         while(feld[j-1] > wert) {
             feld[j] = feld[j-1];
             --j;
         }
         feld[j] = wert;
     }
}


void cm_Bubble(itemType feld[], int N) {
     int i, j;
     for(i=N; i>=1; --i) {
         for(j=2; j<=i; ++j) {
             if(feld[j-1] > feld[j]) {
                 cm_Tausch(feld, j-1, j);
             }
         }
     }
}


void cm_Shellsort(itemType feld[], int N) {
     int i, j, h;
     itemType wert;
     for(h=1; h<=N/9; h=3*h+1);
     for( ; h>0; h/=3) {
         for(i=h+1; i<=N; i+=1) {
             wert = feld[i];
             j = i;
             while(j>h && feld[j-h] > wert) {
                     feld[j] = feld[j-h];
                     j -= h;
             }
             feld[j] = wert;
         }
     }
}


void cm_DistributionCounting(itemType feld[], int M, int N) {
        int i, j;
        itemType *zaehlfeld = new itemType[M];
        itemType *hilfsfeld = new itemType[N+1];

        for(j=0; j<M; ++j) zaehlfeld[j] = 0;
        for(i=1; i<=N; ++i) zaehlfeld[zaehlfeld[i]]++;
        for(j=1; j<M; ++j) zaehlfeld[j] += zaehlfeld[j-1];
        for(i=N; i>=1; --i) hilfsfeld[zaehlfeld[feld[i]]--] = feld[i];
        for(i=1; i<=N; ++i) feld[i] = hilfsfeld[i];
}


void cm_quicksort(itemType feld[], int l, int r) {
    int i, j;
    itemType wert;
    if(r > l) {
        wert = feld[r];
        i = l-1;
        j = r;
        for( ; ; ) {
            while(feld[++i] < wert) ;
            while(feld[--j] > wert) ;
            if(i >= j) break;
            cm_Tausch(feld, i, j);
        }
        cm_Tausch(feld, i, r);
        cm_quicksort(feld, l, i-1);
        cm_quicksort(feld, i+1, r);
    }
}


   Computer    Programmieren (incl. C++ Kurs)    Algorithmen    Bücher    

Zeitschriften    Heavy Metal    Mountainbiking    Meine Katze    

Über mich und die Site    Links    Downloads    Gästebuch    HP mit Umfrage