Exemplo n.º 1
0
static int Partition(int data[],int lo,int hi) 
{
    int key=data[hi];
    int j, i=lo-1;
    for(j=lo;j<hi;j++) 
    {
        if(data[j]<=key)
        {
            i=i+1;
            swap_array(&data[i],&data[j]);
        }
    }
    swap_array(&data[i+1],&data[hi]); 
    return i+1;
}
Exemplo n.º 2
0
/* Swap sort : bubble, quick ; 
 * Bubble sort, sometimes incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. 
 * The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. 
 * Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.
 */
void bubble_sort(int* a, int size) /* worst: O(n^2) */
{
    //_DEBUG_ENTER(bubble_sort);

    int i,j;
    for(i=1; i<size-1; i++)
        for(j=0; j<size-i; j++)
        {
            if(a[j]>a[j+1])
            {
                swap_array(&a[j],&a[j+1]);
            }
        }
}
Exemplo n.º 3
0
static void
ath9k_regd_sort(void *a, u32 n, u32 size, ath_hal_cmp_t *cmp)
{
	u8 *aa = a;
	u8 *ai, *t;

	for (ai = aa + size; --n >= 1; ai += size)
		for (t = ai; t > aa; t -= size) {
			u8 *u = t - size;
			if (cmp(u, t) <= 0)
				break;
			swap_array(u, t, size);
		}
}
Exemplo n.º 4
0
/* Selection sort: 选择, 堆排序
 * Selection sort: The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. 
 * Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. 
 * The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
 * */
void selection_sort(int *a, int size) /* worst: O(n^2) */
{
    //_DEBUG_ENTER(selection_sort);

    int i,j,max;
    for(i=0; i<size-1; i++)
    {
        max = 0;
        for(j=0; j<size-i; j++)
        {
            if(a[j]>a[max])
                max = j;
        }
        if(max != (size-i-1))
            swap_array(&a[max], &a[size-i-1]);
    }
}
Exemplo n.º 5
0
int main(int argc, char* argv[])
{	
	double dt = TIME_TOTAL / TIME_PARTS;
	double dl = ROD_LENGTH / ROD_PARTS;
	
	int myrank, all_processes;
	MPI_Init(&argc, &argv);
	MPI_Comm_size(MPI_COMM_WORLD, &all_processes);
	MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

	double mytime;
	if (myrank == 0){
		mytime = MPI_Wtime();
	}
	
	//Инициализация
	int block_size = (ROD_PARTS	 - 1) / all_processes;
	if (myrank < (ROD_PARTS - 1) % all_processes){
		++block_size;
	}
	double* temperature = (double*)malloc(sizeof(double) * (block_size + 2));
	double* old_temperature = (double*)malloc(sizeof(double) * (block_size + 2));
	
	for (int i = 0; i < block_size + 2; ++i){
		old_temperature[i] = 1.0;
	}
	if (myrank == 0){
		old_temperature[0] = 0.0;
	}
	if (myrank == all_processes - 1){
		old_temperature[block_size + 1] = 0.0;
	}
	//конец инициализации
	
	for (int t = 0; t < TIME_PARTS; t++){
		if (t != 0){
			if (myrank % 2 == 0){
				send_data(old_temperature, block_size, myrank, all_processes);
				recv_data(old_temperature, block_size, myrank, all_processes, MPI_STATUS_IGNORE);
			}
			else{
				recv_data(old_temperature, block_size, myrank, all_processes, MPI_STATUS_IGNORE);
				send_data(old_temperature, block_size, myrank, all_processes);
			}
		}
		for (int i = 1; i <= block_size; i++){
			temperature[i] = 
				old_temperature[i] + 
				dt * TEMPERATURE_COEFF * (old_temperature[i + 1] - 
				2 * old_temperature[i] + 
				old_temperature[i - 1]) / (dl * dl);
		}
		swap_array(&old_temperature, &temperature);
	}
	
	if (myrank != 0){//Отсылаем главному процессу результат
		MPI_Send(old_temperature + 1, block_size, MPI_DOUBLE, 0, myrank, MPI_COMM_WORLD);
	}
	else{
		double *results = (double*)malloc(sizeof(double) * (ROD_PARTS + 1));
		//Крайние точки сохраняют температуру
		results[0] = 0;
		results[ROD_PARTS] = 0;

		for (int i = 1; i <= block_size; i++)
			results[i] = old_temperature[i];

		int start = block_size + 1;
		for (int i = 1; i < all_processes; i++)		{
			int recv_size = (ROD_PARTS - 1) / all_processes;
			if (i < (ROD_PARTS - 1) % all_processes)
				recv_size++;
			MPI_Recv(results + start, recv_size, MPI_DOUBLE, i, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
			start += recv_size;
		}
		
		/*//Вывод результата на экран
		for (int i = 0; i <= ROD_PARTS; i++)
			printf("%.6lf\n", results[i]);
		
		printf("-------------------------------------------\n");

		for (int i = 1; i < ROD_PARTS; i++){
			double x = i * dl;
			double sum = 0;
			for (int m = 0; m < SUM_LENGTH; m++){
				double a = exp(-TEMPERATURE_COEFF * M_PI * M_PI * (2 * m + 1) * (2 * m + 1) * TIME_TOTAL)
				 * sin(M_PI * (2 * m + 1) * x / ROD_LENGTH) / (2 * m + 1);
				sum = sum + 4 * 1 * a / M_PI;
			}
			double value = sum;
			printf("%.6f\n delta = %.6f\n", value, fabs(value - results[i]));
		}*/

		free(results);
	}

	if (myrank == 0){
		mytime = MPI_Wtime() - mytime;
		printf("%f\n", mytime);
	}
	
	free(temperature);
	free(old_temperature);
	
	MPI_Finalize(); 
	return 0;
}