Exemplo n.º 1
0
void dc3(int *r, int *sa, int n, int m) {
    int j, *rn = r + n, *san = sa + n, ta = 0, tb = (n + 1) / 3, tbc = 0, p;
    r[n] = r[n + 1] = 0;
    rep(i, n)
    if (i % 3 != 0) wa[tbc++] = i;
    rsort(r + 2, wa, wb, tbc, m);
    rsort(r + 1, wb, wa, tbc, m);
    rsort(r, wa, wb, tbc, m);
    for (p = 1, rn[F(wb[0])] = 0, j = 1; j < tbc; j++)
        rn[F(wb[j])] = c0(r, wb[j - 1], wb[j]) ? p - 1 : p++;
    if (p < tbc) dc3(rn, san, tbc, p);
    else rep(i, tbc)
    san[rn[i]] = i;
    rep(i, tbc)
    if (san[i] < tb) wb[ta++] = san[i] * 3;
    if (n % 3 == 1) wb[ta++] = n - 1;
    rsort(r, wb, wa, ta, m);
    rep(i, tbc)
    wv[wb[i] = G(san[i])] = i;
    int i;
    for (i = j = p = 0; i < ta && j < tbc; p++)
        sa[p] = c12(wb[j] % 3, r, wa[i], wb[j]) ? wa[i++] : wb[j++];
    for (; i < ta; p++) sa[p] = wa[i++];
    for (; j < tbc; p++) sa[p] = wb[j++];
}
Exemplo n.º 2
0
int main()
{
	int i,nums[MAX],right,left,cnt=0;
	FILE *fp;
	
	if((fp=fopen("kadai-02-1.txt","r"))==NULL){
		printf("can't open a file\n");
		exit(1);
	}
	
	while(fscanf(fp,"%d",&nums[cnt]) != EOF) cnt++;
	
	left=0;right=cnt-1;
	
	while(left<right){
		right=rsort(nums,left,right);
		
		if(left>=right-1) break;
		
		left=lsort(nums,left,right);
	}
	
	for(i=0;i<cnt;i++){
		printf("%d\n",nums[i]);
	}
	
	return 0;
}
Exemplo n.º 3
0
static F2(jtcfrz){A z;B b=0,p;I j,n;Z c,d,*t,*u,*v;
 RZ(w=rsort(w)); 
 n=AN(w); u=ZAV(w); 
 GATV(z,CMPX,1+n,1,0); v=ZAV(z); *v=c=*ZAV(a); p=!c.im;
 for(j=0;j<n;++j){
  d=znegate(u[j]); t=j+v; *(1+t)=*t; 
  DO(j, *t=zplus(*(t-1),ztymes(d,*t)); --t;); 
  *v=ztymes(d,*v);
  if(p&&d.im)if(b=!b)c=u[j]; else if(p=ZCJ(c,u[j])){t=v; DO(2+j, t++->im=0.0;);}
Exemplo n.º 4
0
void suffixarray(char text[],int t[],int m)
{
    int i,j,k,n;
    static int s[N*2],tmp[N+2];
    for(n=0;text[n];n++)s[n]=text[n],t[n]=n;
    for(t[n]=i=n;i<n*2;i++)s[i]=0;
    for(i=1;i<n;i*=2)
    {
        rsort(s+i,t,tmp,n,m);
        rsort(s,tmp,t,n,m);
        for(j=0;j<n;j++)tmp[j]=s[j];
        for(j=0,m=1;j<n;j++)
        {
            s[t[j]]=m;
            if(tmp[t[j]]!=tmp[t[j+1]] || tmp[t[j]+i]!=tmp[t[j+1]+i])m++;
        }
    }
}
Exemplo n.º 5
0
int main()
{
	int numeros[10] = {1,3,6,2,4,3,1,8,6,9};
	imprimir (numeros, 10);
	sort(numeros, 10);
	imprimir (numeros, 10);
	rsort(numeros, 10);
	imprimir (numeros, 10);
	return EXIT_SUCCESS;
}
Exemplo n.º 6
0
int main()
{
    int round,i,tmp;
    node_ptr root=NULL;
    scanf("%d",&round);
    for(i=0;i<round;i++)
    {
        scanf("%d",&tmp);
        root=insert_list(root,tmp);
    }
    root=rsort(root);
    print_list(root);
}
Exemplo n.º 7
0
int main(int argc, char** argv){

    uint8_t prefix = atoi(argv[argc-1]);

    /////////////////////////////////////////////////////////////
    //  PartitionCount
    /////////////////////////////////////////////////////////////
    FILE *partition = fopen(argv[2], "rb");
    if(partition == NULL) {
        fprintf(stderr, "%s\n", "Fail to open file");
        exit(0);
    }
    fread(partitionCount, sizeof(unsigned long long), 256, partition);
    fclose(partition);

    /////////////////////////////////////////////////////////////
    //  Load Partition
    /////////////////////////////////////////////////////////////
    FILE *fp = fopen(argv[1], "rb");
    if(fp == NULL) {
        fprintf(stderr, "%s\n", "Fail to open file");
        exit(0);
    }

    S_Prefix = malloc(sizeof(*S_Prefix) * partitionCount[prefix]);
    if(S_Prefix == NULL) {
        fprintf(stderr, "%s\n", "Fail to alloc memory: S_Prefix");
        exit(0);
    }
    
    prefixMemory = malloc(sizeof(*prefixMemory)*READ_CODE_LEN* partitionCount[prefix]);

    unsigned int readNum = 0;
    while(fread(prefixMemory + readNum*READ_CODE_LEN*1024, sizeof(uint8_t), READ_CODE_LEN*1024, fp))
        readNum++;
    fclose(fp);



    unsigned long long i, j;
    for(i = 0; i < partitionCount[prefix]; i++){
        S_Prefix[i] = prefixMemory + i * READ_CODE_LEN;
        if(S_Prefix[i] == NULL){
            fprintf(stderr, "Fail to alloc memory: S_Prefix[%llu]\n", i);
        }
    }

        
   ///////////////////////////////////////////////////////////////////
   //  Print Pre-sort suffixes
   ///////////////////////////////////////////////////////////////////        
   //printf("Pre-sort suffixes with prefix %d\n", prefix);
   //PrinfSuffixes(S_Prefix, partitionCount[prefix]);


   ///////////////////////////////////////////////////////////////////
   //  Sort suffixes
   ///////////////////////////////////////////////////////////////////           
    ta = malloc(sizeof(*ta)*partitionCount[prefix]); // Total size: 4n
    if(ta == NULL) {
        fprintf(stderr, "%s\n", "Fail to alloc memory: ta");
        exit(0);
    }
    stack = malloc(sizeof(*stack)*partitionCount[prefix]);
    if(stack == NULL) {
        fprintf(stderr, "%s\n", "Fail to alloc memory: stack");
        exit(0);
    }
    rsort(S_Prefix, ta, stack, partitionCount[prefix]);
    free(stack); stack = NULL;
    free(ta); ta = NULL;
   

    ///////////////////////////////////////////////////////////////////
    //  Print sorted suffixes
    ///////////////////////////////////////////////////////////////////        
    //printf("Sorted suffixes with prefix %d\n", prefix);
    //PrinfSuffixes(S_Prefix, partitionCount[prefix]);


    free(prefixMemory);
    free(S_Prefix);

}
Exemplo n.º 8
0
/*!
 *  \param argv[1] Dimension of square matrix, i.e. number of rows = number of columns
 */
int main(int argc, char** argv) {

    /* Message identifier for sending/receiving columns in a matrix */
    int COLUMN_TAG = 0;
    /* Dimension of square matrix (i.e. number of rows = number of columns) */
    int DIMENSION;
    /* Used for error handling */
    int error_code;
    /* Total number of processes used in this program */
    int NUMBER_OF_PROCESSES;
    /* Current process */
    int PROCESS_ID;
    /* Main loop counter. Counts from 0 to \f$\log_2 N\f$, where N = DIMENSION. */
    int program_counter;
    /* Message identifier for sending/receiving rows in a matrix */
    int ROW_TAG = 1;

    /* Used to start timing shearsort algorithm */
    time_t start;
    /* Used to end timing shearsort algorithm */
    time_t end;

    /* Derived datatype for sending a column in a matrix to a process */
    MPI_Datatype column_type;
    /* Used in MPI_Recv */
    MPI_Status status;

    /***************************************************************************************************/

    if (argc != 2) {
       printf("Usage: ./shearsort [dimension of square matrix]\nPlease try again.\n");
       exit(1);
    }

    if ((DIMENSION = atoi(argv[1])) <= 0) {
       printf("Error: Invalid argument for dimension of square matrix. Please try again.\n");
       exit(1);
    }

    /***************************************************************************************************/

    error_code = MPI_Init(&argc, &argv);
    error_code = MPI_Comm_size(MPI_COMM_WORLD, &NUMBER_OF_PROCESSES);
    error_code = MPI_Comm_rank(MPI_COMM_WORLD, &PROCESS_ID);

    if (error_code != 0) {
       printf("Error initializing MPI and obtaining task information.\n");
       MPI_Finalize();
       exit(1);
    }

    if (DIMENSION != NUMBER_OF_PROCESSES) {
       printf("Dimension of square matrix = %d\tNumber of processes = %d\n", DIMENSION, NUMBER_OF_PROCESSES);
       printf("Number of processes does NOT equal dimension of square matrix. Please try again.\n");
       MPI_Finalize();
       exit(1);
    }

    srand(time(NULL));

    /***************************************************************************************************/

    int matrix[DIMENSION][DIMENSION];
    int numbers[DIMENSION];

    /***** Create a derived datatype that contains all of the elements in a column *****/
    error_code = MPI_Type_vector(DIMENSION, 1, DIMENSION, MPI_INT, &column_type);
    error_code = MPI_Type_commit(&column_type);

    if (error_code != 0) {
       printf("Error creating derived vector datatype.\n");
       MPI_Finalize();
       exit(1);
    }

    /****************************************************************************************************
    ** MASTER                                                                                          **
    ****************************************************************************************************/
    if (PROCESS_ID == MASTER) {
       int current_column,
           current_row,
           i,
           destination,    /* recipient of data */
           source;         /* sender of data    */

       unsigned char is_sorted; /* TRUE if matrix is sorted diagonally in ascending order */

       printf("\n");
       printf("Initializing matrix...\n");
       printf("\n");

       initialize(&matrix[0][0], DIMENSION, DIMENSION);

       #ifdef DEBUG
           /*************************************************************************************
           ** Note: Be sure that DIMENSION is not too large so that the matrix will be small   **
           **       enough to be viewable. --BPD                                               **
           *************************************************************************************/
           printf("======================================================================\n");
           printf("== Initial matrix                                                   ==\n");
           printf("======================================================================\n\n");
           print_matrix(&matrix[0][0], DIMENSION, DIMENSION);
           printf("\n");
       #endif

       printf("Sorting matrix...\n");
       printf("\n");

       start = time(NULL);

       for (program_counter = 0; program_counter < (int) ceil(log((double) DIMENSION) / log(2.0)); program_counter++) {
           printf("   Pass %d of %d...\n\n", program_counter + 1, (int) ceil((log((double) DIMENSION) / log(2.0))));

           /****************************************************************************************************
           ** Send unsorted rows to workers, then receive sorted rows from them                               **
           ****************************************************************************************************/
           printf("      Sending rows to workers... ");
           for (destination = 1; destination < NUMBER_OF_PROCESSES; destination++) {
               MPI_Send(&matrix[destination][0], DIMENSION, MPI_INT, destination, ROW_TAG, MPI_COMM_WORLD);
           }
           for (source = 1; source < NUMBER_OF_PROCESSES; source++) {
               MPI_Recv(&matrix[source][0], DIMENSION, MPI_INT, source, ROW_TAG, MPI_COMM_WORLD, &status);
           }

           /***** Master sorts its row *****/
           sort(&matrix[0][0], DIMENSION);

           printf("Received sorted rows from workers.\n\n");

           /****************************************************************************************************
           ** Send unsorted columns to workers, then receive sorted columns from them                         **
           ****************************************************************************************************/
           printf("      Sending columns to workers... ");
           for (destination = 1; destination < NUMBER_OF_PROCESSES; destination++) {
               MPI_Send(&matrix[0][destination], 1, column_type, destination, COLUMN_TAG, MPI_COMM_WORLD);
           }
           for (source = 1; source < NUMBER_OF_PROCESSES; source++) {
               MPI_Recv(&numbers, DIMENSION, MPI_INT, source, COLUMN_TAG, MPI_COMM_WORLD, &status);
               for (i = 0; i < DIMENSION; i++) {
                   matrix[i][source] = numbers[i];
               }
           }
           printf("Received sorted columns from workers.\n\n");

           /***** Master sorts its column *****/
           for (i = 0; i < DIMENSION; i++) {
               numbers[i] = matrix[i][0];
           }
           sort(numbers, DIMENSION);
           for (i = 0; i < DIMENSION; i++) {
               matrix[i][0] = numbers[i];
           }
       }

       /****************************************************************************************************
       ** For the last time, send unsorted rows to workers and then receive sorted rows from them         **
       ****************************************************************************************************/
       printf("   Sending rows to workers... ");
       for (destination = 1; destination < NUMBER_OF_PROCESSES; destination++) {
           MPI_Send(&matrix[destination][0], DIMENSION, MPI_INT, destination, ROW_TAG, MPI_COMM_WORLD);
       }
       for (source = 1; source < NUMBER_OF_PROCESSES; source++) {
           MPI_Recv(&matrix[source][0], DIMENSION, MPI_INT, source, ROW_TAG, MPI_COMM_WORLD, &status);
       }
       printf("Received sorted rows from workers.\n\n");

       /****************************************************************************************************
       ** Check if diagonals below and above the main diagonal are sorted in ascending order              **
       ****************************************************************************************************/
       printf("Checking if matrix is sorted... ");
       for (i = 0, is_sorted = TRUE; i < DIMENSION - 1 && is_sorted == TRUE; i++) {
           for (current_row = i, current_column = 0; current_row < DIMENSION - 1 && is_sorted == TRUE; current_row++, current_column++) {
               if (matrix[current_row][current_column] > matrix[current_row + 1][current_column + 1]) {
                  is_sorted = FALSE;
               }
           }
       }
       for (i = 0; i < DIMENSION - 1 && is_sorted == TRUE; i++) {
           for (current_row = 0, current_column = i; current_column < DIMENSION - 1 && is_sorted == TRUE; current_row++, current_column++) {
               if (matrix[current_row][current_column] > matrix[current_row + 1][current_column + 1]) {
                  is_sorted = FALSE;
               }
           }
       }

       if (is_sorted == TRUE) {
          printf("Matrix is sorted.\n\n");
          printf("Printing results...\n\n");
       }
       else {
          printf("Matrix is NOT sorted. Aborting...\n\n");
          MPI_Finalize();
          exit(1);
       }

       end = time(NULL);
    }
    /****************************************************************************************************
    ** WORKERS                                                                                         **
    ****************************************************************************************************/
    else {

       /****************************************************************************************************
       ** Get rows from Master, sort them, and then return them back to Master                            **
       ****************************************************************************************************/
       for (program_counter = 0; program_counter < (int) ceil((log((double) DIMENSION) / log(2.0))); program_counter++) {
           MPI_Recv(&numbers, DIMENSION, MPI_INT, MASTER, ROW_TAG, MPI_COMM_WORLD, &status);
           /***** Sort even rows in ascending order and odd rows in descending order *****/
           if (PROCESS_ID % 2 == 0) {
              sort(numbers, DIMENSION);
           }
           else {
              rsort(numbers, DIMENSION);
           }
           MPI_Send(&numbers, DIMENSION, MPI_INT, MASTER, ROW_TAG, MPI_COMM_WORLD);

           /****************************************************************************************************
           ** Get columns from Master, sort them, and then return them back to Master                         **
           ****************************************************************************************************/
           MPI_Recv(&numbers, DIMENSION, MPI_INT, MASTER, COLUMN_TAG, MPI_COMM_WORLD, &status);
           sort(numbers, DIMENSION);
           MPI_Send(&numbers, DIMENSION, MPI_INT, MASTER, COLUMN_TAG, MPI_COMM_WORLD);
       }

       /****************************************************************************************************
       ** For the last time, get rows from Master, sort them, and then return them back to Master         **
       ****************************************************************************************************/
       MPI_Recv(&numbers, DIMENSION, MPI_INT, MASTER, ROW_TAG, MPI_COMM_WORLD, &status);
       sort(numbers, DIMENSION);
       MPI_Send(&numbers, DIMENSION, MPI_INT, MASTER, ROW_TAG, MPI_COMM_WORLD);

    }

    /****************************************************************************************************
    ** Print results                                                                                   **
    ****************************************************************************************************/
    if (PROCESS_ID == MASTER) {
       #ifdef DEBUG
           int current_column,
               current_row,
               i;

           printf("======================================================================\n");
           printf("== Diagonals                                                        ==\n");
           printf("======================================================================\n\n");
           printf("Below main diagonal in sorted matrix:\n\n");
           for (i = 0; i < DIMENSION; i++) {
               for (current_row = i, current_column = 0; current_row < DIMENSION; current_row++, current_column++) {
                   printf("%10d\t", matrix[current_row][current_column]);
               }
               printf("\n");
           }
           printf("\nAbove main diagonal in sorted matrix:\n\n");
           for (i = 0; i < DIMENSION; i++) {
               for (current_row = 0, current_column = i; current_column < DIMENSION; current_row++, current_column++) {
                   printf("%10d\t", matrix[current_row][current_column]);
               }
               printf("\n");
           }
           printf("\n");
           printf("======================================================================\n");
           printf("== Sorted matrix                                                    ==\n");
           printf("======================================================================\n\n");
           print_matrix(&matrix[0][0], DIMENSION, DIMENSION);
           printf("\n");
       #endif

       printf("======================================================================\n");
       printf("== Summary                                                          ==\n");
       printf("======================================================================\n\n");
       printf("Total number of processes:    %10d\n\n", NUMBER_OF_PROCESSES);
       printf("Dimension of square matrix:   %10d\n",  DIMENSION);
       printf("Number of elements in matrix: %10d\n\n", DIMENSION * DIMENSION);
       printf("Total runtime:                   %10.2f seconds\n\n", difftime(end, start));
    }

    MPI_Finalize();

    return 0;

}