Prof. Dr. R. Laue                                                                                                                                  SS00
                                Informatik II
                                Übungsblatt 3
                               


Lösung Aufgabe 5 (10 Programmierpunkte - Abgabe per email)


#include <stdio.h> 
#include <stdlib.h> 
#include <sys/times.h> 

#define DIM 1000000

          main() 
          { 
                  struct tms buffer; 
                  int feld[DIM]; 
                  int feld_kopie[DIM]; 

                  int i; 
                  for (i=0;i<DIM;i++) 
                          { 
/*
				if (i%2)
				  feld[i] = feld_kopie[i] = rand(); 
				else
*/
				  feld[i] = feld_kopie[i] = 7; 
                          } 
                  times(&buffer); 
                  printf("Zeit vor dem Sortieren: %d\n",buffer.tms_utime); 
                  my_qsort_000000(feld, DIM ); 
                  times(&buffer); 
                  printf("Zeit vor dem UNIX Sortieren: %d\n",buffer.tms_utime); 
                  my_qsort_000000(feld_kopie, DIM ); 
                  times(&buffer); 
                  printf("Zeit nach dem UNIX Sortieren: %d\n",buffer.tms_utime); 

                  if (memcmp(feld,feld_kopie, DIM * sizeof(int) ) != 0) 
		    printf("Wir haben Probleme\n"); 
          } 

          my_comp(int *a, int *b) 
          { 
                  return *a - *b; 
          } 

/*
          my_qsort_000000(int *feld, int feld_laenge) 
          { 
                  qsort(feld,feld_laenge, sizeof(int), my_comp); 
          }
*/
my_qsort_000000(int *feld, int feld_laenge) 
{
	qs(feld,0,feld_laenge-1);

}

qs(int a[],int l,int r)
{
int i,j,h,v;
if (r>l) 
	{
	i = l; j = r; v=a[l];
	do {
		do { i++; } while ((i<=r) && (a[i]<v));
		do { j--; } while ((j>=l) && (a[j]>v));

		if (j<i) break;
		h = a[i];a[i]=a[j];a[j]=h;
	} while (1);
	h = a[l];a[l]=a[j];a[j]=h;
	qs(a,l,j-1);
	qs(a,i,r);
	}
}