view example/many_task/ppe/QuickSort.cc @ 220:305ac1897c50 draft

fix
author gongo@localhost.localdomain
date Mon, 09 Feb 2009 21:58:45 +0900
parents 028ffc9c0375
children 839e34d0cc3c
line wrap: on
line source

#include "QuickSort.h"
#include <stdio.h>
#include <string.h>

SchedDefineTask(QuickSort);

int
QuickSort::run(void* rbuff, void* wbuff) {
    // copy value
    int begin	= 0;
    int end = get_param(0);

    Data *r_data = (Data*)get_input(rbuff, 0);
    Data *w_data = (Data*)get_output(wbuff, 0);

    //printf("[PPE] Quick: length:%d addr->%x \n",end, (int*)rbuff);
    //printf("[PPE] Quick: data[0]: %d addr->%x\n",sizeof(r_data),r_data); 

    //show_data(r_data, end);
    quick_sort(r_data, begin, end-1);
    memcpy(w_data, r_data, sizeof(Data)*end);

    return 0;
}

void
QuickSort::quick_sort( Data *data, int begin, int end ) {

    if (begin < end) {
	int where = (begin + end) / 2;
	int pivot = data[where].index;
	data[where].index = data[begin].index;
	int p = begin;
	int i;
	for (i=begin+1; i<=end; i++) {
	    if (data[i].index < pivot) { 
		p++; 
		swap(data, p, i); 
	    }
	}
	data[begin].index = data[p].index;
	data[p].index = pivot;
	
	quick_sort(data, begin, p-1);
	quick_sort(data, p+1, end);
    }
}

void
QuickSort::swap( Data *data, int left, int right )
{
    int tmp	      = data[left].index;
    data[left].index  = data[right].index;
    data[right].index = tmp;
}