示例#1
0
int main() {
    
  u_i len = 12;
  int arr[12] = {2, 13, 12, 16, 15, 4, 17, 8, 1, 18, 14, 9};
  int *temp1 = copy(arr, len);
  insertion_sort(temp1, len);
  int *temp2 = copy(arr, len);
  quick_sort(temp2, len);
  int *temp3 = copy(arr, len);
  merge_sort(temp3, len);
  int *temp4 = copy(arr, len);
  selection_sort(temp4, len);
  int *temp5 = copy(arr, len);
  counting_sort(temp5, len);
  int *temp6 = copy(arr, len);
  heap_sort(temp6, len);
  int *temp7 = copy(arr, len);
  radix_sort(temp7, len);
  
  bool eq = equal(temp1, temp2, len) &
            equal(temp2, temp3, len) &
            equal(temp3, temp4, len) &
            equal(temp4, temp5, len) &
            equal(temp5, temp6, len) &
            equal(temp6, temp7, len);
            
  printf("\x1B[32m""Equal arrays :%s\n""\033[0m", eq ? "true" : "false");
  
  // Free all arrays allocated.
}
示例#2
0
int main()
{
    int i;
    __int64 x,y,s,max;
    scanf("%d",&n);
    max=-1;
    for(i=1; i<=n; i++)
    {
        scanf("%d",&b[i]);
        if(b[i]>max)
            max=b[i];
    }
    counting_sort(max);
    s=0;
    open1=1;
    closed1=n;
    open2=1;
    closed2=0;
    s=0;
    for(i=1; i<n; i++)
    {
        x=getmin();
        y=getmin();
        s+=x+y;
        closed2++;
        b[closed2]=x+y;
    }
    printf("%I64d\n",s);
    return 0;
}
示例#3
0
radix_sort(a, k)
{
	for (0 -> k - 1)
	{
		counting_sort(a, 10);
	}
}
//recursive call, first we count sort on the leftmost column,
//then we proceed to recursively sort the next column
//aux is passed so we dont reallocate every time the helper array
void msd_radix_sort(const std::vector<std::string> & strings, std::vector<int> &v, std::vector<int> &aux, int start, int end, int chrindx)
{
    out("msd_radix_sort %d, %d, %d\n", start, end, chrindx);
    if(end - start < 2) return;
    if(end - start < 24) {
        insertion_sort(strings, v, aux, start, end, chrindx);
        return;
    }

    for(int i = start; i < end; i++) {
        out("h %s\n", strings[v[i]].c_str());
    }

    int counts[R + 1] = {0};
    counting_sort(strings, v, aux, start, end, chrindx, counts);

    for(int r = 0; r < R; r++) {
        if(counts[r+1] - counts[r] > 1) {
            msd_radix_sort(strings, v, aux, start + counts[r], 
                    start + counts[r + 1], chrindx + 1);
        }
    }

    for(int i = start; i < end; i++) {
        out("s %s\n", strings[v[i]].c_str());
    }
}
示例#5
0
void test(void)
{
    int A[NARRAY], B[NARRAY], k;
    int nsuccess, ntests;

    printf("Testing counting sort: ");

    nsuccess = ntests = 0;
    k = 10;
    for (int count = 0; count < NTESTS; count++) {
        /* construct an array for testing */
        for (int i = 0; i < NARRAY; i++)
            A[i] = rand() % k;

        /* test */
        counting_sort(A, B, NARRAY, k);
        ntests++;

        /* verification */
        qsort(A, NARRAY, sizeof(int), cmp);
        for (int i = 0; i < NARRAY; i++) {
            if (B[i] != A[i])
                continue;
        }
        nsuccess++;
    }

    printf("%d/%d, %2.f%%\n", nsuccess, ntests,
            (double) nsuccess/ntests * 100);
}
示例#6
0
int main(int argc, char* argv[]){
	if(argv[1]==NULL || argv[2]==NULL){
		printf("ERROR : Insert the arguments\ne.g)sorting_exe Q sample.txt\n");
		return 0;
	}

	fi = fopen(argv[2],"r");

	int arr[1000000];
	int* tarr;
	int num;
	int i=0;
	char sort_type;
	struct timespec start,end;
	sort_type = *argv[1];

	while(!feof(fi)){
		fscanf(fi,"%d ",&num);
		arr[i++]=num;
	}
	int length = i;
	int tlength;
	switch(sort_type){
		case 'Q':
			clock_gettime(CLOCK_REALTIME,&start);	
			quick_sort(arr,0,length-1);
			clock_gettime(CLOCK_REALTIME,&end);
			break;
		case 'M':
			tarr=(int*)malloc(sizeof(int)*length);
			for(i=0;i<length;i++){
				tarr[i]=arr[i];
			}
			clock_gettime(CLOCK_REALTIME,&start);	
			merge_sort(arr,tarr,0,length-1);
			clock_gettime(CLOCK_REALTIME,&end);
			break;
		case 'C':
			tlength = maximum(arr,length)+1;
			tarr = (int*)malloc(sizeof(int)*tlength);
			clock_gettime(CLOCK_REALTIME,&start);	
			counting_sort(arr,tarr,length,tlength);
			clock_gettime(CLOCK_REALTIME,&end);
			break;
		case 'I':
			clock_gettime(CLOCK_REALTIME,&start);	
			insert_sort(arr,length);
			clock_gettime(CLOCK_REALTIME,&end);
			break;
		default:
			printf("ERROR : Input appropriate arguments\nyou should input Q,M,I and C\n");
			return 0;
			break;
	}
	print_arr(arr,length);
	
	printf("\n");
	convert2second(&start,&end);
	return 0;
}
示例#7
0
int
main(int argc, char const *argv[])
{
    int* numbers = NULL;
    int length = 0;
    int i = 0;
    int max_number = 0;

    int counting_sort(int *numbers, int length, int max_number);

    while(EOF != scanf("%d", &length))
    {
        scanf("%d", &max_number);
        if (NULL == (numbers = (int*)malloc(length * sizeof(int))))
        {
            return 0;
        }

        for(i = 0; i < length; i++)
            scanf("%d", &numbers[i]);

        counting_sort(numbers, length, max_number + 1);

        printf("%d", numbers[0]);
        for(i = 1; i < length; i++)
            printf(" <= %d", numbers[i]);
        printf("\n");

        free(numbers);
    }

    return 0;
}
示例#8
0
int radixsort(int *a,int *b,int d,int size)// d is the number of digits in all the elements
// a is the actual array and b is an empty array to hold the final sorted list
{
    int i;
    for(i=1;i<=d;i++)
    counting_sort(a,b,9,size,i);
}
示例#9
0
void print_list()
  {
  int i;
  TITEM *t;

  if (!pagelen) print_head();
  if (!grp_sort)
     {
     for(i=0,t=itlist;i<item_count;i++,t++)
        {
        endpage(i);
        print_polozka(i,t);
        }
     }
  else
     {
     TITEM *list;
     int *nums;
     list=(TITEM *)malloc(sizeof(TITEM)*item_count);
     nums=(int *)malloc(sizeof(int)*item_count);
     counting_sort(itlist,list,item_count,nums);
     for(i=0,t=list;i<item_count;i++,t++)
        {
        endpage(i);
        print_polozka(nums[i],t);
        }
     free(list);
     free(nums);
     }
  }
示例#10
0
void main(){

	int A[length] = {6 , 0 , 2 , 0 , 1 , 3 , 4 , 6 , 1 , 3 , 2} , B[length];
	printArray(A , length);
	counting_sort(A , B , max(A , length) , length);
	printArray(B , length);

}
示例#11
0
int main(int argc, const char *argv[])
{
	FILE *f;
	off_t filesize;
	struct stat sb;
	clock_t start, end;
	int bufn = 100;
	int range = -1;
	int *counter;
	int rc;

	if (argc < 2) {
		fprintf(stderr, "%s <filename> [bufn] [range]\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	f = fopen(argv[1], "rb");
	if (f == NULL) {
		fprintf(stderr, "Failed to open file %s\n", argv[1]);
		return EXIT_FAILURE;
	}

	if (argc > 2) {
		bufn = strtol(argv[2], NULL, 0);
		if (argc == 4)
			range = strtol(argv[3], NULL, 0);
	}

	if (stat(argv[1], &sb)) {
		perror("stat");
		exit(EXIT_FAILURE);
	}

	filesize = sb.st_size;

	if (range < 0) {
		range = find_range(f, bufn);
		if (range < 0) {
			fprintf(stderr, "Failed to get range\n");
			exit(EXIT_FAILURE);
		}
	}

	start = clock();
	rc = counting_sort(f, range, bufn, &counter);
	end = clock();

	if (rc) {
		fprintf(stderr, "Failed to sort  %s\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	print_counter(counter, range);
	
	fprintf(stderr, "%ld bytes sorted in %f seconds\n", filesize, (double)(end - start) / CLOCKS_PER_SEC);

	return EXIT_SUCCESS;
}
示例#12
0
void sort_strings(item *A, int size, int max_length) {
    int groups[max_length + 1];

    partition(A, size, max_length, groups, item_string_length);

    for (int len = max_length; len > 0; len--) {
        counting_sort(A, groups[len - 1], size, 26, item_nth_char, len - 1);
    }
}
示例#13
0
void suffix_array() {
  int dict = 128; // n log n
  RA = RA_tmp[0]; RA2 = RA_tmp[1];
  iota(SA, SA + N, 0);
  for (int i = 0; i < N; ++i) RA[i] = S[i];
  for (k = 1; k < N; k <<= 1) {
    // n log^2 n
    sort(SA, SA + N, cmp);
    // n log n
    counting_sort(SA, SA2, RA + k, dict);
    counting_sort(SA2, SA, RA, dict);
    RA2[SA[0]] = 0;
    for (int i = 1; i < N; ++i) RA2[SA[i]] = RA2[SA[i - 1]] + cmp(SA[i - 1], SA[i]);
    swap(RA, RA2);
    if (RA[SA[N - 1]] == N - 1) break;
    dict = RA[SA[N - 1]] + 1; // n log n
  }
}
示例#14
0
void radix_sort(int *a,int n,int k)
{
 int d=0,i=0;
 int max=a[0];
  for(i=1;i<n;i++)
    if(a[i]>max)
      max=a[i];
    for(d=1;max/d>0;d=d*10)
      counting_sort(a,n,d);
}
示例#15
0
int main()
{
    int nums[] = {6, 4, 7, 8, 5, 6, 3, 9, 2, 1};
    int len = arr_len(nums);
    print(nums, len);
    counting_sort(nums, len);
    print(nums, len);

    return 0;
}
示例#16
0
int main()
{
    int A[10] = {3,6,10,8,4,2,7,1,9,5};
    int B[10];
    int i;
    counting_sort(A, B, 10, 10);
    for(i = 0; i < 10; i++){
        printf("index:%d B:%d\n", i, B[i]);
    }
    return 0;
}
示例#17
0
int main()  {
    int n = 0;
    scanf("%d", &n);
    int i = 0;
    int* ar = (int*) malloc(sizeof(int) * n);
    for ( i = 0; i < n; ++i ) {
        scanf("%d", ar + i);
    }
    counting_sort(ar, n);
    free(ar);
    return 0;
}
int main(void){
	int faltas[12] = {8, 5, 2, 5, 1, 8, 3, 1, 2, 4, 7, 1};
	char *alunos[12] = {"Joao", "Jose", "Maria", "Paulo", "Ana", "Pedro", "Rita", "Carlos", "Roberto", "Joana", "Fernando", "Antonio"};

	printf("Simple:\n");
	simple_counting_sort(faltas, alunos, 12, 10);

	printf("\nCouting Sort:\n");
	counting_sort(faltas, alunos, 12, 10);
	
	return 0;
}
int main(int argc, char *argv[]) {
  // Handle parameter
  if (argc != 2 && argc != 3) {
    fprintf(stderr, USAGE_MESSAGE, argv[0]);
    return EXIT_FAILURE;
  }
  size_t N = atoi(argv[1]);

  // Create and initialise list
  int *list = (int *) malloc(N * sizeof(int));
  if (list == NULL) {
    fprintf(stderr, MEMORY_MESSAGE);
    return EXIT_FAILURE;
  }
  int i;
  srand48(time(NULL));
  for (i = 0; i < N; ++i) {
    list[i] = lrand48() % RANGE_MAX;
  }

  // Print list before sorting
  if (argc == 3) {
    printf("List before sorting:\n");
    print_list(list, N);
  }

  // Run counting sort and measure time
  double begin = omp_get_wtime();
  list = counting_sort(list, N, RANGE_MAX);
  double end = omp_get_wtime();
  printf("Time spent: %fs\n", end-begin);

  // Print list after sorting
  if (argc == 3) {
    printf("\nList after sorting:\n");
    print_list(list, N);
  }

  // Check list
  if (checkSortedList(list, N) == 1) {
    fprintf(stderr, "List was not sorted correctly!\n");
    free(list);
    return EXIT_FAILURE;
  }

  // Free memory
  free(list);

  return EXIT_SUCCESS;
}
示例#20
0
// Driver program to test above function
int main()
{
    int n = 9, i;

    int input[] = {1, 5, 2, 7, 3, 4, 4, 1, 5};

    counting_sort(input, n);

    printf("Sorted Array : ");
    for(i = 0; i < n; i++)
        printf("%d ", input[i]);

    return 0;
}
示例#21
0
int main()
{
  // Array Which needs to be sorted
  int arr[] = {0, 6, 7, 2, 3, 5, 4, 4, 0};
  // COnsidering the value in the array will not be more then 7
  int *C;
  int arr_size, max_element;
  arr_size = sizeof(arr)/sizeof(int);
  max_element = 7;
  printf("Array before sorting\n");
  print(arr, arr_size);
  C = counting_sort(arr, max_element, arr_size);
  printf("Array after sorting\n");
  print(C, arr_size);
}
int main(int argc, char *argv[]){
  // Handle parameter
  if(argc != 2 && argc != 3){
    fprintf(stderr, USAGE_MESSAGE, argv[0]);
    return EXIT_FAILURE;
  }
  size_t N = atoi(argv[1]);

  // Create and initialise list
  int *list = (int *) malloc(N * sizeof(int));
  if(list == NULL){
    fprintf(stderr, MEMORY_MESSAGE);
    return EXIT_FAILURE;
  }
  int i;
  srand48(time(NULL));
  for(i=0; i<N; ++i){
    list[i] = lrand48() % RANGE_MAX;
  }

  // Print list before sorting
  if(argc == 3){
    printf("List before sorting:\n");
    print_list(list, N);
  }

  // Measure time
  clock_t begin, end;
  double time_spent;
  printf("Starting counting sort for problem size %zu\n", N);
  begin = clock();
  list = counting_sort(list, N, RANGE_MAX);
  end = clock();
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("Time spent: %fs\n", time_spent);

  // Print list after sorting
  if(argc == 3){
    printf("\nList after sorting:\n");
    print_list(list, N);
  }

  // Free memory
  free(list);

  return EXIT_SUCCESS;
}
示例#23
0
文件: test.c 项目: 4179e1/misc
int main (void)
{
	int array1[] = {5, 2, 4, 6, 1, 3};
	printf ("Insert Sort\n");
	insert_sort (array1, ARRAY_LEN(array1));
	print_array (array1, ARRAY_LEN(array1));

	int array2[] = {5, 2, 4, 6, 1, 3};
	printf ("Selection Sort\n");
	selection_sort (array2, ARRAY_LEN (array2));
	print_array (array2, ARRAY_LEN (array2));


	int array3[] = {5, 2, 4, 6, 1, 3};
	printf ("Merge Sort\n");
	merge_sort (array3, 0, ARRAY_LEN(array3) - 1);
	print_array (array3, ARRAY_LEN(array3));

	int array4[] = {5, 2, 4, 6, 1, 3};
	printf ("Bubble sort\n");
	bubble_sort (array4, ARRAY_LEN(array4));
	print_array (array4, ARRAY_LEN(array4));

	int arrayz[] = {7, 0, 9, 3, 9, 4};
	printf ("quick sort\n");
	quick_sort (arrayz, 0, ARRAY_LEN(arrayz) - 1);
	print_array (arrayz, ARRAY_LEN(arrayz));

	int array6[] = {2, 8, 7, 1, 3, 5, 6};
	printf ("randomized quick sort\n");
	randomized_quick_sort (array6, 0, ARRAY_LEN(array6) - 1);
	print_array (array6, ARRAY_LEN(array6));

	int array7[] = {2, 8, 7, 1, 3, 5, 6};
	printf ("tail quick sort\n");
	tail_quick_sort (array7, 0, ARRAY_LEN(array7) - 1);
	print_array (array7, ARRAY_LEN(array7));

	int a[] = {2, 5, 3, 0, 2, 3, 0, 3};
	int b[ARRAY_LEN(a)];
	memset (b, 0, sizeof(b));
	printf ("counting sort\n");
	counting_sort (a, b, ARRAY_LEN(a), ARRAY_LEN(b), 5);
	print_array (b, ARRAY_LEN(b));

	return 0;
}
示例#24
0
文件: radix-sort.c 项目: lttzzlll/Cup
// all of the files are counting sort interface
// so use these and think how to add new protocols
// counting sort driver
int counting_sort_main() {
    // test block
    int a[SIZE];
    int b[SIZE];
    int limit = 100;
    int n = 100;
    
    random_range(a, 1, n, limit);
    
    display(a, 1, n);
    
    counting_sort(a, b, 1, n, limit);
    
    display(b, 1, n);
    
    return 0;
}
/*  The main() begins  */
int main()
{
  int n, k = 0, A[15], i;
  printf("Enter the number of input : ");
  scanf("%d", &n);
  printf("\nEnter the elements to be sorted :\n");
  for (  i = 1; i <= n; i++)
  {
    scanf("%d", &A[i]);
    if (A[i] > k) {
      k = A[i];
    }
  }
  counting_sort(A, k, n);
  printf("\n");
  return 0;
}
示例#26
0
// 测试各种排序算法
//
void test_sort()
{
	for (int i = 0; sort_function_list[i].func != NULL; ++i) {
		printf("\n=== %s ===\n", sort_function_list[i].name);
		test_sort_function(sort_function_list[i].func);
	}

	const int length = 11;
	printf("\n=== 计数排序 ===\n");
	int array2[11] = {65, 32, 49, 10, 18, 72, 27, 42, 18, 58, 91};

	print_array(array2, length, " data: ");

	counting_sort(array2, length, 100);

	print_array(array2, length, " data: ");
}
示例#27
0
文件: main.c 项目: herbertdai/ITA
void testCountingSort() {
   printf("\nNow is Counting sort>>>>>>>>>>\n");
   
   int maxNumber = ARRAY_SIZE;


   startProfileTime();
   randomize_maxnum(A, ARRAY_SIZE, 256);
   print_array(A, ARRAY_SIZE);
   endProfileTime("Gen random array");

   startProfileTime();
   counting_sort(A, NULL, ARRAY_SIZE, maxNumber);
   endProfileTime("counting sort");

   printf("\nSorted array is :  ");
   print_array(A, ARRAY_SIZE);
}
示例#28
0
int main() {
    u_int input[INPUT_SIZE],
        *output,
        ilen = INPUT_SIZE,
        i;

    for (i = 0; i < ilen; ++i) {
        input[i] = rand_u_int();
    }
    printf("INPUT ARRAY:\n");
    print_array(input, INPUT_SIZE);
    printf("\n");

    output = counting_sort(input, ilen);
    printf("OUTPUT ARRAY:\n");
    print_array(output, INPUT_SIZE);
    printf("\n");
}
void radixsort( unsigned int *list, unsigned int listLen, unsigned int *tempbuffer )
{
  unsigned int i = 0, max = list[0], exp = 1;
  
  //Find the maximum number to know number of digits
  for (i = 1; i < listLen; i++)
  {
    if (list[i] > max)
      max = list[i];
  }
 
  //Do counting sort for every digit.
  while (max / exp > 0)
  {  
    counting_sort(list,tempbuffer,listLen,exp);
    exp *= 10;
  }
}
示例#30
0
/*  The main() begins  */
int main()
{
    int n, k = 0, i;
    printf("Enter the number of input : ");
    *(&n)=450;

    int A[n];
    printf("\nEnter the elements to be sorted :\n");
    for (i = 1; i <= n; i++)
    {
        *(&A[i])=710;
        if (A[i] > k) {
            k = A[i];
        }
    }
    counting_sort(A, k, n);
    printf("\n");
    return 0;
}