void Qsort(ElementType ori[], int left, int right) { int i, j; ElementType pivot; //对于较小的数组,采用插入排序会比快速排序更优, //但此处为何是小于等于,而不是大于? if(left + CUTOFF <= right){ pivot = median3(ori, left, right); i = left; j = right - 1; for(;;){ while(ori[++i] < pivot); while(ori[--j] > pivot); if(i < j){ swap(&ori[i], &ori[j]); }else{ break; } } swap(&ori[i], &ori[right-1]); Qsort(ori, left, i-1); Qsort(ori, i+1, right); }else{ Insertsort(ori + left, right - left + 1); } }
int main() { int n,i; //待排序的关键字个数 int r[MAXSIZE]; printf("请输入待排序的关键字个数:"); scanf("%d",&n); for(i=1;i<=n;i++) //输入待排序的关键字 scanf("%d",&r[i]); Insertsort(r,n); }
int main() { void Insertsort(int a[],int n); int a[100],n,i; printf("Input the number of elements in the arrey:"); scanf("%d",&n); printf("Input arrey:"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } Insertsort(a,n); return 0; }
/* Traverse the buckets and put the elements back into their groups. Split the groups and mark all finished groups. The elements are moved in blocks. */ static void intogroups(bucket b[], int pos) { character ch; bucket s; boolean finis; for (ch = 0; ch < CHARS; ch++) { if (!b[ch]) continue; for (s = b[ch]; s; s = s->next) { finis = IS_ENDMARK(ch); if (s->size < INSERTBREAK && !finis) { if (s->size > 1) s->head = Insertsort(s->head, &s->tail, pos); finis = TRUE; } intogroup(s->tag, s->head, s->tail, finis); } b[ch] = NULL; } }
void main() { Seqlist S; int i; char ch1,ch2; printf("请输入10个待排序数据:(每个数据间用空格隔开)\n"); for(i=1; i<=n; i++) scanf("%d",&S[i].key); ch1='y'; while (ch1=='y' || ch1=='Y') { printf("*****************菜单***********************\n"); printf("请选择下列*作:\n"); printf("1------------------更新待排序数据-----------\n"); printf("2------------------直接插入排序-------------\n"); printf("3------------------冒泡排序-----------------\n"); printf("4------------------快速排序-----------------\n"); printf("5------------------直接选择排序-------------\n"); printf("6------------------堆排序-------------------\n"); printf("7------------------归并排序-----------------\n"); printf("8------------------基数排序-----------------\n"); printf("9------------------退出---------------------\n"); printf("请选择*作类别(1-9):"); scanf("\n%c",&ch2); switch (ch2) { case '1': printf("请输入更新待排序数据:\n"); for (i=1; i<=n; i++) scanf ("%d",&S[i].key); break; case '2': printf("请输入要输出第几趟结果:"); scanf("\n%d",&m); for (i=1; i<=n; i++) R[i].key=S[i].key; Insertsort(); break; case '3': printf("请输入要输出第几趟结果:"); scanf("\n%d",&m); for (i=1; i<n+1; i++) R[i].key=S[i].key; Bubblesort(); break; case '4': printf("请输入要输出第几趟结果:"); scanf("\n%d",&m); for (i=1; i<n+1; i++) R[i].key=S[i].key; num=0; Quicksort(1,n); break; case '5': printf("请输入要输出第几趟结果:"); scanf("\n%d",&m); for (i=1; i<n+1; i++) R[i].key=S[i].key; Selectsort(); break; case '6': printf("请输入要输出第几趟结果:"); scanf("\n%d",&m); for (i=1; i<n+1; i++) R[i].key=S[i].key; Heapsort(); break; case '7': printf("请输入要输出第几趟结果:"); scanf("\n%d",&m); for (i=1; i<n+1; i++) R[i].key=S[i].key; Mergesort(); break; case '8': printf("请输入要输出第几趟结果:"); scanf("\n%d",&m); for (i=0; i<n; i++) R[i].key=S[i+1].key; Radixsort(); break; case '9': ch1='n'; break; default: ch1='n'; } } }