int main() { srand(time(NULL)); int n = 0, m = 4; int a[n], b[m]; genArray(a, n); genArray(b, m); sort(a, a+n); sort(b, b+m); printArray(a, n); printArray(b, m); int c[n+m]; int p = 0, p1 = 0, p2 = 0; while(p1 < n || p2 < m){ if(p1 >= n || (p2 < m && b[p2] < a[p1])) c[p++] = b[p2++]; else c[p++] = a[p1++]; } printArray(c, n+m); // cout<<c[n]<<endl; cout<<findMedianSortedArrays(a, n, b, m)<<endl; return 0; }
// 配列の初期化コード生成 static void genList0(int node, int dim, boolean byt) { int typ = syGetType(node); int lVal = syGetLVal(node); int rVal = syGetRVal(node); if (typ==SySEMI) { // リストの途中 genList0(lVal, dim, byt); // 左から genList0(rVal, dim, byt); // 右の順番で辿る } else if (dim==0) { // コード生成すべき深さ if (byt) { // バイト配列の末端 if (syGetType(node)!=SyCNST) error("バグ...genList0"); // 定数だけ OK vmDbCns(syGetLVal(node)); // DB XX を生成 } else { // ワード配列(ポインタ含む) genDW(node); // DW XX を生成 } } else if (typ==SyLIST) { // 初期化配列を発見 if (dim==1) { // コード生成直前 int lab = newLab(); // 配列の先頭に sySetRVal(node, lab); // ラベルを生成し printLab(lab); // rVal に記録 } genList0(lVal, dim-1, rVal==1); // 配列の本体を辿る } else if (typ==SyARRY) { // 非初期化配列が含まれていた if (dim==1) genArray(node); // 非初期化配列生成 } }
Iterator *ts_it_create(TreeSet *ts) { Iterator *it = NULL; void **tmp = genArray(ts); if (tmp != NULL) { it = it_create(ts->size, tmp); if (it == NULL) free(tmp); } return it; }
// 初期化データの生成 void genData(int idx) { int root = syGetRoot(); int typ = syGetType(root); int dim = ntGetDim(idx); if (typ==SyLIST) genList(root,dim); // 初期化された配列 else if (typ==SyARRY) genArray(root); // 非初期化配列[array(n1,...)] vmNam(idx); // 次のような出力をする genDW(root); // Name DW xx }
Iterator *ll_it_create(LinkedList *ll) { Iterator *it = NULL; void **tmp = genArray(ll); if (tmp != NULL) { it = it_create(ll->size, tmp); if (it == NULL) free(tmp); } return it; }
int main(int argc, char * argv[]){ int * array = 0; int control, n, min, max; clock_t start, stop; double t = 0.0; /* Start timer */ start = clock(); if((argc == 4) && (atoi(argv[1]) > 0)){ n = atoi(argv[1]); min = atoi(argv[2]); max = atoi(argv[3]); if((n == 1) || (min == max)){ control = 1; } else{ array = genArray(n, min, max); #ifdef DEBUG fprintArray(array, n, "antes.txt"); #endif SoaresCountingSort(array, n, min, max); #ifdef DEBUG fprintArray(array, n, "depois.txt"); #endif control = ControlSort(array, n); } if(control) printf("O array esta ordenado."); else printf("O array nao está ordenado."); } else fprintf(stderr, "Parameters Error."); free(array); /* Stop timer */ stop = clock(); t = (double) (stop-start)/CLOCKS_PER_SEC; printf("\n\nRun time: %f sec\n", t); return 0; }
void checkArray(int A[], int n, int s) { int *B; // temporary buffer int nerrs; #ifdef DEBUG int checkPre, checkPost; // checksums for unsorted and sorted // arrays #endif int i; B = (int *) malloc(n * sizeof(int)); assert(B != NULL); genArray(B, n, s); #ifdef DEBUG checkPre = 0; for (i = 0; i < n; i++) checkPre += B[i]; #endif #ifdef CHECK_PIVOT_QUALITY printf("average quick sort call depth = %ld (ideal=%d)\n", qCalls == 0 ? 0 : qDepthSum / qCalls, lg2(n)); #endif qsort(B, n, sizeof(int), compint); #ifdef DEBUG checkPost = n > 0 ? B[0] : 0; nerrs = 0; for (i = 1; i < n; i++) { nerrs += (B[i - 1] > B[i]); checkPost += B[i]; } if (nerrs) printf("Error in sort() detected: %d out-of-order elements\n", nerrs); if (checkPost != checkPre) printf("Error in sort() detected: unexpected values present\n"); #endif nerrs = 0; for (i = 0; i < n; i++) nerrs += (B[i] != A[i]); printf("check of sort of array of length %d from seed %d: %d errors\n", n, s, nerrs); free(B); } // checkArray()
int main(int argc, char* argv[]) { const int SIZE = 1 << 8; const int NPOT = SIZE - 3; int a[SIZE], b[SIZE], c[SIZE]; float ms_time = 0.0f; // Scan tests printf("\n"); printf("****************\n"); printf("** SCAN TESTS **\n"); printf("****************\n"); genArray(SIZE - 1, a, 50); // Leave a 0 at the end to test that edge case printArray(SIZE, a, true); zeroArray(SIZE, b); printDesc("cpu scan, power-of-two"); ms_time = StreamCompaction::CPU::scan(SIZE, b, a); printf("CPU execution time for scan: %.5fms\n", ms_time); printArray(SIZE, b, true); zeroArray(SIZE, c); printDesc("cpu scan, non-power-of-two"); ms_time = StreamCompaction::CPU::scan(NPOT, c, a); printf("CPU execution time for scan: %.5fms\n", ms_time); printArray(NPOT, b, true); printCmpResult(NPOT, b, c); zeroArray(SIZE, c); printDesc("naive scan, power-of-two"); StreamCompaction::Naive::scan(SIZE, c, a); //printArray(SIZE, c, true); printCmpResult(SIZE, b, c); zeroArray(SIZE, c); printDesc("naive scan, non-power-of-two"); StreamCompaction::Naive::scan(NPOT, c, a); //printArray(SIZE, c, true); printCmpResult(NPOT, b, c); zeroArray(SIZE, c); printDesc("work-efficient scan, power-of-two"); ms_time = StreamCompaction::Efficient::scan(SIZE, c, a); printf("CUDA execution time for work efficient scan: %.5fms\n", ms_time); //printArray(SIZE, c, true); printCmpResult(SIZE, b, c); zeroArray(SIZE, c); printDesc("work-efficient scan, non-power-of-two"); ms_time = StreamCompaction::Efficient::scan(NPOT, c, a); printf("CUDA execution time for work efficient scan: %.5fms\n", ms_time); //printArray(NPOT, c, true); printCmpResult(NPOT, b, c); zeroArray(SIZE, c); printDesc("thrust scan, power-of-two"); StreamCompaction::Thrust::scan(SIZE, c, a); //printArray(SIZE, c, true); printCmpResult(SIZE, b, c); zeroArray(SIZE, c); printDesc("thrust scan, non-power-of-two"); StreamCompaction::Thrust::scan(NPOT, c, a); //printArray(NPOT, c, true); printCmpResult(NPOT, b, c); printf("\n"); printf("*****************************\n"); printf("** STREAM COMPACTION TESTS **\n"); printf("*****************************\n"); // Compaction tests genArray(SIZE - 1, a, 4); // Leave a 0 at the end to test that edge case printArray(SIZE, a, true); int count, expectedCount, expectedNPOT; zeroArray(SIZE, b); printDesc("cpu compact without scan, power-of-two"); count = StreamCompaction::CPU::compactWithoutScan(SIZE, b, a); expectedCount = count; printArray(count, b, true); printCmpLenResult(count, expectedCount, b, b); zeroArray(SIZE, c); printDesc("cpu compact without scan, non-power-of-two"); count = StreamCompaction::CPU::compactWithoutScan(NPOT, c, a); expectedNPOT = count; printArray(count, c, true); printCmpLenResult(count, expectedNPOT, b, c); zeroArray(SIZE, c); printDesc("cpu compact with scan"); count = StreamCompaction::CPU::compactWithScan(SIZE, c, a); printArray(count, c, true); printCmpLenResult(count, expectedCount, b, c); zeroArray(SIZE, c); printDesc("work-efficient compact, power-of-two"); count = StreamCompaction::Efficient::compact(SIZE, c, a); //printArray(count, c, true); printCmpLenResult(count, expectedCount, b, c); zeroArray(SIZE, c); printDesc("work-efficient compact, non-power-of-two"); count = StreamCompaction::Efficient::compact(NPOT, c, a); //printArray(count, c, true); printCmpLenResult(count, expectedNPOT, b, c); }
int main(int argc, char* argv[]) { const int SIZE = 1 << 10; const int NPOT = SIZE - 3; int a[SIZE], b[SIZE], c[SIZE]; // Scan tests printf("\n"); printf("****************\n"); printf("** SCAN TESTS **\n"); printf("****************\n"); genArray(SIZE - 1, a, 50); // Leave a 0 at the end to test that edge case a[SIZE - 1] = 0; printArray(SIZE, a, true); zeroArray(SIZE, b); printDesc("cpu scan, power-of-two"); StreamCompaction::CPU::scan(SIZE, b, a); //printArray(SIZE, b, true); zeroArray(SIZE, c); printDesc("cpu scan, non-power-of-two"); StreamCompaction::CPU::scan(NPOT, c, a); //printArray(NPOT, b, true); printCmpResult(NPOT, b, c); zeroArray(SIZE, c); printDesc("naive scan, power-of-two"); StreamCompaction::Naive::scan(SIZE, c, a); //printArray(SIZE, c, true); printCmpResult(SIZE, b, c); zeroArray(SIZE, c); printDesc("naive scan, non-power-of-two"); StreamCompaction::Naive::scan(NPOT, c, a); //printArray(NPOT, c, true); printCmpResult(NPOT, b, c); zeroArray(SIZE, c); printDesc("work-efficient scan, power-of-two"); StreamCompaction::Efficient::scan(SIZE, c, a); //printArray(SIZE, c, true); printCmpResult(SIZE, b, c); zeroArray(SIZE, c); printDesc("work-efficient scan, non-power-of-two"); StreamCompaction::Efficient::scan(NPOT, c, a); //printArray(NPOT, c, true); printCmpResult(NPOT, b, c); zeroArray(SIZE, c); printDesc("thrust scan, power-of-two"); StreamCompaction::Thrust::scan(SIZE, c, a); //printArray(SIZE, c, true); printCmpResult(SIZE, b, c); zeroArray(SIZE, c); printDesc("thrust scan, non-power-of-two"); StreamCompaction::Thrust::scan(NPOT, c, a); //printArray(NPOT, c, true); printCmpResult(NPOT, b, c); //* printf("\n"); printf("*****************************\n"); printf("** STREAM COMPACTION TESTS **\n"); printf("*****************************\n"); // Compaction tests genArray(SIZE - 1, a, 4); // Leave a 0 at the end to test that edge case printArray(SIZE, a, true); a[SIZE - 1] = 0; int count, expectedCount, expectedNPOT; zeroArray(SIZE, b); printDesc("cpu compact without scan, power-of-two"); count = StreamCompaction::CPU::compactWithoutScan(SIZE, b, a); expectedCount = count; printArray(count, b, true); printCmpLenResult(count, expectedCount, b, b); zeroArray(SIZE, c); printDesc("cpu compact without scan, non-power-of-two"); count = StreamCompaction::CPU::compactWithoutScan(NPOT, c, a); expectedNPOT = count; printArray(count, c, true); printCmpLenResult(count, expectedNPOT, b, c); zeroArray(SIZE, c); printDesc("cpu compact with scan"); count = StreamCompaction::CPU::compactWithScan(SIZE, c, a); printArray(count, c, true); printCmpLenResult(count, expectedCount, b, c); zeroArray(SIZE, c); printDesc("work-efficient compact, power-of-two"); printArray(SIZE, a, true);//delete count = StreamCompaction::Efficient::compact(SIZE, c, a); printArray(count, c, true); printCmpLenResult(count, expectedCount, b, c); zeroArray(SIZE, c); printDesc("work-efficient compact, non-power-of-two"); printArray(SIZE, a, true);//delete count = StreamCompaction::Efficient::compact(NPOT, c, a); printArray(count, c, true); printCmpLenResult(count, expectedNPOT, b, c); printf("\n"); printf("*****************************\n"); printf("** Radix Sort **\n"); printf("*****************************\n"); a[0] = 4; a[1] = 7; a[2] = 2; a[3] = 6; a[4] = 3; a[5] = 5; a[6] = 1; a[7] = 0; zeroArray(8, c); printDesc("Radix Sort, power-of-two"); printArray(8, a, true);//delete StreamCompaction::RadixSort::sort(8, c, a); printArray(8, c, true); //printCmpResult(SIZE, b, c);*/ std::cin.get(); }