int checkSimpleSort() { { char text [100]; uint V[100], i; uint len=100;; while (len !=0) { printf("\nIntro number of integers integer: "); fgets(text, 100, stdin); len = atoi(text); if (!len) break; i=0; while(i<len) { printf("Intro integer [%d]: ",i); fgets(text, 100, stdin); V[i]=atoi(text); i++; } printf("\nWill sort vector: "); for (i=0;i<len;i++) printf("[%d]",V[i]); simplesort(V,len); printf("\nSorted vector: "); for (i=0;i<len;i++) printf("[%d]",V[i]); printf("\n"); } } }
/** Intersection of lists of occurrences **/ int svs (void *index, uint *idsIn, uint nids, uint **list, uint *len) { //sorting ids ==> lists are sorted by frequency !! uint i; uint *ids = (uint *)malloc (sizeof(uint) * nids); for (i=0;i<nids;i++) ids[i]=idsIn[i]; simplesort(ids,nids); // ==> change to sort by lenList !! /**starting SVS ------------------------ */ //Creating the first list; { uint i,j,n; uint *firstlist; uint matches; uncompressList (index, ids[0], list, len); firstlist =*list; n=*len; uint result; for (i=1; i<nids; i++) { //the nids-1 remaining lists matches =0; for (j=0;j<n;j++) { result=fsearch (index, firstlist[j] , ids[i]); //result=fsearchUNCOMPRESS (index, firstlist[j] , ids[i]); if (result) { //if (fsearch (index, firstlist[j] , ids[i])) { //if (fsearchUNCOMPRESS (index, firstlist[j] , ids[i])) { firstlist[matches++]= firstlist[j]; //printf("\nFSEARCH de posicion %d = acierto contra palabra ",firstlist[j]); } //else printf("\nFSEARCH de posicion %d = fallo contra palabra ",firstlist[j]); } n=matches; if (n ==0) { free(*list); *list=NULL;*len =0; break; } } *len =n; free(ids); } return 0; }
int main() { setlocale(LC_ALL, "Russian"); printf("This is assorting of %s\n", FILEREAD); int len = 0; char* buffer; if (readfileD(FILEREAD, &buffer, &len)) { printf("INPUT ERROR\n"); } int nline = 0; char** text = split_linesD(buffer, len, &nline); simplesort(text, nline); make_file(FILEWRITE, text, nline); free(text[0]); free(text); return OK; }
int sradixsort(const u_char **a, int n, const u_char *tab, u_int endch) { const u_char *tr, **ta; int c; u_char tr0[256]; SETUP; if (n < THRESHOLD) simplesort(a, n, 0, tr, endch); else { if ((ta = malloc(n * sizeof(a))) == NULL) return (-1); r_sort_b(a, ta, n, 0, tr, endch); free(ta); } return (0); }
void radix_sort(RECHEADER **a, RECHEADER **ta, int n) { u_int count[256], nc, bmin; u_int c; RECHEADER **ak, **tai, **lim; RECHEADER *hdr; int stack_size = 512; stack *s, *sp, *sp0, *sp1, temp; RECHEADER **top[256]; u_int *cp, bigc; int data_index = 0; if (n < THRESHOLD && !DEBUG('r')) { simplesort(a, n, 0); return; } s = emalloc(stack_size * sizeof *s); memset(&count, 0, sizeof count); /* Technically 'top' doesn't need zeroing */ memset(&top, 0, sizeof top); sp = s; push(a, n, data_index); while (!empty(s)) { pop(a, n, data_index); if (n < THRESHOLD && !DEBUG('r')) { simplesort(a, n, data_index); continue; } /* Count number of times each 'next byte' occurs */ nc = 0; bmin = 255; lim = a + n; for (ak = a, tai = ta; ak < lim; ak++) { hdr = *ak; if (data_index >= hdr->keylen) { /* Short key, copy to start of output */ if (UNIQUE && a != sp->sa) /* Stop duplicate being written out */ hdr->keylen = -1; *a++ = hdr; n--; continue; } /* Save in temp buffer for distribute */ *tai++ = hdr; c = hdr->data[data_index]; if (++count[c] == 1) { if (c < bmin) bmin = c; nc++; } } /* * We need save the bounds for each 'next byte' that * occurs more so we can sort each block. */ if (sp + nc > s + stack_size) { stack_size *= 2; sp1 = erealloc(s, stack_size * sizeof *s); sp = sp1 + (sp - s); s = sp1; } /* Minor optimisation to do the largest set last */ sp0 = sp1 = sp; bigc = 2; /* Convert 'counts' positions, saving bounds for later sorts */ ak = a; for (cp = count + bmin; nc > 0; cp++) { while (*cp == 0) cp++; if ((c = *cp) > 1) { if (c > bigc) { bigc = c; sp1 = sp; } push(ak, c, data_index+1); } ak += c; top[cp-count] = ak; *cp = 0; /* Reset count[]. */ nc--; } swap(*sp0, *sp1, temp); for (ak = ta+n; --ak >= ta;) /* Deal to piles. */ *--top[(*ak)->data[data_index]] = *ak; } free(s); }
/* Stable sort, requiring additional memory. */ static void r_sort_b(u_char const **a, u_char const **ta, int n, int i, u_char const *tr, u_int endch) { static int count[256]; static u_int nc; static u_int bmin; u_int c; u_char const **ak, **ai; stack s[512], *sp, *sp0, *sp1, temp; u_char const **top[256]; int *cp; u_int bigc; sp = s; push(a, n, i); while (!empty(s)) { pop(a, n, i); if (n < THRESHOLD) { simplesort(a, n, i, tr, endch); continue; } if (nc == 0) { bmin = 255; for (ak = a + n; --ak >= a;) { c = tr[(*ak)[i]]; if (++count[c] == 1 && c != endch) { if (c < bmin) bmin = c; nc++; } } if (sp + nc > s + SIZE) { r_sort_b(a, ta, n, i, tr, endch); continue; } } sp0 = sp1 = sp; bigc = 2; if (endch == 0) { top[0] = ak = a + count[0]; count[0] = 0; } else { ak = a; top[255] = a + n; count[255] = 0; } for (cp = count + bmin; nc > 0; cp++) { while (*cp == 0) cp++; if ((c = *cp) > 1) { if (c > bigc) { bigc = c; sp1 = sp; } push(ak, c, i+1); } top[cp-count] = ak += c; *cp = 0; /* Reset count[]. */ nc--; } swap(*sp0, *sp1, temp); for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */ *--ak = *--ai; for (ak = ta+n; --ak >= ta;) /* Deal to piles. */ *--top[tr[(*ak)[i]]] = *ak; } }
/* Unstable, in-place sort. */ static void r_sort_a(u_char const **a, int n, int i, u_char const *tr, u_int endch) { static u_int count[256]; static int nc; static u_int bmin; u_int c; u_char const **ak; u_char const *r; stack s[SIZE]; stack *sp; stack *sp0; stack *sp1; stack temp; u_int *cp; u_int bigc; u_char const **an; u_char const *t; u_char const **aj; u_char const **top[256]; /* Set up stack. */ sp = s; push(a, n, i); while (!empty(s)) { pop(a, n, i); if (n < THRESHOLD) { simplesort(a, n, i, tr, endch); continue; } an = a + n; /* Make character histogram. */ if (nc == 0) { bmin = 255; /* First occupied bin, excluding eos. */ for (ak = a; ak < an;) { c = tr[(*ak++)[i]]; if (++count[c] == 1 && c != endch) { if (c < bmin) bmin = c; nc++; } } if (sp + nc > s + SIZE) { /* Get more stack. */ r_sort_a(a, n, i, tr, endch); continue; } } /* * Set top[]; push incompletely sorted bins onto stack. * top[] = pointers to last out-of-place element in bins. * count[] = counts of elements in bins. * Before permuting: top[c-1] + count[c] = top[c]; * during deal: top[c] counts down to top[c-1]. */ sp0 = sp1 = sp; /* Stack position of biggest bin. */ bigc = 2; /* Size of biggest bin. */ if (endch == 0) /* Special case: set top[eos]. */ top[0] = ak = a + count[0]; else { ak = a; top[255] = an; } for (cp = count + bmin; nc > 0; cp++) { while (*cp == 0) /* Find next non-empty pile. */ cp++; if (*cp > 1) { if (*cp > bigc) { bigc = *cp; sp1 = sp; } push(ak, *cp, i+1); } top[cp-count] = ak += *cp; nc--; } swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */ /* * Permute misplacements home. Already home: everything * before aj, and in bin[c], items from top[c] on. * Inner loop: * r = next element to put in place; * ak = top[r[i]] = location to put the next element. * aj = bottom of 1st disordered bin. * Outer loop: * Once the 1st disordered bin is done, ie. aj >= ak, * aj<-aj + count[c] connects the bins in a linked list; * reset count[c]. */ for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0) for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);) swap(*ak, r, t); } }