// 原始快速排序,pivot取最左边的值 static void qsort1(int *data, int left, int right) { if (left < right) { int i = partition1(data, left, right); qsort1(data, left, i - 1); qsort1(data, i + 1, right); } }
void qsort1 (register int l, register int r, char *a[]) { register int i, j; char *w; char x[256]; i = l; j = r; strncpy (x, a[(l + r) / 2], 255); while (i <= j) { while (strcmp (a[i], x) < 0) i++; while (strcmp (a[j], x) > 0) j--; if (i <= j) { w = a[i]; a[i] = a[j]; a[j] = w; i++; j--; } } if (l < j) qsort1 (l, j, a); if (i < r) qsort1 (i, r, a); }
/* Simplest version, Lomuto partitioning */ void qsort1(int l, int u) { int i, m; if (l >= u) return; m = l; for (i = l+1; i <= u; i++) if (x[i] < x[l]) swap(++m, i); swap(l, m); qsort1(l, m-1); qsort1(m+1, u); }
/* qsort: sort v[left]...v[right] into increasing order */ void qsort1(void *v[], int left, int right,int (*comp)(void *, void *)) { int i, last; void swap(void *v[], int, int); if (left >= right)/* do nothing if array contains */ return;/* fewer than two elements */ swap(v, left, (left + right)/2); last = left; for (i = left+1; i <= right; i++) if ((*comp)(v[i], v[left]) < 0) swap(v, ++last, i); swap(v, left, last); qsort1(v, left, last-1, comp); qsort1(v, last+1, right, comp); }
int main(int argc, char * argv[]) { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(1, 100); std::vector<int> q(argc == 1 ? 10000000 : atoi(argv[1])); std::cout << "problem size is " << q.size() << std::endl; for(int i = 0; i < q.size(); i++) q[i] = dis(gen); if(getenv("DEBUG") != 0) qsortstat_debug(1); //std::vector<int> q0 = q; //std::sort(q0.begin(),q0.end()); //std::cout << "regular sort gives " << check(q0.begin(),q0.end()) << std::endl; omp_set_nested(1); omp_set_dynamic(1); // default dynamic is impl specific double t0,t1,t00; t00 = omp_get_wtime(); bb = q.begin(); #pragma omp parallel { #pragma omp single { t0 = omp_get_wtime(); qsort1(q.begin(),q.end()); } } t1 = omp_get_wtime(); std::cout << "parallel sort gives " << check(q.begin(),q.end()) << " total " << t1-t00 << " = net " << t1-t0 << " + setup " << t0-t00 << std::endl; return 0; }
void qsort1(void *v[], int left, int right, int (*comp)(void *, void *)) { int i, last; void swap(void *v[], int, int); if (left >= right) return; swap(v, left, (left+right)/2); last = left; for (i = left+1; i <= right; i++) if ((*comp)(v[i], v[left]) < 0) swap(v, ++last, i); swap(v, left, last); qsort1(v, left, last-1, comp); qsort1(v, last+1, right, comp); }
/*Program starts*/ int main(int argc, char *argv[]) { int nlines; /* number of input lines read */ int numeric = 0; /* 1 if numeric sort */ int reverse = 0; /* 1 if reverse sort */ int foldcase = 0; /* 1 if folding case */ int dirorder = 0; /* 1 if "directory order" comparision */ /* storage supplied by main */ char linebuf[MAXLEN * MAXLINES]; /* check options */ if (argc > 1) if (getopt(argv, &numeric, &reverse, &foldcase, &dirorder) == -1) return 2; /* read - sort - write */ if ((nlines = readlines(lineptr, linebuf, MAXLINES)) > 0) { qsort1((void **) lineptr, 0, nlines -1, (int (*)(void *, void *))(numeric ? numcmp : (foldcase ? strcasecmp : strcmp)), reverse, dirorder); writelines(lineptr, nlines); return 0; } else { printf("error: input too big to sort\n"); return 1; } return 0; }
/* qsort1: sort v[left]...v[right] into increasing order */ void qsort1(char v[][MAXLEN], int left, int right) { int i, last; void swap(char v[][MAXLEN], int i, int j); if (left >= right) /* do nothing if array contains */ return; /* fewer than two elements */ swap(v, left, (left + right)/2); last = left; for (i = left+1; i <= right; i++) if (strcmp(v[i], v[left]) < 0) swap(v, ++last, i); swap(v, left, last); qsort1(v, left, last-1); qsort1(v, last+1, right); }
/* qsort: sort v[left]...v[right] into increasing order */ void qsort1(struct employee * v[], int left, int right) { int i, last; void swap(struct employee * v[], int i, int j); if (left >= right) /* do nothing if array contains */ return; /* fewer than two elements */ swap(v, left, (left + right)/2); /* move partition elem */ last = left; /* to v[0] */ for (i = left + 1; i <= right; i++) /* partition */ if (v[i]->number < v[left]->number) swap(v, ++last, i); swap(v, left, last); /* restore partition elem */ qsort1(v, left, last-1); qsort1(v, last+1, right); }
void qsortp(void **base, /* base address */ size_t n, /* elements */ int (*f) (void *, void *)) /* compare function */ { qsort1(base, base + n, f); }
int main(){ ElemType userVal; printf("Please enter a set of integers.\n"); int checker; LIST* lst = lst_create(); while (1){ checker = scanf("%i", &userVal); if (checker == EOF || checker == 0) break; lst_push_back(lst, userVal); } printf("The list before the quick sort: \n"); lst_print(lst); printf("The list after the quick sort: \n"); qsort1(lst); lst_print(lst); lst_free(lst); return 0; }
void main() { int a[10]; srand(time(NULL)); for(int i=0;i<10;++i) { a[i]=rand()%100; } for(int i=0;i<10;++i) { printf("%d ",a[i]); } printf("\n"); qsort1(a,0,9); for(int i=0;i<10;++i) { printf("%d ",a[i]); } getch(); }
void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *)) { /* when nel is 0, the expression '(nel - 1) * width' is wrong */ if (!nel) return; qcompar = (int (*)(const char *, const char *)) compar; qsort1(base, (char *)base + (nel - 1) * width, width); }
void qsort1(void *v[], int left, int right, int (*comp)(void *, void *)) { int i, last; if (left >= right) /* recurssion termination condition */ return; swap(v, left, (left+right)/2); last = left; for (i=left+1 ; i<=right ; i++) if ((*comp)(v[i], v[left]) < 0) swap(v, ++last, i); swap(v, left, last); qsort1(v, left, last-1, comp); qsort1(v, last+1, right, comp); }
void qsort1(LIST* lst){ if(lst_length(lst) <= 1) return; ElemType pivot = lst_pop_front(lst); LIST* pivotList = lst_create(); lst_push_front(pivotList, pivot); LIST* lst2 = lst_filter_leq(lst, pivot); qsort1(lst); qsort1(lst2); lst_concat(lst2, pivotList); lst_concat(lst2, lst); lst_concat(lst, lst2); }
/* qsort: sort v[left] ... v[right] into increasing order */ void qsort1(void *v[], int left, int right, int (*comp)(void *, void *), int reverse, int dirorder) { int i, last; void swap(void *v[], int i, int j); char tvi[MAXLEN], tvleft[MAXLEN]; if (left >= right) /* do nothing if array contains */ return; /* fewer than two elements */ swap(v, left, (left + right) / 2); last = left; /* If dirorder then copy the original string to a temporary string with correct filtering */ if (dirorder == 1) strfcpy(tvleft, v[left]); /* Else copy verbatim */ else strncpy(tvleft, v[left], strlen(v[left]) + 1); for (i = left + 1; i <= right; i++) { /* for dirorder */ if (dirorder == 1) strfcpy(tvi, v[i]); /* else copy verbatim */ else strncpy(tvi, v[i], strlen(v[i]) + 1); if (reverse == 1) { if ((*comp)(tvi, tvleft) > 0) swap(v, ++last, i); } else if (reverse == 0) if ((*comp)(tvi, tvleft) < 0) swap(v, ++last, i); } swap(v, left, last); qsort1(v, left, last, comp, reverse, dirorder); qsort1(v, last+1, right, comp, reverse, dirorder); }
/** \param A array \param low low index \param high high index */ void qsort1(int* A, int low, int high) { int i = low; int j = high; int x = A[(low+high)/2]; /** selection of the reference element */ do { while(A[i] < x) ++i; /** search for items to move */ while(A[j] > x) --j; if(i <= j) { int d = A[i]; /** moving items */ A[i] = A[j]; A[j] = d; i++; j--; } } while(i < j); if(low < j) qsort1(A, low, j); /** recursive call to the remaining parts of the array */ if(i < high) qsort1(A, i, high); }
main(int argc, char *argv[]) { int nlines;/* number of input lines read */ int numeric = 0;/* 1 if numeric sort */ int option,c; while(--argc>0 && (*++argv)[0] == '-'){ c=(*argv)[1]; switch(c){ case 'n': option=INC; numeric=1; break; case 'r': option=DEC; break; case 'f': option=LOWER; break; default : printf("ERROR!!! invalid sorting option....\n"); break; } } if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { if(option==INC) qsort1((void**) lineptr, 0, nlines-1,(int (*)(void*,void*))numcmp); else if(option==LOWER) qsort1((void**) lineptr, 0, nlines-1,(int (*)(void*,void*))charcmp); else qsort1((void**) lineptr, 0, nlines-1,(int (*)(void*,void*))strcmp); writelines(lineptr, nlines,option); return 0; } else { printf("input too big to sort\n"); return 1; } }
int main(int argc, char *argv[]) { int nlines, option; int numeric = 0, reverse = 0; while(--argc > 0 && (*++argv)[0] == '-') while(option = *++argv[0]) { switch(option) { case 'n': numeric = 1; break; case 'r': reverse = 1; break; default: printf("error: invalid option\n"); break; } } printf("numeric %d\n", numeric); printf("reverse%d\n", reverse); if((nlines = readlines(lineptr, MAXLINES)) > 0) { if(reverse) qsort1((void **)lineptr, 0, nlines-1, (int (*) (void*, void*))(numeric ? numcmpr : strcmpr)); else qsort1((void **)lineptr, 0, nlines-1, (int (*) (void*, void*))(numeric ? numcmp : strcmp)); printf("\nafter sorting \n"); writelines(lineptr, nlines); return 0; } else { printf("Error: input too big to sort\n"); return 1; } }
int main (int argc, char *argv[]) { int nlines; int numeric = 0; if (argc > 1 && strcmp(argv[1], "-n") == 0) numeric = 1; if ((nlines = readlines(lineptr, MAXLINES)) >= 0) { qsort1((void **)lineptr, 0, nlines-1, (int (*)(void *, void *))(numeric ? numcmp : strcmp)); writelines(lineptr, nlines); return 0; } else { printf("input too big to sort\n"); return 1; } }
/* sort input lines */ int main() { int nlines; /* numbers of input lines read */ if ((nlines = readlines(list, MAXLINES)) >= 0) { clock_t start, finish; start = clock(); qsort1(list, 0, nlines-1); writelines(list, nlines); finish = clock(); printf("Tijd: %f", ( (double)finish - start ) ); return 0; } else { printf("error: input too big to sort\n"); return 1; } }
void qsort1(I b, I e) { if(b == e) return; qsortstat(b,e,bb); auto d = std::distance(b,e); auto p = *std::next(b, d/2); using W = decltype(p); // NOTE: see http://en.cppreference.com/w/cpp/algorithm/partition for alternative/better //C++14 auto mid1 = std::partition(b,e,[p] (const auto & em) { return em < p; }); //C++14 auto mid2 = std::partition(mid1,e,[p] (const auto & em) { return !(p < em); }); auto mid1 = std::partition(b,e,[p] (const W & em) { return em < p; }); auto mid2 = std::partition(mid1,e,[p] (const W & em) { return !(p < em); }); decltype(mid1) ps[2] = { b,mid2}; decltype(mid1) pe[2] = { mid1,e}; #pragma omp parallel for if (d > 20) for(int i = 0; i < 2; i++) qsort1(ps[i],pe[i]); // wait here implicit }
int main(int argc, char *argv[]) { int nlines = 0; int numeric = 0; if (argc > 1 && strcmp(argv[1], "-n") == 0) { numeric = 1; } if ((nlines = getlines(lineptr, MAXLINES)) >= 0) { qsort1((void **) lineptr, 0, nlines-1, (int (*)(void *, void *)) (numeric ? numcmp : strcmp) ); printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n"); printlines(lineptr, nlines); printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n"); } else { printf("error in reading lines \n"); } return 0; }
int main(int argc,char **argv) { struct dirent *dp; struct stat stp; DIR *dirp; char dir[512]; char argdir[512]; char buf[1024]; char str[128]; char *files[2048],*dirs[2048]; long ffsize=0,ddsize=0, i; off_t fsize[2048]; MPEG_HEAD finfo[2048]; int audioenc=0,fd=0; strcpy(argdir,"."); if (argc==2) strcpy(argdir,argv[1]); dirp = opendir (argdir); if (dirp == 0) { fprintf (stderr,"opendir \"%s\": %s\n", argdir, strerror (errno)); exit (errno); } ffsize=0; /* Read directory contents and mp3 file infos*/ while ((dp = readdir (dirp)) != NULL) { strcpy (dir, argdir); strcat (dir, "/"); strcat (dir, dp->d_name); if (strstr (dir, ".mp3")) audioenc = 1; else if (strstr (dir, ".ogg")) audioenc = 2; else audioenc = 0; //printf("DIR:%s\n",dir); lstat (dir, &stp); finfo[ffsize].fmode = stp.st_mode; if (S_ISLNK (finfo[ffsize].fmode)) { stat (dir, &stp); if (S_ISDIR (stp.st_mode)) finfo[ffsize].fmode = stp.st_mode; } if (!S_ISDIR (stp.st_mode)) { files[ffsize] = (char *) realloc ((char *) files[ffsize], strlen (dp->d_name) + 2); strcpy (files[ffsize], dp->d_name); fsize[ffsize] = stp.st_size; if (audioenc==1) { //mp3 if (!S_ISREG (stp.st_mode)) { finfo[ffsize].valid = 0; fd = -1; } else fd = open (dir, O_RDONLY); if (fd < 0) { finfo[ffsize].valid = 0; } else if (S_ISREG (stp.st_mode)) { get_mp3header (&finfo[ffsize], stp.st_size, fd); close (fd); } } if (audioenc==2) { //ogg if (!S_ISREG (stp.st_mode)) { finfo[ffsize].valid = 0; fd = -1; } else fd = open (dir, O_RDONLY); if (fd < 0) { finfo[ffsize].valid = 0; } else if (S_ISREG (stp.st_mode)) { oggnfo (&finfo[ffsize], stp.st_size, fd); finfo[ffsize].valid = 1; finfo[ffsize].enctype = 2; //ogg close (fd); } } else if (audioenc==0) // non-audio file finfo[ffsize].valid = 0; ffsize++; } else { dirs[ddsize] = (char *) realloc ((char *) dirs[ddsize], strlen (dp->d_name) + 2); strcpy (dirs[ddsize], dp->d_name); ddsize++; } } closedir (dirp); /*Sort dirs */ if ((ddsize - 1) > 1) qsort1 (0, ddsize - 1, dirs); /*Sort files */ if ((ffsize - 1) > 0) { qsort2 (0, ffsize - 1, files, fsize, finfo); } //print dirs for (i = 0; i < ddsize; i++) { printf ("d//////%s\n",dirs[i]); } //print files for (i = 0; i < ffsize; i++) { if (strstr (files[i], ".mp3")) { if (finfo[i].vbr) { strcpy(str,"VBR"); //printf("I:%ld\n",i); if ((finfo[i].mins+finfo[i].secs)) finfo[i].bitrate=fsize[i]*8/(60*finfo[i].mins+finfo[i].secs)/1000; else finfo[i].bitrate=0; } else strcpy(str,"Kbps"); sprintf (buf,"fmp3/%08ld/%02d/%02d/%03d/%3s/%s\n", fsize[i],finfo[i].mins,finfo[i].secs, finfo[i].bitrate,str,files[i]); } else if (strstr (files[i], ".ogg")) { //sprintf(buf,"%s\n",files[i]); strcpy(str,"VBR"); sprintf (buf,"fmp3/%08ld/%02d/%02d/%03d/%3s/%s\n", fsize[i],finfo[i].mins,finfo[i].secs, finfo[i].bitrate,str,files[i]); } else sprintf (buf,"f/%ld/////%s\n",fsize[i],files[i]); printf("%s",buf); } return 0; }
void sort_tab_number() { qsort1(tab_pt, 0, maxst - 1); }
void quicksort1(int *data, int n) { qsort1(data, 0, n - 1); }
static void qsort1(char *a1, char *a2, register size_t width) { register char *left, *right; register char *lefteq, *righteq; int cmp; for (;;) { if (a2 <= a1) return; left = a1; right = a2; lefteq = righteq = a1 + width * (((a2-a1)+width)/(2*width)); /* Pick an element in the middle of the array. We will collect the equals around it. "lefteq" and "righteq" indicate the left and right bounds of the equals respectively. Smaller elements end up left of it, larger elements end up right of it. */ again: while (left < lefteq && (cmp = (*qcompar)(left, lefteq)) <= 0) { if (cmp < 0) { /* leave it where it is */ left += width; } else { /* equal, so exchange with the element to the left of the "equal"-interval. */ lefteq -= width; qexchange(left, lefteq, width); } } while (right > righteq) { if ((cmp = (*qcompar)(right, righteq)) < 0) { /* smaller, should go to left part */ if (left < lefteq) { /* yes, we had a larger one at the left, so we can just exchange */ qexchange(left, right, width); left += width; right -= width; goto again; } /* no more room at the left part, so we move the "equal-interval" one place to the right, and the smaller element to the left of it. This is best expressed as a three-way exchange. */ righteq += width; q3exchange(left, righteq, right, width); lefteq += width; left = lefteq; } else if (cmp == 0) { /* equal, so exchange with the element to the right of the "equal-interval" */ righteq += width; qexchange(right, righteq, width); } else /* just leave it */ right -= width; } if (left < lefteq) { /* larger element to the left, but no more room, so move the "equal-interval" one place to the left, and the larger element to the right of it. */ lefteq -= width; q3exchange(right, lefteq, left, width); righteq -= width; right = righteq; goto again; } /* now sort the "smaller" part */ qsort1(a1, lefteq - width, width); /* and now the larger, saving a subroutine call because of the for(;;) */ a1 = righteq + width; } /*NOTREACHED*/ }
/****************************************************************************** * * hypre_IJMatrixInsertRowPETSc * * inserts a row into an IJMatrix, * if diag_i and offd_i are known, those values are inserted directly * into the ParCSRMatrix, * if they are not known, an auxiliary structure, AuxParCSRMatrix is used * *****************************************************************************/ HYPRE_Int hypre_IJMatrixInsertRowPETSc(hypre_IJMatrix *matrix, HYPRE_Int n, HYPRE_Int row, HYPRE_Int *indices, double *coeffs) { HYPRE_Int ierr = 0; hypre_ParCSRMatrix *par_matrix; hypre_AuxParCSRMatrix *aux_matrix; HYPRE_Int *row_starts; HYPRE_Int *col_starts; MPI_Comm comm = hypre_IJMatrixContext(matrix); HYPRE_Int num_procs, my_id; HYPRE_Int row_local; HYPRE_Int col_0, col_n; HYPRE_Int i, temp; HYPRE_Int *indx_diag, *indx_offd; HYPRE_Int **aux_j; HYPRE_Int *local_j; double **aux_data; double *local_data; HYPRE_Int diag_space, offd_space; HYPRE_Int *row_length, *row_space; HYPRE_Int need_aux; HYPRE_Int indx_0; HYPRE_Int diag_indx, offd_indx; hypre_CSRMatrix *diag; HYPRE_Int *diag_i; HYPRE_Int *diag_j; double *diag_data; hypre_CSRMatrix *offd; HYPRE_Int *offd_i; HYPRE_Int *offd_j; double *offd_data; hypre_MPI_Comm_size(comm, &num_procs); hypre_MPI_Comm_rank(comm, &my_id); par_matrix = hypre_IJMatrixLocalStorage( matrix ); aux_matrix = hypre_IJMatrixTranslator(matrix); row_space = hypre_AuxParCSRMatrixRowSpace(aux_matrix); row_length = hypre_AuxParCSRMatrixRowLength(aux_matrix); col_n = hypre_ParCSRMatrixFirstColDiag(par_matrix); row_starts = hypre_ParCSRMatrixRowStarts(par_matrix); col_starts = hypre_ParCSRMatrixColStarts(par_matrix); col_0 = col_starts[my_id]; col_n = col_starts[my_id+1]-1; need_aux = hypre_AuxParCSRMatrixNeedAux(aux_matrix); if (row >= row_starts[my_id] && row < row_starts[my_id+1]) { if (need_aux) { row_local = row - row_starts[my_id]; /* compute local row number */ aux_j = hypre_AuxParCSRMatrixAuxJ(aux_matrix); aux_data = hypre_AuxParCSRMatrixAuxData(aux_matrix); local_j = aux_j[row_local]; local_data = aux_data[row_local]; row_length[row_local] = n; if ( row_space[row_local] < n) { hypre_TFree(local_j); hypre_TFree(local_data); local_j = hypre_CTAlloc(HYPRE_Int,n); local_data = hypre_CTAlloc(double,n); row_space[row_local] = n; } for (i=0; i < n; i++) { local_j[i] = indices[i]; local_data[i] = coeffs[i]; } /* make sure first element is diagonal element, if not, find it and exchange it with first element */ if (local_j[0] != row_local) { for (i=1; i < n; i++) { if (local_j[i] == row_local) { local_j[i] = local_j[0]; local_j[0] = row_local; temp = local_data[0]; local_data[0] = local_data[i]; local_data[i] = temp; break; } } } /* sort data according to column indices, except for first element */ qsort1(local_j,local_data,1,n-1); } else /* insert immediately into data into ParCSRMatrix structure */ {
int main() { qsort1(0, 19); return 0; }
static void qsort1(void **base, void **lim, int (*f)(void *, void *)) { void **i, **j; void **lptr, **hptr; size_t n; int c; top: n = (lim - base) / 2; if (n == 0) return; hptr = lptr = base+n; i = base; j = lim - 1; for (;;) { if (i < lptr) { if ((c = (*f)(*i, *lptr)) == 0) { lptr --; swap2(i, lptr); continue; } if (c < 0) { i += 1; continue; } } begin: if (j > hptr) { if ((c = (*f)(*hptr, *j)) == 0) { hptr ++; swap2(hptr, j); goto begin; } if (c > 0) { if (i == lptr) { hptr ++; swap3(i, hptr, j); i = lptr += 1; goto begin; } swap2(i, j); j -= 1; i += 1; continue; } j -= 1; goto begin; } if (i == lptr) { if (lptr-base >= lim-hptr) { qsort1(hptr+1, lim, f); lim = lptr; } else { qsort1(base, lptr, f); base = hptr+1; } goto top; } lptr -= 1; swap3(j, lptr, i); j = hptr -= 1; } }