Exemplo n.º 1
0
void getSuffixArray() {
	m = 300;
	for (int i = 0; i < n; ++i) {
		rank[i] = str[i];
		tmp[i] = i;
	}
	radixSort();
	for (int j = 1, i, p; j < n; j <<= 1, m = p) {
		for (i = n - j, p = 0; i < n; ++i)
			tmp[p++] = i;
		for (i = 0; i < n; ++i)
			if (sa[i] >= j)
				tmp[p++] = sa[i] - j;
		radixSort();
		for (swap(tmp, rank), rank[sa[0]] = 0, i = p = 1; i < n; ++i)
			rank[sa[i]] = cmp(sa[i - 1], sa[i], j) ? p - 1 : p++;
	}
	for (int i = 0, j, k = 0; i < n; ++i, k = max(k - 1, 0))
		if (rank[i]) {
			j = sa[rank[i] - 1];
			for (; str[i + k] == str[j + k]; k++);
			height[rank[i]] = k;
		}
	for (int i = 2; i <= n; ++i)
		myLog[i] = myLog[i >> 1] + 1;
	for (int i = 1; i < n; ++i)
		f[i][0] = height[i];
	for (int j = 1; 1 << j <= n; ++j)
		for (int i = 1; i + (1 << j) <= n; ++i)
			f[i][j] = min(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
}
Exemplo n.º 2
0
int main()
{

    int a[8] = {25,36,16,13,9,5};
    radixSort(a,8);
    return 0;
}
Exemplo n.º 3
0
int main() {
    int a[] = {142, 245, 125, 425, 249, 1345, 112, 545, 2122, 4124};
    int i;
    radixSort(a, 10, 4);
    for(i = 0; i < 10; i++) printf("%d ",a[i]);
    printf("\n");
    return 0;
}
int main() {
	int A[] = {222, 542, 331, 110, 221, 332, 520, 312};
	int size = sizeof(A) / sizeof(int);
	radixSort(A, size, 9);
/*	for (int i = 0; i < size; ++i)
		std::cout << A[i] << " ";
*/
	return 0;
}
Exemplo n.º 5
0
void Solve::calcSuffixArr()
{
	int i, j, p;
	for(int i = 0; i < n; i ++)
		x[i] = str[i], y[i] = i;
	radixSort();
	for(p = j = 1; p < n; upperlim = p, j <<= 1)
	{
		for(p = 0, i = n - j; i < n; i ++) y[p ++] = i;
		for(int i = 0; i < n; i ++)
			if(sa[i] >= j)
				y[p ++] = sa[i] - j;
		radixSort();
		std::swap(x, y);
		for(i = 1, p = 1, x[sa[0]] = 0; i < n; i ++)
			x[sa[i]] = cmp(sa[i - 1], sa[i], j) ? p - 1 : p ++;
	}
}
Exemplo n.º 6
0
void testFunctionsClass() {
   //string infixVar = "ab+cd-*";
   //cout << getInfixExpression(infixVar);

   vector<string> radixVectorVar(3);
   radixVectorVar = { "123", "456", "789" };
   radixSort(radixVectorVar, 3, 10);

   return;
}
Exemplo n.º 7
0
	unsigned int DataBuffer::getSortedIndicesFromUintArray( unsigned int id )
	{
		assert( id < ((unsigned int) uintArrays.size()) );
		assert( uintArrays[id] != NULL );

		unsigned int ri = newUintArray( uintArrays[id]->sz );
		radixSort( uintArrays[id]->u, uintArrays[ri]->u, uintArrays[id]->sz );

		return ri;
	}
Exemplo n.º 8
0
void testFunctionsClass() {
   //string infixVar = "ab+cd-*";
   //cout << getInfixExpression(infixVar);

   vector<string> radixVectorVar(3);
   int digitsPerNumber = 3;
   int radix = 36;
   radixVectorVar = { "ABC", "123", "XYZ", "213", "ADF", "312", "QWE", "RTY", "123", "ABC" };
   radixSort(radixVectorVar, digitsPerNumber, radix);

   return;
}
int main(void) {
  struct CList *head;
  head = init(MAX);
  printf("Масивът преди сортирането:\n");
  print(head);
  head = radixSort(head);
  printf("Масивът след сортирането:\n");
  print(head);
  check(head);
  clear(head);
  return 0;
}
Exemplo n.º 10
0
int main(int argc, char* argv[])
{
    int array[MAX_NUM] = {329, 457, 657, 839, 436, 720, 355};
    printf("Test array is:\n");
    printArray(array, MAX_NUM);

    radixSort(array, MAX_NUM, 3, 9);

    printf("Sort array is:\n");
    printArray(array, MAX_NUM);
    
    return 0;
}
Exemplo n.º 11
0
//radix sort
void main()
{
    int i,a[10];
    for(i=0; i<10; i++)
    {
        scanf("%d",&a[i]);
    }
    radixSort(a,10);
    for(i=0; i<10; i++)
    {
        printf("%d ",a[i]);
    }
}
Exemplo n.º 12
0
int main()
{

	int n,i;
	printf("enter n:");
	scanf("%d",&n);
	int *array=(int *)malloc(n*sizeof(int ));
	printf ("enter array ");
	for ( i=0;i<n;i++)
		scanf("%d",&array[i]);
	radixSort(array,n);
	printarray(array,n);
	return 0;
}
void SubstrSorter::passAndSort(const std::vector<std::string>& strings)
{
	//std::cout << "passAndSort" << std::endl;
	//std::cout << "combineStrings" << std::endl;
	combineStrings(strings);
	//std::cout << "normalizeCombinedStrings" << std::endl;
	normalizeCombinedStrings();
	//std::cout << "divideCombinedStrings" << std::endl;
	divideCombinedStrings();
	//std::cout << "radixSort" << std::endl;
	radixSort(s12, engAlphSize, 3);

	//std::cout << "encodeS12" << std::endl;
	if (encodeS12()) {
		//std::cout << "translateS12" << std::endl;
		translateS12();
		//std::cout << "radixSortBasedOnS12" << std::endl;
		radixSortBasedOnS12(s12, 0);
	}

	//std::cout << "radixSortBasedOnS12" << std::endl;
	radixSortBasedOnS12(s0, 1);
	//std::cout << "radixSort" << std::endl;
	radixSort(s0, engAlphSize, 1);
	//std::cout << "merge" << std::endl;
	merge();
	//std::cout << "removeZeroesAndDuplicatesFromSA" << std::endl;
	removeZeroesAndDuplicatesFromSA();
	//std::cout << "createLCP" << std::endl;
	createLCP();
	//std::cout << "calcSubstrLen" << std::endl;
	calcSubstrLen();

	//std::cout << "revertNormalizedCombinedStrings" << std::endl;
	revertNormalizedCombinedStrings();
	//std::cout << "passAndSort finished" << std::endl;
}
Exemplo n.º 14
0
int main () {
    
    std::cout << "Radix sort program"  << std::endl;

    const IntList::value_type data[] = { 624, 852, 426, 987, 269,
                                       146, 415, 301, 730, 78, 593 };

    IntList values (data, data + sizeof data / sizeof *data);

    radixSort (values);
    std::copy (values.begin (), values.end (), OstreamIter (std::cout, " "));

    std::cout << std::endl << "End radix sort program" << std::endl;

    return 0;   
}
Exemplo n.º 15
0
void testRadixSort() {
   printf("\nNow is Radix sort>>>>>>>>>>\n");
   
   startProfileTime();
   randomize_maxnum(A, ARRAY_SIZE, 256);
   print_array(A, ARRAY_SIZE);
   endProfileTime("Gen random array");

   startProfileTime();
   radixSort(A, 0, ARRAY_SIZE - 1, 5, 10); 
   endProfileTime("Radix sort");

   printf("\nSorted array is :  ");
   print_array(A, ARRAY_SIZE);

}
Exemplo n.º 16
0
int main (int argc, char *argv[]) {
    const int size = atoi(argv[1]);
    srand(time(NULL));
	int i;
   
	types *array = malloc(size * sizeof(types));
	
	if (sizeof(types) == 1)
		for (i=0;i<size;i++) {
			uint8_t *temp = (uint8_t *) &array[i];
			*temp = (((double) rand() / (RAND_MAX - 1)) * 0x0f);
			if (rand() % 2) *temp |= 0x80;
		}
	else if (sizeof(types) == 2)
		for (i=0;i<size;i++) {
			uint16_t *temp = (uint16_t *) &array[i];
			*temp = (((double) rand() / (RAND_MAX - 1)) * 0x0fff);
			if (rand() % 2) *temp |= 0x8000;
		}
	else if (sizeof(types) == 4)
		for (i=0;i<size;i++) {
			uint32_t *temp = (uint32_t *) &array[i];
			*temp = (((double) rand() / (RAND_MAX - 1)) * 0x0fffffff);
			if (rand() % 2) *temp |= 0x80000000;
		}
	else if (sizeof(types) == 8)
		for (i=0;i<size;i++) {
			uint64_t *temp = (uint64_t *) &array[i];
			*temp = (((double) rand() / (RAND_MAX - 1)) * 0x0fffffffffffffff);
			if (rand() % 2) *temp |= 0x8000000000000000;
		}
		
	radixSort(RADIX_INT, &array[0], size);

    uint8_t sorted = 1;
    for (i=1;i<size;i++)
        if (array[i] < array[i-1]) {
            sorted = 0;
            break;
        }
    if (sorted) printf("Array sucessfully sorted!\n");
	else printf("Array was not sorted!\n");
	
	free(array);
    return 0;
}
int main(int v, char** c){

	inputCorretto(v, c);
	int len = atoi(c[1]);
	int cifre = atoi(c[2]);

	int** m;
	m = generateMatrixRadix( len, cifre );

	for( int i = 0; i < len; i++ ){
		printArray(m[i], cifre);
		putchar('\n');
	}

	radixSort(m, len, cifre);

	return 0;
}
Exemplo n.º 18
0
int main(){
    int A[MAX], n, i, Max = -1;

    printf("Enter Number of Elements: ");
    scanf("%d", &n);

    printf("Enter %d Elements(>=0):\n", n);
    for(i=0; i<n; i++){
        scanf("%d", &A[i]);
        Max = max(Max, A[i]);
    }

    radixSort(A, n, Max);

    printArray(A, n);

    return 0;
}
Exemplo n.º 19
0
//O(n*k) with the radixSort function as the longest operation
int main(int argc, char * argv[]){
    DEQUE *radixarray[RADIXSIZE], *maindeque;
    int i, number, max, mainitems;
    maindeque = createDeque();
    for(i = 0; i < RADIXSIZE; i++)
        radixarray[i] = createDeque();
    getNumbers(maindeque, &max);
    radixSort(maindeque, radixarray, max);
    mainitems = numItems(maindeque);
    system("clear");
    for(i = 0; i < mainitems; i++){
        number = removeLast(maindeque);
        printf("%d\n", number);
    }
    destroyDeque(maindeque);
    for(i = 0; i < RADIXSIZE; i++){
        destroyDeque(radixarray[i]);
    }
    return 0;
}
int main()
{ 	
	int size;
	Node* buckets;
	ElementType data[] = {110, 245, 895, 658, 321, 852, 147, 458, 469, 159, 347, 28};

	printf("\n\t====== test for radix sorting towards the data array ======\n");
	printf("\n\t=== the initial array is as follows ===\n");
	size = 12;
	printArray(data, size);

	buckets = initBuckets();
	printf("\n\t=== the buckets array is as follows ===\n");
	radixSort(buckets, data, size);
	printArray(data, size); 

	//printf("%2d", singleBit(28, 4));

	return 0;
}
Exemplo n.º 21
0
TEST(RadixSortRand, RadixSortTest) {
    static constexpr size_t n = 1 << 20;
    U32Vector vct(n);
    for (size_t i = 0; i < n; ++i) {
        vct[i] = rand();
    }
    U32Vector copy1(vct);
    U32Vector copy2(vct);
    {
        Timer t("radix");
        radixSort(copy1);
    }
    {
        Timer t("std::sort");
        sort(copy2.begin(), copy2.end());
    }
    for (size_t i = 1; i < n; ++i) {
        EXPECT_LE(copy1[i - 1], copy1[i]);
    }
}
Exemplo n.º 22
0
int main(void) {
	int *a, n, i;
	setbuf(stdout, NULL);

	printf("Enter n\n");
	scanf("%d",&n);

	a = (int*) calloc(n, sizeof(int));

	for(i = 0; i < n; i++){
		scanf("%d",&a[i]);
	}

	radixSort(a, n);

	for(i = 0; i < n; i++){
		printf("%d\t", a[i]);
	}

	return EXIT_SUCCESS;
}
Exemplo n.º 23
0
size_t Days::run() {
    // Step 1, pick a common root
    // The input files are required to have all leafs first in the file, sorted such that they have the same order,
    // thus graph1[0] == graph2[0] holds
    root = 0;
    // Step 2, label all leafs in T1 a DF manner
    // Step 3, label all corresponding leaf nodes in T2 as for T1 (no-ops)
    // Step 4 (part 1), set all splits in T1
    step2Counter = 1;
    visited.resize(graph1.size(), false);
    step2(root);
    // Step 4 (part 2), find all splits in T2
    fill(visited.begin(), visited.begin()+graph2.size(), false);
//    visited.assign(graph2.size(), false);
    step4(root);

    // Step 4 (part 3), find all shared splits between T1 and T2
    //TODO: use radix sort
    //sort(graph1NodeInfo.begin(), graph1NodeInfo.end(), Days::comp());
    //sort(graph2NodeInfo.begin(), graph2NodeInfo.end(), Days::comp());
    radixSort(graph1NodeInfo);
    radixSort(graph2NodeInfo);
    
//    size_t i;
//    for(i=0;i<graph1NodeInfo.size();i++){
//        cout<<"["<<graph1NodeInfo[i].minLabel<<","<<graph1NodeInfo[i].maxLabel<<","<<graph1NodeInfo[i].size<<"] ";
//    }
//    cout<<endl;
//    for(i=0;i<graph2NodeInfo.size();i++){
//        cout<<"["<<graph2NodeInfo[i].minLabel<<","<<graph2NodeInfo[i].maxLabel<<","<<graph2NodeInfo[i].size<<"] ";
//    }
//    cout<<endl;
    
    size_t t1, t2,sharedSplits = 0;
    const size_t size1 = graph1NodeInfo.size();
    const size_t size2 = graph2NodeInfo.size();

    t1=t2=0;
    while(t1 < size1 && t2 < size2){
    
        const node node1 = graph1NodeInfo[t1];
        const node node2 = graph2NodeInfo[t2];
        
        if(node1 == node2){
            if(node1.size == node2.size) {
//            if(node2.maxLabel - node2.minLabel + 1 == node2.size) {
                // node1 is always filled with consequently filled intervals, thus that size of node1 always indicates
                // a legal split
                sharedSplits++;
                t1++;
            }
            t2++;
        } else if(node1 < node2){
            t1++;
        } else{
            t2++;
        }
        
    }
    // The RF-distance is then "number of splits not found in both trees", which is equal to the number of splits
    // in both trees, minus all the shared splits between T1 and T2 minus all the shared splits in T2 and T1
    return size1 + size2 - 2*sharedSplits;
}
Exemplo n.º 24
0
int main(int argc, char* argv[])
{
	//printf("Hello World!\n");
	int i=0;
	int digit;
	int tmp;
	int *m_array =(int *)malloc(sizeof(int)*LENGTH);
	int *m_array_out =(int *)malloc(sizeof(int)*LENGTH);
	if (m_array==0  || m_array_out==0)
	{
		printf("内存空间不够%d字节\n",LENGTH);
		return 0;	
	} 
	else
	{
		for (i=0;i<LENGTH;i++)
		{
			m_array[i]=rand()%LENGTH;
			//m_array[i]=LENGTH-i;
			//m_array[i]=i;
			//printf("%d ",m_array[i]);

		}

		//selectSort(m_array,LENGTH);
		//mergeSort(m_array,LENGTH);
		//quickSort(m_array,LENGTH);
		//heapSort(m_array,LENGTH);
		//quickSort2(m_array,LENGTH);
		//tailRecurQuickSort(m_array,LENGTH);
		//randQuickSort(m_array,LENGTH);
		//countingSort(m_array,LENGTH,LENGTH-1,m_array_out);
		tmp=LENGTH-1;
		digit=0;
		while (tmp>0)
		{
			tmp=tmp>>4;
			digit++;
		}
		radixSort(m_array,LENGTH,digit,m_array_out);

		
// 		printf("\n");
// 		for (i=0;i<LENGTH;i++)
// 		{
// 			printf("%d ",m_array[i]);
// 		}
// 		printf("\n");
	

		testSort(m_array,LENGTH);
		//testSort(m_array_out,LENGTH);
		
// 		for (i=0;i<LENGTH;i++)
// 		{
// 			printf("binarySearch idx: %d\n",binarySearch(m_array,LENGTH,m_array[i]));
// 		}
// 		printf("binarySearch idx: %d\n",binarySearch(m_array,LENGTH,-11));

		free(m_array);
		free(m_array_out);
		return 0;
	}

}
Exemplo n.º 25
0
void NO_INLINE sort2(Key * data, size_t size)
{
	radixSort(data, size);
}
Exemplo n.º 26
0
void radixSort_16(int array[], int length) {
    radixSort(array, length, 16);
}
Exemplo n.º 27
0
void radixSort_256(int array[], int length) {
    radixSort(array, length, 256);
}
Exemplo n.º 28
0
/**
 *  main()
 *  The main function for input and output
 **/
int main(int argv, char** args)
{
    int n, a;
    int i = 0;
    int m = 0;
    char * file;
    char fd[80];
    FILE * data;
    int *A;
    struct timeval start, end;
    long elapsedTime;

    printf(" 1. random\n 2. Nearly Sorted \n 3. Few Unique\n 4. Reversed\n");
    printf("Please enter the number of the data you want to sort: \n");
    scanf("%d", &a);
    switch(a) {
        case(1): strcpy(fd, "Data/rd");
                break;
        case(2): strcpy(fd, "Data/nearlySorted");
                break;
        case(3): strcpy(fd, "Data/fewunique");
                break;
        case(4): strcpy(fd, "Data/reversed");
                break;
        default: strcpy(fd, "Data/rd");
                break;
    }


    printf("1. 100\n2. 1000\n3. 10000\n4. 100000\n ");
    printf("Please the letter of the number of elements you wish to sort:\n " );
    scanf("%d", &n);
    switch(n) {
          case 1: file = strcat(fd,"100.txt");
                          m = 100;
                          break;
          case 2: file = strcat(fd,"1000.txt");
                          m = 1000;
                          break;
          case 3: file = strcat(fd,"10000.txt");
                          m = 10000;
                          break;
          case 4: file = strcat(fd,"100000.txt");
                          m = 100000;
                          break;
          default: printf("Please enter 1, 2, 3, or 4.\n");
                          return 0;
    }

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

    data = fopen(file, "r");
    for (i = 0; fscanf (data, "%d", A + i) != EOF; i++);

    fclose(data);

    printf("%s************** Result **************\n",KYEL);
/*    printf("%sThe input array is: ", KCYN);
    for (i = 0; i < m; i++) {
        printf("%d ", A[i]);
    }
*/
    printf("\n");

    gettimeofday(&start, NULL);

    radixSort(A, m);

    gettimeofday(&end, NULL);
    elapsedTime = (end.tv_sec - start.tv_sec)*1000000 + end.tv_usec - start.tv_usec;

    /* print the sorted array */
/*    printf("%sThe sorted array is: ", KGRN);
    for(i = 1; i < m+1; ++i)
                printf(" %d ", A[i]);
*/
    printf("\n");
    printf("\nElapsed time for non-optimized Radix Sort is: ");
    printf("%ld", elapsedTime);
    printf("%s\n", " ns");

    gettimeofday(&start, NULL);

    radix_sort(A, m);

    gettimeofday(&end, NULL);
    elapsedTime = (end.tv_sec - start.tv_sec)*1000000 + end.tv_usec - start.tv_usec;
    printf("\n");
    printf("\nElapsed time for optimized Radix sort is: ");
    printf("%ld", elapsedTime);
    printf("%s\n", " ns");

    free(A);

}
Exemplo n.º 29
0
int main() {

  //Print initial array
  printf("\n//=====Shuffled array===================//");
  setarray();
  printarray("Initial Array");

  //=====Simple sorts=====//
  printf("\n\n//=====Simple sorts====================//");
  //Insertion sort
  setarray();
  insertionSort(array,10);
  printarray("InsertionSort");
  //Selection sort
  setarray();
  selectionSort(array,10);
  printarray("SelectionSort");
  
  //=====Eficient Sorts=====//
  printf("\n\n//=====Eficient sorts==================//");
  //Mergesort
  setarray();
  mergeSort(array,0,9,10);
  printarray("MergeSort");
  //Heapsort
  setarray();
  heapSort(array,10);
  printarray("HeapSort");
  //Quicksort
  setarray();
  quickSort(array,0,9);
  printarray("QuickSort");
  
  //Bubble sort and variants
  printf("\n\n//=====Bubble sort and variants========//");
  //Bubblesort
  setarray();
  bubbleSort(array,10);
  printarray("BubbleSort");
  //Shellsort
  setarray();
  shellSort(array,10);
  printarray("ShellSort");
  //Combsort 
  setarray();
  combSort(array,10);
  printarray("CombSort");

  //Distribution sorts
  printf("\n\n//=====Distribution sorts==============//");
  //Countingsort
  setarray();
  countingSort(array, 10, 0, 99);
  printarray("CountingSort");   
  //Radixsort
  setarray();
  radixSort(array, 10);
  printarray("RadixSort");  

  //Other sorts
  printf("\n\n//=====Other sorts=====================//");
  //Gnomesort
  setarray();
  gnomeSort(array,10);
  printarray("GnomeSort");    
  //Cyclesort
  setarray();
  cycleSort(array,10);
  printarray("CycleSort");

  printf("\n\n");     
}
List method7(List list, long long int size){
	List head = radixSort(list, 8, size);
	return head;
}