int main(int argc, char *argv[]) { int n = ARRAY_SIZE; int *array; HANDLE(!(array = malloc(n * sizeof(int)))); monitor *m; make_array(&array, n, ARRAY_ALGO, RANGE); NOTIFY(print_array(&array, n, OUT_LINE_SIZE)); m = monitor_init(SELF); monitor_start(m); csort(array, n); monitor_end(m); NOTIFY(print_array(&array, n, OUT_LINE_SIZE)); assert(verify(&array, n)); free(array); monitor_print_stats(m, VERBOSE); monitor_free(m); return 0; }
int main(void) { char str[512], buf[512]; memset(str, 0, 512); memset(buf, 0, 512); scanf("%[^\n]s", str); csort(str, buf); printf("%s", buf); return 0; }
int main(void) { int arr[N][M]; int i; init(arr); putit(arr); printf("\n"); for (i=0; i<N; i++) csort(arr[i], M); putit(arr); printf("\n"); csort((int *)arr, N*M); putit(arr); return 0; }
int solve ( ) { csort ( ); //*sort* result = 0; // zerowanie for ( int i = 0; i < numOfNumbers; i ++ ) { min = 1010; // maksimum sum = length = 0; // zerowanie if ( ! visited [ i ] ) // jeśli nie odwiedzony if ( dfs ( i ) && length > 1) // to odwiedź, i jeśli długość cyklu > 1 dodaj koszt result += ( minElement * ( length + 1 ) + sum + min < min * ( length - 2 ) + sum ) ? minElement * ( length + 1 ) + sum + min : min * ( length - 2 ) + sum; // z tym że można go nieco ulepszyć jeśli koszt zamiany minimum z cyklu i minimum ogólnego i z powrotem będzie dawał jakiś zysk } return result; //wynik }
int main(){ int arr1[10]; refillArray(arr1,10); shellSort(arr1, 10); refillArray(arr1,20); qSort(arr1, 0, 9); refillArray(arr1,30); csort(arr1,10,31); return 0; }
int main(){ int i; int v[] = {7, 2, 7, 2, 7, 2, 7, 2, 7, 2}; int *a = csort(v, 8); int *b = dsort(v, 8, 0, 13); for(i = 0; i < 8; i++) printf("%d ", a[i]); putchar('\n'); for(i = 0; i < 8; i++) printf("%d ", b[i]); free(a); free(b); scanf("%d", &i); return 0; }
int main(void) { int num[N]; init(num); putit(num); bsort(num); putit(num); printf("\nwait 2s\n\n"); sleep(2); init(num); putit(num); csort(num); putit(num); return 0; }
int main(int argc, char **argv) { char src[100000]; gets(src); int len, n = 1, i, k = 0; len = strlen(src); for(i = 0; i < len; i++) { if (src[i] == ' ' && src[i+1] != ' ') n++; if (src [i] != ' ') k++; } if (src[0] == ' ') n--; if (src[len-1] == ' ') n--; char *dest = (char*)malloc((k+n)*sizeof(char)); csort(src, dest); //printf("%d - %d\n", n, k); free(dest); return 0; }
/* * convert switch of the form * switch v := i.(type) { case t1: ..; case t2: ..; } * into if statements */ static void typeswitch(Node *sw) { Node *def; NodeList *cas, *hash; Node *a, *n; Case *c, *c0, *c1; int ncase; Type *t; Val v; if(sw->ntest == nil) return; if(sw->ntest->right == nil) { setlineno(sw); yyerror("type switch must have an assignment"); return; } walkexpr(&sw->ntest->right, &sw->ninit); if(!istype(sw->ntest->right->type, TINTER)) { yyerror("type switch must be on an interface"); return; } cas = nil; /* * predeclare temporary variables * and the boolean var */ facename = temp(sw->ntest->right->type); a = nod(OAS, facename, sw->ntest->right); typecheck(&a, Etop); cas = list(cas, a); casebody(sw, facename); boolname = temp(types[TBOOL]); typecheck(&boolname, Erv); hashname = temp(types[TUINT32]); typecheck(&hashname, Erv); t = sw->ntest->right->type; if(isnilinter(t)) a = syslook("efacethash", 1); else a = syslook("ifacethash", 1); argtype(a, t); a = nod(OCALL, a, N); a->list = list1(facename); a = nod(OAS, hashname, a); typecheck(&a, Etop); cas = list(cas, a); c0 = mkcaselist(sw, Stype); if(c0 != C && c0->type == Tdefault) { def = c0->node->right; c0 = c0->link; } else { def = nod(OBREAK, N, N); } /* * insert if statement into each case block */ for(c=c0; c!=C; c=c->link) { n = c->node; switch(c->type) { case Ttypenil: v.ctype = CTNIL; a = nod(OIF, N, N); a->ntest = nod(OEQ, facename, nodlit(v)); typecheck(&a->ntest, Erv); a->nbody = list1(n->right); // if i==nil { goto l } n->right = a; break; case Ttypevar: case Ttypeconst: n->right = typeone(n); break; } } /* * generate list of if statements, binary search for constant sequences */ while(c0 != C) { if(c0->type != Ttypeconst) { n = c0->node; cas = list(cas, n->right); c0=c0->link; continue; } // identify run of constants c1 = c = c0; while(c->link!=C && c->link->type==Ttypeconst) c = c->link; c0 = c->link; c->link = nil; // sort by hash c1 = csort(c1, typecmp); // for debugging: linear search if(0) { for(c=c1; c!=C; c=c->link) { n = c->node; cas = list(cas, n->right); } continue; } // combine adjacent cases with the same hash ncase = 0; for(c=c1; c!=C; c=c->link) { ncase++; hash = list1(c->node->right); while(c->link != C && c->link->hash == c->hash) { hash = list(hash, c->link->node->right); c->link = c->link->link; } c->node->right = liststmt(hash); } // binary search among cases to narrow by hash cas = list(cas, typebsw(c1, ncase)); } if(nerrors == 0) { cas = list(cas, def); sw->nbody = concat(cas, sw->nbody); sw->list = nil; walkstmtlist(sw->nbody); } }
/* * normal (expression) switch. * rebulid case statements into if .. goto */ static void exprswitch(Node *sw) { Node *def; NodeList *cas; Node *a; Case *c0, *c, *c1; Type *t; int arg, ncase; casebody(sw, N); arg = Snorm; if(isconst(sw->ntest, CTBOOL)) { arg = Strue; if(sw->ntest->val.u.bval == 0) arg = Sfalse; } walkexpr(&sw->ntest, &sw->ninit); t = sw->type; if(t == T) return; /* * convert the switch into OIF statements */ exprname = N; cas = nil; if(arg != Strue && arg != Sfalse) { exprname = temp(sw->ntest->type); cas = list1(nod(OAS, exprname, sw->ntest)); typechecklist(cas, Etop); } else { exprname = nodbool(arg == Strue); } c0 = mkcaselist(sw, arg); if(c0 != C && c0->type == Tdefault) { def = c0->node->right; c0 = c0->link; } else { def = nod(OBREAK, N, N); } loop: if(c0 == C) { cas = list(cas, def); sw->nbody = concat(cas, sw->nbody); sw->list = nil; walkstmtlist(sw->nbody); return; } // deal with the variables one-at-a-time if(!okforcmp[t->etype] || c0->type != Texprconst) { a = exprbsw(c0, 1, arg); cas = list(cas, a); c0 = c0->link; goto loop; } // do binary search on run of constants ncase = 1; for(c=c0; c->link!=C; c=c->link) { if(c->link->type != Texprconst) break; ncase++; } // break the chain at the count c1 = c->link; c->link = C; // sort and compile constants c0 = csort(c0, exprcmp); a = exprbsw(c0, ncase, arg); cas = list(cas, a); c0 = c1; goto loop; }
static Case* mkcaselist(Node *sw, int arg) { Node *n; Case *c, *c1, *c2; NodeList *l; int ord; c = C; ord = 0; for(l=sw->list; l; l=l->next) { n = l->n; c1 = mal(sizeof(*c1)); c1->link = c; c = c1; ord++; if((uint16)ord != ord) fatal("too many cases in switch"); c->ordinal = ord; c->node = n; if(n->left == N) { c->type = Tdefault; continue; } switch(arg) { case Stype: c->hash = 0; if(n->left->op == OLITERAL) { c->type = Ttypenil; continue; } if(istype(n->left->type, TINTER)) { c->type = Ttypevar; continue; } c->hash = typehash(n->left->type); c->type = Ttypeconst; continue; case Snorm: case Strue: case Sfalse: c->type = Texprvar; c->hash = typehash(n->left->type); switch(consttype(n->left)) { case CTFLT: case CTINT: case CTRUNE: case CTSTR: c->type = Texprconst; } continue; } } if(c == C) return C; // sort by value and diagnose duplicate cases switch(arg) { case Stype: c = csort(c, typecmp); for(c1=c; c1!=C; c1=c1->link) { for(c2=c1->link; c2!=C && c2->hash==c1->hash; c2=c2->link) { if(c1->type == Ttypenil || c1->type == Tdefault) break; if(c2->type == Ttypenil || c2->type == Tdefault) break; if(!eqtype(c1->node->left->type, c2->node->left->type)) continue; yyerrorl(c2->node->lineno, "duplicate case %T in type switch\n\tprevious case at %L", c2->node->left->type, c1->node->lineno); } } break; case Snorm: case Strue: case Sfalse: c = csort(c, exprcmp); for(c1=c; c1->link!=C; c1=c1->link) { if(exprcmp(c1, c1->link) != 0) continue; setlineno(c1->link->node); yyerror("duplicate case %N in switch\n\tprevious case at %L", c1->node->left, c1->node->lineno); } break; } // put list back in processing order c = csort(c, ordlcmp); return c; }
static Case* csort(Case *l, int(*f)(Case*, Case*)) { Case *l1, *l2, *le; if(l == C || l->link == C) return l; l1 = l; l2 = l; for(;;) { l2 = l2->link; if(l2 == C) break; l2 = l2->link; if(l2 == C) break; l1 = l1->link; } l2 = l1->link; l1->link = C; l1 = csort(l, f); l2 = csort(l2, f); /* set up lead element */ if((*f)(l1, l2) < 0) { l = l1; l1 = l1->link; } else { l = l2; l2 = l2->link; } le = l; for(;;) { if(l1 == C) { while(l2) { le->link = l2; le = l2; l2 = l2->link; } le->link = C; break; } if(l2 == C) { while(l1) { le->link = l1; le = l1; l1 = l1->link; } break; } if((*f)(l1, l2) < 0) { le->link = l1; le = l1; l1 = l1->link; } else { le->link = l2; le = l2; l2 = l2->link; } } le->link = C; return l; }
/* * convert switch of the form * switch v := i.(type) { case t1: ..; case t2: ..; } * into if statements */ void typeswitch(Node *sw) { Node *def; NodeList *cas; Node *a; Case *c, *c0, *c1; int ncase; Type *t; if(sw->ntest == nil) return; if(sw->ntest->right == nil) { setlineno(sw); yyerror("type switch must have an assignment"); return; } walkexpr(&sw->ntest->right, &sw->ninit); if(!istype(sw->ntest->right->type, TINTER)) { yyerror("type switch must be on an interface"); return; } cas = nil; /* * predeclare temporary variables * and the boolean var */ facename = nod(OXXX, N, N); tempname(facename, sw->ntest->right->type); a = nod(OAS, facename, sw->ntest->right); typecheck(&a, Etop); cas = list(cas, a); casebody(sw, facename); boolname = nod(OXXX, N, N); tempname(boolname, types[TBOOL]); typecheck(&boolname, Erv); hashname = nod(OXXX, N, N); tempname(hashname, types[TUINT32]); typecheck(&hashname, Erv); t = sw->ntest->right->type; if(isnilinter(t)) a = syslook("efacethash", 1); else a = syslook("ifacethash", 1); argtype(a, t); a = nod(OCALL, a, N); a->list = list1(facename); a = nod(OAS, hashname, a); typecheck(&a, Etop); cas = list(cas, a); c0 = mkcaselist(sw, Stype); if(c0 != C && c0->type == Tdefault) { def = c0->node->right; c0 = c0->link; } else { def = nod(OBREAK, N, N); } loop: if(c0 == C) { cas = list(cas, def); sw->nbody = concat(cas, sw->nbody); sw->list = nil; walkstmtlist(sw->nbody); return; } // deal with the variables one-at-a-time if(c0->type != Ttypeconst) { a = typebsw(c0, 1); cas = list(cas, a); c0 = c0->link; goto loop; } // do binary search on run of constants ncase = 1; for(c=c0; c->link!=C; c=c->link) { if(c->link->type != Ttypeconst) break; ncase++; } // break the chain at the count c1 = c->link; c->link = C; // sort and compile constants c0 = csort(c0, typecmp); a = typebsw(c0, ncase); cas = list(cas, a); c0 = c1; goto loop; }
void CountingSort::sort(std::vector<unsigned int>& elements) { std::vector<unsigned int> b(elements.size()); csort(elements, b, _vmax); elements = b; }
int rcsort(t_list *a, t_list *b) { return (csort(a, b) * -1); }