Esempio n. 1
0
int main(void){
  Item a[10];
  
  rand_array(a);
  printf("before: ");
  print_array(a);
  selection_sort(a,0,9);
  printf("after: ");
  print_array(a);

  rand_array(a);
  printf("before: ");
  print_array(a);
  insertion_sort(a,0,9);
  printf("after: ");
  print_array(a);

  rand_array(a);
  printf("before: ");
  print_array(a);
  quicksort(a,0,9);
  printf("after: ");
  print_array(a);

}
Esempio n. 2
0
File: v1.c Progetto: Mohanx/cs325
void bench_2()
{

	int i, j;
	int trials = 5;
	int *array;
	long time_sum;

	printf("\nAlgorithm 2 Benchmarking Begin\n");

	for(i=100; i < 901; i += 100)
	{
		time_sum = 0;

		for(j=0; j < trials; ++j)
		{
			sleep(.2); //We wait one second to assure random seeds properly
			array = rand_array(i); //Create new array
		
			//Begin benchmarking
			timer_start();
			print_max_2(array, i);
			timer_stop();
			
			time_sum += timer_get_time();
		}
		
		time_sum = time_sum / trials; //Gets average
		printf("%d elements took an average %ld microsecs over %d trials\n",
				i, time_sum, trials);
	}


	for(i=1000; i < 9001; i +=1000)
	{	
		time_sum = 0;
		for(j=0; j < trials; ++j)
		{
			sleep(.2); //We wait one second to assure random seeds properly
			array = rand_array(i); //Create new array
	
			timer_start();
			print_max_2(array, i);
			timer_stop();
			
			time_sum += timer_get_time();
		}

		time_sum = time_sum / trials;
		printf("%d elements took an average %ld microsecs over %d trials\n", i, time_sum, trials);
	}
	

}
Esempio n. 3
0
TEST(Random, Array)
{
    int num = 100;
    int block[num];

    rand_array(block, num);

    bool test[num];
    memset(test, 0, sizeof(test));

    //  mark each idx in the block
    for (int i = 0; i < num; i++)
    {
        int idx = block[i];
        // each index is in the range 0 <= n < num
        EXPECT_TRUE(idx >= 0);
        EXPECT_TRUE(idx < num);
        // each idx must only appear once
        EXPECT_FALSE(test[idx]);
        test[idx] = true;
    }

    //  check that all the values are set
    for (int i = 0; i < num; i++)
    {
        EXPECT_TRUE(test[i]);
    }
}
Esempio n. 4
0
DEF_TEST(Sort, reporter) {
    /** An array of random numbers to be sorted. */
    int randomArray[500];
    /** The reference sort of the random numbers. */
    int sortedArray[SK_ARRAY_COUNT(randomArray)];
    /** The random numbers are copied into this array, sorted by an SkSort,
        then this array is compared against the reference sort. */
    int workingArray[SK_ARRAY_COUNT(randomArray)];
    SkRandom    rand;

    for (int i = 0; i < 10000; i++) {
        int count = rand.nextRangeU(1, SK_ARRAY_COUNT(randomArray));
        rand_array(rand, randomArray, count);

        // Use qsort as the reference sort.
        memcpy(sortedArray, randomArray, sizeof(randomArray));
        qsort(sortedArray, count, sizeof(sortedArray[0]), compare_int);

        memcpy(workingArray, randomArray, sizeof(randomArray));
        SkTHeapSort<int>(workingArray, count);
        check_sort(reporter, "Heap", workingArray, sortedArray, count);

        memcpy(workingArray, randomArray, sizeof(randomArray));
        SkTQSort<int>(workingArray, workingArray + count - 1);
        check_sort(reporter, "Quick", workingArray, sortedArray, count);
    }
}
Esempio n. 5
0
void dns_save_send(DNS_HANDLE pDcb, PCHAR pDomainName, USHORT sAddr, UCHAR iBankOffset, UCHAR iType)
{
	PCHAR pBuf;
	BOOLEAN bDns1Failed, bDns2Failed;

	bDns1Failed = IsValidIP(Sys_pDnsIp) ? FALSE : TRUE;
	bDns2Failed = IsValidIP(Sys_pDnsIp2) ? FALSE : TRUE;
	if (bDns1Failed && bDns2Failed)
	{
		dns_failed(sAddr, iBankOffset);
		return;
	}

	pBuf = heap_save_str(pDomainName);
	if (pBuf)
	{
		pDcb->iBankOffset = iBankOffset;
		pDcb->sCallBack = sAddr;
		pDcb->iState = DS_PENDING;
		rand_array(pDcb->pID, DNS_ID_LEN);
		pDcb->iType = iType;
		pDcb->pName = pBuf;
		pDcb->bDns1Failed = bDns1Failed;
		pDcb->bDns2Failed = bDns2Failed;
		dns_send(pDcb);
	}
	else
	{
		// dns module resource limited, try later
		dns_failed(sAddr, iBankOffset);
	}
}
Esempio n. 6
0
void GenNonce(PCHAR pNonce)
{
	UCHAR pVal[16];

	rand_array(pVal, 16);
	char2asc_str(pNonce, pVal, 16, FALSE);
}
Esempio n. 7
0
File: v1.c Progetto: Mohanx/cs325
void bench_1()
{

	int i, j;
	int trials = 5;
	int *array;
	long time_sum;

	printf("\nAlgorithm 1 Benchmarking Begin\n");
	
	for(i=100; i < 901; i += 100)
	{
		time_sum = 0;

		for(j=0; j < trials; ++j)
		{
			sleep(.2); //We wait one second to assure random seeds properly
			array = rand_array(i); //Create new array
		
			//Begin benchmarking
			timer_start();
			algo_1(array, i);
			timer_stop();
			
			time_sum += timer_get_time();
		}
		
		time_sum = time_sum / trials; //Gets average
		printf("%d elements took an average %ld microsecs over %d trials\n",
				i, time_sum, trials);
	}

	// We don't need 1000-9000 cases here because that would take forever	

}
Esempio n. 8
0
int main()
{
    int N[10] = {1, 5, 4, 3, 2, 10, 7, 6, 8, 9};

    rand_array(N);
    print_array(N, 0);
    
    bubble_sort(N, 10);
    print_array(N, 0);
}
Esempio n. 9
0
void DhcpInit()
{
	rand_array(_pDhcpXID, DHCP_XID_LEN);
	_iDhcpState = DHCP_STATE_INIT;
	_iDhcpRetry = 0;
	_lLeaseTime = 0;
	_lCurrent = 0;

	DhcpOpenUdp();
}
Esempio n. 10
0
int main(int argc, const char *argv[]) {
    if (argc != 2) {
        printf("Usage: %s <Arraysize>\n", argv[0]);
        return -1;
    }
    int size = atoi(argv[1]);
    int *array = rand_array(size);

    insert_sort(array, size);

    free(array);
    return 0;
}
Esempio n. 11
0
int main(int argc, char *argv[])
{
    int len = 5;

    // seed the random number generator
    srand(time(NULL));

    int* arrayA = rand_array(len);
    print_int_array(arrayA, len);
    int* arrayB = rand_array(len);
    print_int_array(arrayB, len);

    complex_number* complexArray = initialize_complex_array(arrayA, arrayB, len);
    float* magnitudeArray = calculate_magnitudes(complexArray, len);
    print_float_array(magnitudeArray, len);

    // free memory
    free(arrayA);
    free(arrayB);
    free(complexArray);
    free(magnitudeArray);
}
Esempio n. 12
0
int main(void) {
  scheduler_begin();

  struct array * A = rand_array(1000000);
  seq_threshold = 100;

  printf("before sort: %s\n", check_sort(A));
  par_mergesort(A);
  printf("after sort: %s\n", check_sort(A));

  scheduler_end();
  return 0;
}
Esempio n. 13
0
TEST(List, Find)
{
    int idx;
    Item *head = 0;

    Item *item = 0;
    int num = 10;
    Item items[num];

    //  Initialise each item
    for (int i = 0; i < num; i++)
    {
        init_item(& items[i], i);
    }

    //  create a randomised table of indexes
    int block[num];
    rand_array(block, num);

    // add our items randomly to sorted list
    for (int i = 0; i < num; i++)
    {
        int idx = block[i];
        item = & items[idx];
        item_add_sorted(& head, item, 0);
    }

    //  find first item
    idx = 0;
    item = item_find(& head, & items[idx], 0);
    EXPECT_EQ(item, & items[idx]);

    //  find last item
    idx = num-1;
    item = item_find(& head, & items[idx], 0);
    EXPECT_EQ(item, & items[idx]);

    //  find middle item
    idx = num / 2;
    item = item_find(& head, & items[idx], 0);
    EXPECT_EQ(item, & items[idx]);

    //  try to find non-existent item
    Item wrong;
    init_item(& wrong, -5);

    item = item_find(& head, & wrong, 0);
    EXPECT_EQ(0, item);
}
Esempio n. 14
0
TEST(List, Sorted)
{
    int size;
    Item *head = 0;

    Item *item = 0;
    int num = 100;
    Item items[num];

    //  Initialise each item
    for (int i = 0; i < num; i++)
    {
        init_item(& items[i], i);
    }

    int block[num];
    rand_array(block, num);

    // add our items randomly to sorted list
    for (int i = 0; i < num; i++)
    {
        int idx = block[i];
        item = & items[idx];
        item_add_sorted(& head, item, 0);
    }

    //  Pop off stack : it should be in order
    for (int i = 0; i < num; i++)
    {
        item = item_pop(& head);
        EXPECT_TRUE(item);
        // check it is the correct item
        EXPECT_EQ(& items[i], item);

        // check the item is correct
        item_validate(item, i);
    }

    EXPECT_EQ(0, head);

    item = item_pop(& head);
    EXPECT_EQ(0, item);

    size = item_size(& head);
    EXPECT_EQ(0, size);
}
Esempio n. 15
0
int main()
{
	int test2[] = {22, -27, 38, -34, 49, 40, 13, -44, -13, 28, 46, 7, -26, 42, 29, 0, -6, 35, 23, -37, 10, 12, -2, 18, -12, -49, -10, 37, -5, 17, 6, -11, -22, -17, -50, -40, 44, 14, -41, 19, -15, 45, -23, 48, -1, -39, -46, 15, 3, -32, -29, -48, -19, 27, -33, -8, 11, 21, -43, 24, 5, 34, -36, -9, 16, -31, -7, -24, -47, -14, -16, -18, 39, -30, 33, -45, -38, 41, -3, 4, -25, 20, -35, 32, 26, 47, 2, -4, 8, 9, 31, -28, 36, 1, -21, 30, 43, 25, -20, -42};
	
	int i, j;
	int trials = 5;
	int *array;
	long time_sum;

	for(i=100; i < 901; i += 100)
	{
		time_sum = 0;

		for(j=0; j < trials; ++j)
		{
			sleep(1); //We wait one second to assure random seed
			array = rand_array(i); //Create new array
		
			//Begin benchmarking
			timer_start();
			algo_1(array, i);
			timer_stop();
			
			time_sum += timer_get_time();
		}
		
		time_sum = time_sum / trials;
		printf("%d elements took an average %ld millisecs\n",
				i, time_sum);
		
	}



	/* We were told we could skip this chunk of testing for this algorithm	
	for(i=1000; i < 9001; i +=1000)
	{
		timer_start();
		algo_1(rand_array(i), i);
		timer_stop();
		printf("%d elements took %ld millisecs\n", i, timer_get_time());
	}
	*/

	return 0;
}
Esempio n. 16
0
void * thrash(void *arg)
{
    ASSERT(arg);
    ThreadTest *tt = (ThreadTest*) arg;
    Item **head = & tt->head;
    Mutex *mutex = & tt->mutex;

    Item *item = 0;
    int num = 100;
    Item items[num];

    //  Initialise each item
    for (int i = 0; i < num; i++)
    {
        init_item(& items[i], i);
    }

    //  create a randomised table of indexes
    int block[num];
    rand_array(block, num);

    // add our items randomly to sorted list
    for (int i = 0; i < num; i++)
    {
        int idx = block[i];
        item = & items[idx];
        item_add_sorted(head, item, mutex);
    }

    //  Remove the items
    for (int i = 0; i < num; i++)
    {
        item = & items[i];
        bool found = item_remove(head, item, mutex);
        EXPECT_TRUE(found);
        // check it is the correct item
        EXPECT_EQ(& items[i], item);

        // check the item is correct
        item_validate(item, i);
    }

    return 0;
}
Esempio n. 17
0
/** 图片分块内像素随机 **/
Mat rand_block(Mat &input){
    Mat output = input.clone();
    //    cvtColor(input, output, CV_BGR2GRAY);
    int n = output.cols*output.rows;
    int data[n];
    //    int i = n;
    //    while ((data[--i] = i)) ;
    for (int i = 0; i < n; i++) data[i] = i;
    rand_array(data, n);
    for (int y = 0, data_trace = 0; y < output.rows; y++) {
        for (int x = 0; x < output.cols; x++, data_trace++) {
            int x_ = data[data_trace]%output.cols;
            int y_ = data[data_trace]/output.cols;
            swap(output.at<Vec3b>(y, x), output.at<Vec3b>(y_, x_));
        }
    }
    //    cvtColor(output, output, CV_GRAY2BGR);
    return output;
}
Esempio n. 18
0
File: math.c Progetto: hankem/ISIS
static void grand_array (SLindex_Type *num) /*{{{*/
{
   /* Gaussian random numbers */
   rand_array (*num, grand);
}
Esempio n. 19
0
File: math.c Progetto: hankem/ISIS
static void urand_array (SLindex_Type *num) /*{{{*/
{
   /* Uniform random numbers */
   rand_array (*num, urand);
}
Esempio n. 20
0
int main(int argc, char* argv[]) {
  int size, num_threads;
  int i, j;

  double *array;

  int buckets_count, bucket_id;
  double bucket_range, bucket_step;
  double *bucket_boundaries;

  Bucket **buckets;
  Bucket *my_buckets, *results;

  if (argc != 3) {
    fprintf(stderr, "Usage: ./bsort size num_threads\n");
    exit(1);
  }

  srand((unsigned) time(NULL));

  size = atoi(argv[1]);
  num_threads = atoi(argv[2]);

  array = rand_array(size);

  #pragma omp parallel num_threads(num_threads) default(shared) private(i, j, my_buckets, bucket_id)
  {
    #pragma omp single
    {
      // number of buckets = number of available threads
      buckets_count = omp_get_num_threads();

      // calculate range of values
      bucket_range = MAX - MIN;

      // bucket step = bucket size
      bucket_step = bucket_range / buckets_count;

      // calculate bucket boundaries
      bucket_boundaries = (double *) malloc(sizeof(double) * (buckets_count - 1));

      // [0,     0.125)
      // [0.125, 0.250)
      // [0.250, 0.375)
      // ...
      // [0.750, 0.875)
      // [0.875, 1.0]

      for (i = 0; i < buckets_count - 1; i++) {
        bucket_boundaries[i] = MIN + (i + 1) * bucket_step;
      }

      // allocate array of bucket's arrays
      // each thread gets own array of buckets
      buckets = (Bucket **) malloc(sizeof(Bucket *) * buckets_count);
    }

    // allocate array of buckets for each thread
    #pragma omp for schedule(static)
    for (i = 0; i < buckets_count; i++) {
      buckets[i] = (Bucket *) malloc(sizeof(Bucket) * buckets_count);

      my_buckets = buckets[i];

      for (j = 0; j < buckets_count; j++)  {
        my_buckets[j].values = (double *) malloc(sizeof(double) * size);
        my_buckets[j].count = 0;
      }
    }

    // distribute elements into buckets
    #pragma omp for schedule(static)
    for (i = 0; i < size; i++) {
      bucket_id = 0;
      j = 0;

      // calculate bucket for current element
      while (j < buckets_count - 1 && array[i] >= bucket_boundaries[j++]) {
        bucket_id++;
      }

      // add element to bucket
      my_buckets[bucket_id].values[my_buckets[bucket_id].count] = array[i];
      my_buckets[bucket_id].count++;
    }

    #pragma omp single
    {
      results = (Bucket *) malloc(sizeof(Bucket) * buckets_count);
    }

    // merge buckets
    // 1-st thread merges all 1-st buckets
    // 2-nd thread merges all 2-nd buckets
    // ...
    #pragma omp for schedule(static)
    for (i = 0; i < buckets_count; i++) {
      results[i].values = (double *) malloc(sizeof(double) * size);
      results[i].count = 0;

      for (j = 0; j < buckets_count; j++) {
        memcpy(
          &results[i].values[results[i].count],
          buckets[j][i].values,
          buckets[j][i].count * sizeof(double)
        );

        results[i].count += buckets[j][i].count;
      }
      free(buckets[j]);

      // sort merged bucket
      qsort(results[i].values, results[i].count, sizeof(double), &doubles_comparator);
    }

    // collect sorted buckets
    #pragma omp single
    {
      free(buckets);

      j = 0;
      for (i = 0; i < buckets_count; i++) {
        memcpy(
          &array[j],
          results[i].values,
          results[i].count * sizeof(double)
        );

        j += results[i].count;
      }
      free(results);
    }
  }

  // print_array(array, size);

  return 0;
}
Esempio n. 21
0
int main(int argc, char *argv[]) {

    int i = 0, key_len = 0, icon_len = 0, err = 0, option, retval = 0;
    char *sz = NULL;
    char *script = NULL;
    char *script_e = NULL;
    char *src = NULL;
    char *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL;
    char *key = NULL, *icon = NULL, *key_e = NULL;

	while ((option = getopt(argc, argv, "k:i:")) != -1) {
		switch (option) {
			case 'k':
				key_e = optarg;
				break;
			case 'i':
				icon = optarg;
				break;
			case '?':
				err = 1;
				if (optopt == 'k' || optopt == 'i') {
					printf("Option -%c requires an argument\n", optopt);
				} else {
					printf("Option -%c unrecognized\n", optopt);
				}
				break;
		}
	}

	if (err || argc > 6 || optind >= argc) {
		printf("%s", USAGE);
		return -1;
	}

	if (key == NULL) { // Use default key, recommended, since it's random + variable length
        srand( time(NULL) );
        key_len = (unsigned int)rand() % 
            ( ( (DEFAULT_KEY_LEN * 3) - DEFAULT_KEY_LEN ) + 1 ) + DEFAULT_KEY_LEN;
        key = rand_array(32, 254, key_len);
    } else { // User specified key
        key = calloc(strlen(key_e)+1, sizeof(char)); 
        strncpy(key, key_e, strlen(key_e));
        key_len = strlen(key_e)+1;

	}

    // Load up the script file
    i = load_script(argv[optind++], &script);
	if (i == -1) {
		goto done;
	}

    // Get the script array size in a string formatted number
    sz = calloc( 10, sizeof(char) );
	if (sz == NULL) {
		goto done;
	}
    sprintf(sz, "%d", i);
    
    // Encrypt the script with the given key
    script_e = xor_enc(script, i+1, key, key_len-1);
	if (script_e == NULL) {
		goto done;
	}

    tmp = src_hex_array(script_e, i);
	if (tmp == NULL) {
		goto done;
	}

    // Create code from template
    tmp2 = replace(C_TEMPLATE, "___BASH_SCR___", tmp);
    if (cats(&tmp, NULL) == -1) {
		goto done;
	}

    tmp3 = src_hex_array(key, key_len);
	if (tmp3 == NULL) {
		goto done;
	}

    tmp = replace(tmp2, "___KEY___", tmp3);
    cats(&tmp2, NULL);
    cats(&tmp3, NULL);
	if (tmp == NULL) {
		goto done;
	}

    tmp2 = replace(tmp, "___BASH_SCR_SIZE___", sz);
    cats(&tmp, NULL);
	if (tmp2 == NULL) {
		goto done;
	}

    sprintf(sz, "%d", key_len);
    src = replace(tmp2, "___KEY_LEN___", sz);
    cats(&tmp2, NULL);
	if (src == NULL) {
		goto done;
	}


    // Write the .c source file
    if (cats(&tmp, argv[optind]) == -1) {
		goto done;
	}
    if (cats(&tmp, ".c") == -1) {
		goto done;
	}
	
    retval = write_file(tmp, src);
    cats(&tmp, NULL);
	if (retval == -1) {
		goto done;
	}
    
    // Create and run our 'make' batch script
	tmp = replace(MAKE_APP, "___APPNAME___", argv[optind]);
	if (tmp == NULL) {
		goto done;
	}
    i = system(tmp);
	cats(&tmp, NULL);
	
	// Add the icon if necessary
	if (icon != NULL) {
		tmp = replace(MAKE_ICON, "___ICONFILE___", icon);
		if (tmp == NULL) {
			goto done;
		}
		tmp2 = replace(tmp, "___APPNAME___", argv[optind]);
		if (tmp2 == NULL) {
			goto done;
		}
	}
	i = system(tmp2);
	cats(&tmp2, NULL);

	// Delete .c source file
    if (cats(&tmp2, argv[optind]) == -1) {
		goto done;
	}
    if (cats(&tmp2, ".c") == -1) {
		goto done;
	}
	remove(tmp2);

done:
	if (script != NULL) {
		cats(&script, NULL);
	}
	if (sz != NULL) {
		cats(&sz, NULL);
	}
	if (script_e != NULL) {
		cats(&script_e, NULL);
	}
	if (key != NULL) {
		cats(&key, NULL);
	}
	if (tmp != NULL) {
		cats(&tmp, NULL);
	}
	if (tmp2 != NULL) {
		cats(&tmp2, NULL);
	}
	if (src != NULL) {
		cats(&src, NULL);
	}
    return 0;
}
Esempio n. 22
0
/* Main program. */
int main(void)
{
    int i, j, n_samples, max_n, step_n;
    int array_size;
    int radix;
    test_item_t a[N_ITEMS], test_array[N_ITEMS];
    test_item_t *timing_array, *copy_array;
    timing_t *t;
    
    /* Assign random values to the key of each array element. */
    rand_array(a, N_ITEMS, 100);

    /* Now test quicksort(). */
    memcpy(test_array, a, sizeof(test_array));
    printf("array before quicksort\n = ");
    print_array(test_array, N_ITEMS);
    printf("\n\n");
    quicksort(test_array, N_ITEMS, sizeof(test_item_t), item_cmp);
    printf("array after quicksort\n = ");
    print_array(test_array, N_ITEMS);
    printf("\n\n");

    /* Now test mergesort0(). */
    memcpy(test_array, a, sizeof(test_array));
    printf("array before mergesort0\n = ");
    print_array(test_array, N_ITEMS);
    printf("\n\n");
    mergesort0(test_array, N_ITEMS, sizeof(test_item_t), item_cmp);
    printf("array after mergesort0\n = ");
    print_array(test_array, N_ITEMS);
    printf("\n\n");

    /* Now test mergesort(). */
    memcpy(test_array, a, sizeof(test_array));
    printf("array before mergesort\n = ");
    print_array(test_array, N_ITEMS);
    printf("\n\n");
    mergesort(test_array, N_ITEMS, sizeof(test_item_t), item_cmp);
    printf("array after mergesort\n = ");
    print_array(test_array, N_ITEMS);
    printf("\n\n");
    
    /* Now test radix sort. */
    memcpy(test_array, a, sizeof(test_array));
    printf("array before radixsort\n = ");
    print_array(test_array, N_ITEMS);
    printf("\n\n");
    radixsort(test_array, N_ITEMS, sizeof(test_item_t), get_value, 10);
    printf("array after radixsort\n = ");
    print_array(test_array, N_ITEMS);
    printf("\n\n");

    /* Now test heapsort. */
    memcpy(test_array, a, sizeof(test_array));
    printf("array before heapsort\n = ");
    print_array(test_array, N_ITEMS);
    printf("\n\n");
    heapsort(test_array, N_ITEMS, sizeof(test_item_t), item_cmp);
    printf("array after heapsort\n = ");
    print_array(test_array, N_ITEMS);
    printf("\n\n");
    
    /* Time the quicksort and mergesort sorting functions. */

    printf("Enter the number of samples to use: ");
    scanf("%d", &n_samples);
    printf("Enter the maximum array length to sort: ");
    scanf("%d", &max_n);
    printf("Enter the step size for array lengths: ");
    scanf("%d", &step_n);

    t = timing_alloc(5);  /* Five different sorting algorithms. */
    
    printf("\nResults (n, qsort, quicksort, mergesort, mergesort0, heapsort) (msec)\n"
	  );
    for(i = step_n; i <= max_n; i += step_n) {
	array_size = i * sizeof(test_item_t);
        timing_array = malloc(array_size);
	copy_array = malloc(array_size);
	rand_array(copy_array, i, MAX_VALUE);

        timing_reset(t);
	
	for(j = 0; j < n_samples; j++) {
	    memcpy(timing_array, copy_array, array_size);
            timing_start();
	    qsort(timing_array, i, sizeof(test_item_t), item_cmp);
            timing_stop(t,0);

	    memcpy(timing_array, copy_array, array_size);
            timing_start();
	    quicksort(timing_array, i, sizeof(test_item_t), item_cmp);
            timing_stop(t,1);

	    memcpy(timing_array, copy_array, array_size);
            timing_start();
	    mergesort(timing_array, i, sizeof(test_item_t), item_cmp);
            timing_stop(t,2);

	    memcpy(timing_array, copy_array, array_size);
            timing_start();
	    mergesort0(timing_array, i, sizeof(test_item_t), item_cmp);
            timing_stop(t,3);

	    memcpy(timing_array, copy_array, array_size);
            timing_start();
	    heapsort(timing_array, i, sizeof(test_item_t), item_cmp);
            timing_stop(t,4);
	}
	printf("%d", i);
	timing_print(t,"\t%.2f",n_samples);
	putchar('\n');

	free(timing_array);
	free(copy_array);
    }
    timing_free(t);

    /* Time radix sort on the largest array, using different radix sizes. */
    printf("\nRadix Sort Results.  Using n = %d\n", max_n);
    printf("(radix, time)\n");
    array_size = max_n * sizeof(test_item_t);
    timing_array = malloc(array_size);
    copy_array = malloc(array_size);
    rand_array(copy_array, max_n, MAX_VALUE);
    for(radix = 2; radix <= max_n; radix <<= 1) {

        timer_reset();
	
	for(j = 0; j < n_samples; j++) {
	    memcpy(timing_array, copy_array, array_size);
            timer_start();
	    radixsort(timing_array, max_n, sizeof(test_item_t), get_value,
		      radix);
            timer_stop();
	}
	
	printf("%d", radix);
	timer_print("\t%.2f", n_samples);
	putchar('\n');
    }
    free(timing_array);
    free(copy_array);
	
    return 0;
}
Esempio n. 23
0
int main(int argc, char *argv[])
{
	int length = Max-1, ret, input = 1, method = 1;
	double start, end;

	if(argc != 3)
	{
		usage();
		return 0;
	}
	else 
	{
		if(strcmp(argv[1], "-i"))
			input = 1;
		else
			input = 0;
			
		if(!strcmp(argv[2], "-1"))
			method = 1;
		else if(!strcmp(argv[2], "-2"))
			method = 2;
		else if(!strcmp(argv[2], "-3"))
			method = 3;
		else if(!strcmp(argv[2], "-4"))
			method = 4;
		else if(!strcmp(argv[2], "-5"))
			method = 5;
		else if(!strcmp(argv[2], "-6"))
			method = 6;
		else if(!strcmp(argv[2], "-7"))
			method = 7;
		else
			method = 1;	
	}

	if(input)
		rand_array(my_array, length);
	else
		input_array(my_array, &length);

	start = (double) nowTime;

	switch(method){
	case 1:
		printf("Insert sort:\n");
		insert_sort(my_array, length);	
		break;
	case 2:
		printf("bubble sort:\n");
		bubble_sort(my_array, length);
		break;
	case 3:
		printf("merge sort:\n");
		merge_sort(my_array, 0, length-1);
		break;
	case 4:
		printf("quick sort:\n");
		quick_sort(my_array, 0, length-1);
		break;
	case 5:
		printf("shell sort:\n");
		shell_sort(my_array, length);
		break;
	case 6:
		printf("heap sort:\n");
		heap_sort(my_array, length);
		break;
	case 7:
		printf("radix sort:\n");
		radix_sort(my_array, length);
		break;
	default:
		printf("quick sort:\n");
		quick_sort(my_array, 0, length-1);
	}

	end = (double) nowTime;
	printf("Sort Method spens %lfms\n", end - start);

	print_array(my_array, length);

	return 0;
}