int main(int v, char **c) { //srand(( unsigned )time(NULL)); inputCorretto(v, c); char ch; double *a; int len = atoi(c[2]); fprintf(stdout, "Vuoi generare l'array a random (y/n)? "); fscanf(stdin, "%c", &ch); if( ch == 'y') { a = generateArray(RANDOM, DOUBLE, len, 100); for( int i = 0; i<len; i++) a[i] = a[i]/100; } else a = generateArray(NOTRANDOM, DOUBLE, len); printArray(a, len, "double"); bucketSort( a, len ); return 0; }
int main() { float array[] = {0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68}; int i; bucketSort(array, 10); pr(array, 10); return 0; }
void doRadixSort(){ DType bv = 1; for (int i=0;i<times;++i){ bucketSort(bv); bv *= mask; remap(); } }
int main() { double a[] = {0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68}; int size = sizeof(a) / sizeof(double); bucketSort(a, size); /* for (int i = 0; i < size; ++i) { std::cout << a[i] << " "; } */ return 0; }
int main() { cout<<"原始数组:"; for(int i=0;i<len;i++) cout<<a[i]<<","; cout<<endl; bucketSort(a); cout<<"排序后数组:"; for(int i=0;i<len;i++) cout<<a[i]<<","; cout<<endl; return 0; }
// proceeding the radix sorting for the array void radixSort(Node* buckets, ElementType* data, int size) { int i; int j; for(i = 1; i <= Round; i++) { for(j=0; j<size; j++) bucketSort(buckets, data[j], singleBit(data[j],i)); // coducting bucket sorting for data array over bucketsToData(buckets, data);// and now, we update the data array from buckets clearBuckets(buckets); } }
int main() { std::mt19937 engine; std::uniform_int_distribution<int> distro(0, 127); std::vector<int> list(5000000); for (auto iter = list.begin(); iter != list.end(); ++iter) { *iter = distro(engine); } auto bucketSorted = list; auto qsorted = list; auto now = std::chrono::system_clock::now(); bucketSort(bucketSorted.begin(), bucketSorted.end()); auto then = std::chrono::system_clock::now(); auto diff1 = std::chrono::duration_cast<std::chrono::microseconds>(then - now).count(); now = std::chrono::system_clock::now(); std::sort(qsorted.begin(), qsorted.end()); then = std::chrono::system_clock::now(); auto diff2 = std::chrono::duration_cast<std::chrono::microseconds>(then - now).count(); int i = 0; /*for(auto itr : bucketSorted) { ++i; if (i % 5 == 0) std::cout << std::endl; std::cout << std::setw(5) << itr; }*/ std::cout << std::endl; std::cout << "Bucket sort took: " << diff1 << " microsecs." << std::endl; std::cout << "std::sort took: " << diff2 << " microsecs." << std::endl; return 0; }
int main(int argc, char** argv) { int err; // error code returned from api calls unsigned int correct; // number of correct results returned size_t global; // global domain size for our calculation size_t local; // local domain size for our calculation unsigned int *results; cl_device_id device_id; // compute device id cl_context context; // compute context cl_command_queue commands; // compute command queue cl_program program; // compute program cl_kernel kernel; // compute kernel cl_mem input; // device memory used for the input array cl_mem output; // device memory used for the output array // Fill our data set with random float values // int numElements = 0 ; if(strcmp(argv[1],"r") ==0) { numElements = SIZE; } else { FILE *fp; fp = fopen(argv[1],"r"); if(fp == NULL) { printf("Error reading file \n"); exit(EXIT_FAILURE); } int count = 0; float c; while(fscanf(fp,"%f",&c) != EOF) { count++; } fclose(fp); numElements = count; } printf("Sorting list of %d floats.\n", numElements); int mem_size = (numElements + (DIVISIONS*4))*sizeof(float); // Allocate enough for the input list float *cpu_idata = (float *)malloc(mem_size); float *cpu_odata = (float *)malloc(mem_size); // Allocate enough for the output list on the cpu side float *d_output = (float *)malloc(mem_size); // Allocate enough memory for the output list on the gpu side float *gpu_odata = (float *)malloc(mem_size); float datamin = FLT_MAX; float datamax = -FLT_MAX; if(strcmp(argv[1],"r")==0) { for (int i = 0; i < numElements; i++) { // Generate random floats between 0 and 1 for the input data cpu_idata[i] = ((float) rand() / RAND_MAX); //Compare data at index to data minimum, if less than current minimum, set that element as new minimum datamin = fminf(cpu_idata[i], datamin); //Same as above but for maximum datamax = fmaxf(cpu_idata[i], datamax); } } else { FILE *fp; fp = fopen(argv[1],"r"); for(int i = 0; i < numElements; i++) { fscanf(fp,"%f",&cpu_idata[i]); datamin = fminf(cpu_idata[i], datamin); datamax = fmaxf(cpu_idata[i],datamax); } } FILE *tp; const char filename2[]="./hybridinput.txt"; tp = fopen(filename2,"w"); for(int i = 0; i < SIZE; i++) { fprintf(tp,"%f ",cpu_idata[i]); } fclose(tp); memcpy(cpu_odata, cpu_idata, mem_size); clock_t gpu_start = clock(); init_bucketsort(numElements); int *sizes = (int*) malloc(DIVISIONS * sizeof(int)); int *nullElements = (int*) malloc(DIVISIONS * sizeof(int)); unsigned int *origOffsets = (unsigned int *) malloc((DIVISIONS + 1) * sizeof(int)); clock_t bucketsort_start = clock(); bucketSort(cpu_idata,d_output,numElements,sizes,nullElements,datamin,datamax, origOffsets); printf("bucketsort is executed\n"); clock_t bucketsort_diff = clock() - bucketsort_start; printf("time is recorded\n"); finish_bucketsort(); printf("bucketsort is finished\n"); double bucketTime = getBucketTime(); cl_float4 *d_origList = (cl_float4*) d_output; cl_float4 *d_resultList = (cl_float4*) cpu_idata; int newlistsize = 0; for(int i = 0; i < DIVISIONS; i++){ newlistsize += sizes[i] * 4; } printf("begin to initialize mergesort\n"); init_mergesort(newlistsize); printf("init_mergesort is successfully\n"); clock_t mergesort_start = clock(); cl_float4 *mergeresult = runMergeSort(newlistsize,DIVISIONS,d_origList,d_resultList,sizes,nullElements,origOffsets); clock_t mergesort_diff = clock() - mergesort_start; finish_mergesort(); gpu_odata = (float*)mergeresult; #ifdef TIMER clock_t gpu_diff = clock() - gpu_start; int gpu_msec = gpu_diff * 1000 / CLOCKS_PER_SEC; int bucketsort_msec = bucketsort_diff * 1000 / CLOCKS_PER_SEC; int mergesort_msec = mergesort_diff * 1000 / CLOCKS_PER_SEC; double mergeTime = getMergeTime(); printf("GPU execution time: %0.3f ms \n", bucketsort_msec+mergesort_msec+bucketTime+mergeTime); printf(" --Bucketsort execution time: %0.3f ms \n", bucketsort_msec+bucketTime); printf(" --Mergesort execution time: %0.3f ms \n", mergesort_msec+mergeTime); #endif #ifdef VERIFY clock_t cpu_start = clock(), cpu_diff; qsort(cpu_odata, numElements, sizeof(float), compare); cpu_diff = clock() - cpu_start; int cpu_msec = cpu_diff * 1000 / CLOCKS_PER_SEC; printf("CPU execution time: %d ms \n", cpu_msec); printf("Checking result..."); // Result checking int count = 0; for(int i = 0; i < numElements; i++){ if(cpu_odata[i] != gpu_odata[i]) { printf("Sort missmatch on element %d: \n", i); printf("CPU = %f : GPU = %f\n", cpu_odata[i], gpu_odata[i]); count++; break; } } if(count == 0) printf("PASSED.\n"); else printf("FAILED.\n"); #endif #ifdef OUTPUT FILE *tp1; const char filename3[]="./hybridoutput.txt"; tp1 = fopen(filename3,"w"); for(int i = 0; i < SIZE; i++) { fprintf(tp1,"%f ",cpu_idata[i]); } fclose(tp1); #endif // printf("%d \n",cpu_odata[1]); // int summy = 0; // for(int i =0; i < HISTOGRAM_SIZE; i++) // summy+=cpu_odata[i]; // printf("%d \n", summy); return 0; }
inline Mln* prepare(std::vector<ListNode*>& lists) { ListNodeSort lns; return bucketSort(lists); }
int main() { const unsigned long int N = 100; std::cout << N << std::endl; srand((int) time(NULL)); int * a = new int[N]; int * aux = new int[N]; copiaNumeros(a,N); for (int i = 0; i < N; ++i) { aux[i] = a[i]; } auto begin = std::chrono::high_resolution_clock::now(); bubbleSort(a, N); auto end = std::chrono::high_resolution_clock::now(); auto tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Bubble: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); cocktailSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Cocktail: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); insertionSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Insertion: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); bucketSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Bucket: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); countingSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Counting: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); mergeSort(a, 0, N-1, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Merge: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); radixSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Radix: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); shellSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Shell: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); selectionSort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Selection: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); quickSort(a, 0, N-1); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Quick: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); heap_sort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Heap: "<<tiempo.count()<<std::endl; for( int i = 0; i<N; i++) { a[i] = aux[i]; } begin = std::chrono::high_resolution_clock::now(); binarySort(a, N); end = std::chrono::high_resolution_clock::now(); tiempo = std::chrono::duration_cast<std::chrono::microseconds>(end-begin); std::cout<<"Binary: "<<tiempo.count()<<std::endl; delete a; delete aux; return 0; }