Exemplo n.º 1
0
static void sortrnd(void** array, int size,
                    int(*comp)(const void *, const void *),
                    double* seed)
{
    if (size <= 15)
        selectionsort(array, size, comp);

    else{
        void*       pivot = array[irand(seed, size)];
        void*       tmp;
        int         i = -1;
        int         j = size;

        for(;;){
            do i++; while(comp(array[i], pivot)<0);
            do j--; while(comp(pivot, array[j])<0);

            if (i >= j) break;

            tmp = array[i]; array[i] = array[j]; array[j] = tmp;
        }

        sortrnd(array    , i     , comp, seed);
        sortrnd(&array[i], size-i, comp, seed);
    }
}
Exemplo n.º 2
0
int main(){
	
	int numbers[ARRAY_NUM];
	for(int i =0; i < ARRAY_NUM;i++){
		numbers[i] = (rand() % 20) + 1;
	}	

	printf("\nPrinting random values\n");

	for ( int j = 0; j < ARRAY_NUM;j++){
		printf("%d, ", numbers[j]);
	}	


	printf("\nSelect sorting it now \n");
	selectionsort(numbers, ARRAY_NUM);


        printf("\nPrinting soprted values\n");

        for ( int j = 0; j < ARRAY_NUM;j++){
                printf("%d, ",  numbers[j]);
        }
	printf("\n");
}
// quick sort s odabirom sortiranja
void quicksort_test(int *niz, int n) {
	int pi;
	if (n < c) {
		selectionsort(niz, n);
	}
	else {
		pi = partition(niz, n); // 4 + 3*n
		quicksort_test(niz, pi);
		quicksort_test(niz + pi + 1, n - pi - 1);
	}
}
Exemplo n.º 4
0
main()
{
      
          int a[10]={2,4,5,8,1,45,20,24,50,34};
     int i;
     
     selectionsort(a, 10);
     for (i=0;i<10; i++){
         printf("%d\t", a[i]);
         }
         system("pause");
         }                      
Exemplo n.º 5
0
int main(){
	srand(time(NULL)); //Damit rand() nicht immer gleiche Folgen ausgibt
	clock_t begin, end;
	int size;
	
	//Aufgabe 3b)
	//Sortieralgorithmen bereits nach ihrer Laufzeit sortiert.
	printf("Welche Laenge(int) soll das Array haben?\n>");
	scanf("%d", &size);
	printf("Eingegebene Arraylaenge: %d\nAuflistung der Laufzeiten:\n", size);
	
	double* arr = malloc(size * sizeof(double));
	double* arrCopy = malloc(size * sizeof(double));
	for(int i = 0; i < size; i++){
		arr[i] = randBet(-1000, 1000);
		arrCopy[i] = arr[i];
	}
	begin = clock();
	quicksort(arrCopy, size);
	end = clock();
	printf("quicksort: %d\n",end-begin);
	
	CopyFirst(arr, arrCopy, size);
	begin = clock();
	mergesort(arrCopy, size);
	end = clock();
	printf("mergesort: %d\n",end-begin);
	
	CopyFirst(arr, arrCopy, size);
	begin = clock();
	selectionsort(arrCopy, size);
	end = clock();
	printf("Selektionsort: %d\n",end-begin);
	
	CopyFirst(arr, arrCopy, size);
	begin = clock();
	insertionsort(arrCopy, size);
	end = clock();
	printf("Insertionsort: %d\n",end-begin);
	
	CopyFirst(arr, arrCopy, size);
	begin = clock();
	bubblesort(arrCopy, size);
	end = clock();
	printf("Bubblesort: %d\n",end-begin);

	free(arr);
	free(arrCopy);
	
	return 0;
}
Exemplo n.º 6
0
void main()
{
    printf("The array before sorting is :");
    int a[20]={0,0};
    int i;
    for(i=0;i<10;i++)
         a[i]=10-i;
    for(i=0;i<10;i++)
        printf("%d ",a[i]);
    selectionsort(a,10);
    printf("\nThe array after sorting is :");
    for(i=0;i<10;i++)
        printf("%d ",a[i]);
}
Exemplo n.º 7
0
int main(){
  int i, size = 10;
  int t[] = {12, 34, 23, 5, 265, 63, 456, 246, 10, 92};
  printf("Unsorted array:");
  for (i = 0; i < size; i++)
    printf("%d\t",t[i]);
  
  selectionsort (t, size-1);
  printf("\nAfter sorting:");
  for (i = 0; i < size; i++)
    printf("%d\t",t[i]);
  printf("\n");
  return 0;
}
Exemplo n.º 8
0
void		aff_opt_1(void)
{
      int	*tab;

      tab = create_tab(n);
      fill_tab(&tab);
      printf("\n%s - Tableau Initial :\n%s\t", COLOR, NONE);
      aff_tab(tab);
      printf("%s - Tri rapide :\n%s\t", COLOR, NONE);
      aff_tab(quicksort(tab, 0, n));
      printf("\n");
      free(tab);

      tab = create_tab(n);
      fill_tab(&tab);
      printf("%s - Tableau Initial :\n%s\t", COLOR, NONE);
      aff_tab(tab);
      printf("%s - Tri par fusion :\n%s\t", COLOR, NONE);
      aff_tab(fusionsort(tab, 0, n));
      printf("\n");
      free(tab);

      tab = create_tab(n);
      fill_tab(&tab);
      printf("%s - Tableau Initial :\n%s\t", COLOR, NONE);
      aff_tab(tab);
      printf("%s - Tri bulle :\n%s\t", COLOR, NONE);
      aff_tab(bubble_sort(tab));
      printf("\n");
      free(tab);

      tab = create_tab(n);
      fill_tab(&tab);
      printf("%s - Tableau Initial :\n%s\t", COLOR, NONE);
      aff_tab(tab);
      printf("%s - Tri selection :\n%s\t", COLOR, NONE);
      aff_tab(selectionsort(tab));
      printf("\n");
      free(tab);

      tab = create_tab(n);
      fill_tab(&tab);
      printf("%s - Tableau Initial :\n%s\t", COLOR, NONE);
      aff_tab(tab);
      printf("%s - Tri insertion :\n%s\t", COLOR, NONE);
      aff_tab(insertionsort(tab));
      printf("\n");
      free(tab);
}
Exemplo n.º 9
0
int main( int argc, char *argv[] ) {

  int *A, i = 0, count = 0, temp ;

  FILE *fin, *fun ; 

  fun = fopen( argv[ 1 ] , "r" ) ;
  fin = fopen( argv[ 1 ] , "r" ) ; 
 
  while( ( fscanf( fin , "%d", &temp ) ) != EOF ) {
 
   count++ ;
   
  }  

  fclose( fin ) ;

  A = ( int * ) malloc( sizeof( int ) * count ) ;

  while( ( fscanf( fun, "%d", &A[ i ] ) ) != EOF ) i++ ;
  
  selectionsort( A, count ) ;

  printf( "\nThe first 5 numbers are." ) ;

  for( i = 0 ; i < 5 ; i++ ) {

    printf( " %d", A[ i ] ) ;

  }

  printf( "\nThe last 5 numbers are." ) ;

  for( i = ( count - 5 ) ; i <= count  ; i++ ) {

    printf( " %d", A[ i ]  ) ;

  }
    
  fclose( fun ) ;

  return  0 ;

}
int main()
{

    srand(time(NULL));
    int count =25;
    for(int i=0;i<5;i++){
        for(int j=4;j!=-1;j--){
            array[i][j]=count--;
        }
    }
    printt();
    selectionsort();
    printf("after selection sort\n");
    printt();
    spiral();
    printf("after spiral\n");
    printtt();
    return 0;
}
Exemplo n.º 11
0
int main(int argc, char **argv){

	// An initially empty list
	struct Node *list = NULL;

	/* Go through argv array, prepeding each string
	and its length (up to 5 characters) to the linked list */

	// LOOP THROUGH ARGV
	int i;
	for (i=1; i < argc; i++){
		list = prepend(list,argv[i]);
	}

	/* Print the list */
	

	printf("prepended: ");

	print(list);

	selectionsort(list, compareData, swapData);

	printf("\n");
	printf("   sorted: ");

	print(list);

	reverse(&list);

	printf("\n");
	printf(" reversed: ");

	print(list);

	printf("\n");

	/* Deallocate all nodes */
	deallocate_list(list);

	return 0;

} //end of main
Exemplo n.º 12
0
int main()
{int i,k;
	struct node *head=NULL;
printf("How many no. u want to insert:");
		scanf("%d",&k);
		for(i=0;i<k;i++)
			{
					AddNode(&head);}
		printf("Before SELECTION sorting.........\n");
		display(&head);
printf("\n");
	printf("----------------------\n");
	
		selectionsort(&head);
			printf("----------------------\n");
	printf("\nAfter SELECTION sorting........\n");
	

	display(&head);
	return 0;
}  
Exemplo n.º 13
0
int main()
{
	int *array;
	int total_count;
	
	printf("\t\t-------SELECTION SORTING-------\n");
	printf("Enter the numbers of elements to be added to array:");
	scanf("%d", &total_count);

    input_array(&array, total_count);

	printf("\nElements before sorting are:\n");
    display_array(array,total_count);

    selectionsort(array, total_count);

	printf("Elements after sorting are:\n");
    display_array(array,total_count);

	return 0;
}
Exemplo n.º 14
0
int main()
{
    int *sortingarray;
    int i,a,b,c;
    srand(time(NULL)*time(NULL));
    printf("Enter size of array: valid values 1-35533\n");
    scanf("%d",&a);
    sortingarray=(int *) malloc(a*sizeof(int));
    a=a-1;
    arraygenerate(sortingarray,&a);
    i=0;
    printf("What sorting mechanism?\n Bubble/shaker -1\nSelection -2\nInsertion -3");
    scanf("%d",&c);
    if(c==1){
    bubblesort(sortingarray,&a);}
    if(c==2){selectionsort(sortingarray,&a);}
    if(c==3){insertionsort(sortingarray,&a);}
    while(i<=a){printf("%d\t",sortingarray[i]);i++;}
    nickname();
    return 0;
}
Exemplo n.º 15
0
void sort()
{
int z=1,choice,a;
 while(z==1) 
    { 
      printf("\n\n****************************************SORTING $ SEARCHING MODULE*****************************\n\n");       
 printf("Enter 1 for binary search\nEnter 2 for bubble sort\n Enter 3 for insertion sort\n Enter 4 for selection sort \n Enter 5 for quick sort:"); 
            scanf("%d",&choice); 
        
    
    switch(choice) 
    { 
        case 1: 
           a=binarysearch(); 
            break; 
            
        case 2: 
           bubblesort();  
            break; 
       case 3: 
            a=insertionsort();
            break;
          case 4: 
           selectionsort(); 
            break; 
           case 5: 
           quicksort(); 
            break; 
        default: 
            printf("\nERROR"); 
            
    } 
        
        
    printf("PRESS 1 for continue 0 for exit"); 
scanf("%d",&z); 
    } 


}
Exemplo n.º 16
0
int main(int argc, char *argv[]) {
	
	//Recupero il numero di elementi da generare
	int nmax;
	nmax = atoi(argv[1]);
	int intervallo = nmax;
	int v[nmax];
	int w[nmax];
	
	//genero il vettore v
	generate_vector(v, nmax, intervallo);
	
	printv(v, nmax);
	printf("Primo e ultimo numero di v: %i - %i\n",v[0],v[nmax-1]);
	
	printf("Ordino il vettore usando il selectionsort...");
	
	selectionsort(v,nmax);
	
	printf("Primo e ultimo numero di v: %i - %i\n",v[0],v[nmax-1]);
	printf("Il counter globale alla fine è: %i\n",globalcounter);
	printv(v, nmax);
}
Exemplo n.º 17
0
int 
main(int argc, char*argv[]){
	int A[MAX], i, num, position = 0;
	
	for(i = 0; i < MAX; i++) {
		A[i] = 0;
	}	
	i = 0;
	printf("Please enter up to 50 values: \n");
	while(scanf("%d",&num) != EOF) {
		A[i] = num;
		i ++;
		position ++;
	}
	selectionsort(A,position - 1);
	
	printf("The sorted array is:");
	for(i = 0; i < position; i ++) {
		printf("%d ", A[i]);
	}
	printf(" \n");

	return 0;
}
Exemplo n.º 18
0
#include<stdio.h>
#include<iostream.h>

void main()
{
     selectionsort()
 }
void Selectionsort()
{
      int i, j, first, temp;
      int numLength = num.length( );
      for (i= numLength - 1; i > 0; i--)
     {
           first = 0;                 // initialize to subscript of first element
           for (j=1; j<=i; j++)   // locate smallest between positions 1 and i.
          {
                 if (num[j] < num[first])
                 first = j;
          }
         temp = num[first];   // Swap smallest found with element in position i.
         num[first] = num[i];
         num[i] = temp;
     }
     return;
}
********************************************************************
  #include<iostream.h>

  void main(void)
  {
   int temp, i, j;