Esempio n. 1
0
int main()
{
    unsigned int len;
/*  when testing, directly assign 20000,40000,100000 to len. 
    printf() and scanf() will not be needed  */
    printf("Please input the length: ");
    scanf("%ld",&len);

    unsigned int array[len];
    unsigned int i;
/*  Generate a sorted array of distinct integers from 0 to len-1.*/
    for(i=0;i<len;i++)
    {
        array[i]=i;
        //printf("%ld ",array[i]);
    }
    printf("\n");

    Insertion_sort(array,len);

/*  Only for testing, not necessary. Not included when measuring time.  
    for(i=0;i<len;i++)
    {
        printf("%ld ",array[i]);
    }
    printf("\n");*/
 
    return 0;
}
int main(){

	int n,index=0;
	while (scanf("%d", &n) != EOF)
	{
		Insertion_sort(n, index);//index指向為未排序的
		printf("%d\n", index+1 & 1 ? num[index / 2] : (num[index / 2] + num[index / 2 + 1]) / 2);//取中間數
		index++;
	}

	return 0;
}
Esempio n. 3
0
/******************************************************************

   Description:
      Initialization routine for the CPU use module.

   Inputs:

   Outputs:

   Returns:
      Always returns 0 on success.  On failure, calls exit().

   Notes:
      Any number of failures cause CPU monitoring to fail.

******************************************************************/
int CPUUSE_initialize( ){

   int ret, index;

   /* Build the TAT directory. */
   if( (ret = CPUUSE_build_tat_directory()) < 0 ){

      fprintf( stderr, "Unable to build TAT directory\n" );
      exit(1);

   }

   /* Allocate array of Cpu_stats_t structures to hold CPU stats. */
   Prev_cpu_stats = (Cpu_stats_t *) calloc( (size_t) 1, 
                                    (size_t) Cpu_stats_size*sizeof(Cpu_stats_t) );
   if( Prev_cpu_stats == NULL ){

      fprintf( stderr, "calloc Failed for %d bytes\n",
               Cpu_stats_size*sizeof(Cpu_stats_t) );
      free( Dir );
      exit(1);

   }

   Curr_cpu_stats = (Cpu_stats_t *) calloc( (size_t) 1, 
                                    (size_t) Cpu_stats_size*sizeof(Cpu_stats_t) );
   if( Curr_cpu_stats == NULL ){

      fprintf( stderr, "calloc Failed for %d bytes\n",
               Cpu_stats_size*sizeof(Cpu_stats_t) );
      free( Prev_cpu_stats );
      free( Dir);
      exit(1);

   }

   /* Allocate Compare_index array. */
   Compare_index = (int *) calloc( (size_t) 1, (size_t)  Cpu_stats_size * sizeof(int) );
   if( Curr_cpu_stats == NULL ){

      fprintf( stderr, "calloc Failed for %d bytes\n",
               Cpu_stats_size*sizeof(int) );
      free( Prev_cpu_stats );
      free( Curr_cpu_stats );
      free( Dir);
      exit(1);

   }

   /* Sort the tat array ... needs to be sorted in increasing order of
      task name. */
   Insertion_sort( Dir, Cpu_stats_size );

   /* Process each tat entry and initialize the cpu stats arrays. */
   for( index = 0; index < Cpu_stats_size; index++ ) {

      strcpy( Curr_cpu_stats[index].task_name, Dir[index].task_name );
      strcpy( Prev_cpu_stats[index].task_name, Curr_cpu_stats[index].task_name );
      strcpy( Curr_cpu_stats[index].node_name, Node_name );
      strcpy( Prev_cpu_stats[index].node_name, Node_name );
      Prev_cpu_stats[index].stats.cpu = Curr_cpu_stats[index].stats.cpu = 0;
      Prev_cpu_stats[index].stats.pid = Curr_cpu_stats[index].stats.pid = -1;
      Prev_cpu_stats[index].stats.next = Curr_cpu_stats[index].stats.next = NULL;
      Compare_index[index] = -1;

   /* End of "for" loop. */
   } 

   /* Initialize the volume stats array. */
   for( index = 0; index < MAX_NUM_TASKS; index++ ){

      Vol_stats_t *task = NULL;
      int ind;

      task = (Vol_stats_t *) calloc( 1, sizeof(Vol_stats_t) );
      if( task == NULL ){

         fprintf( stderr, "calloc Failed for %d bytes\n", sizeof(Vol_stats_t) );
         for( ind = 0; ind < index; ind++ ){

            if( Vol_stats[ind] != NULL )
               free( Vol_stats[ind] );

         }

         exit(1);

      }

      Vol_stats[index] = task;       

      /* If this data structure represents a task, then .... */
      if( index < Cpu_stats_size ){

         int i;

         for( i = 0; i < MAX_ELEVATIONS; i++ )
            task->cpu[i] = -1;

         task->task_name[0] = 0;
         strcpy( task->task_name, Dir[index].task_name );

      }
      else{

         int i;

         if( index == Cpu_stats_size ){

            /* The third to last entry in "task" is for the total cpu. */
            strcpy( task->label.units, "ms" );
            task->task_name[0] = 0;
            strcpy( task->task_name, "Total CPU" );

         }
         else if( index == (Cpu_stats_size+1) ){

            /* The second to last entry in "elev" is for the scan duration. */
            strcpy( task->label.units, "ms" );
            task->task_name[0] = 0;
            strcpy( task->task_name, "Scan Duration" );

         }
         else if( index == (Cpu_stats_size+2) ){

            /* The last entry in "elev" is for the percent total cpu. */
            strcpy( task->label.units, "%" );
            task->task_name[0] = 0;
            strcpy( task->task_name, "% Total CPU" );

         }

         /* Initialize the CPU numbers. */
            for( i = 0; i < MAX_ELEVATIONS; i++ )
               task->cpu[i] = 0;

      }

   }

   Vol_stats_num_elevs = 0;
   Vol_stats_list_num_vols = 0;
 
   /* Do not need the tat anymore. */
   if( Dir != NULL ){

      free( Dir );
      Dir = NULL;

   }

   /* Return success. */
   return(0);

/* End of CPUUSE_initialize() */
}