Exemple #1
0
void quick2(int a[], int left, int right, int y){
	int  s, t, i, j, x, k;
	for(k = 0; k < 10; k++){
		printf("%4d", a[k]);
	}
	for(x = 0; x < y; x++) printf("  ");
	printf(" %d  %d ", left, right);
	printf("\n");
	if(left < right){
		//左端の項を軸にする
		s = a[(left + right) / 2];
		//軸より小さいグループと大きいグループに分ける
		i = left - 1; j = right + 1;
		printf("%2d, ", s);
		while(1){
			//軸より大きいものを選ぶ
			while(a[++i] < s);
			//軸より小さいものを選ぶ
			while(a[--j] > s);
			//逆転している場合、正しく並んでいる
			if(i >= j) break;
			printf("%3d <-> %2d,", a[i], a[j]);
			t = a[i]; a[i] = a[j]; a[j] = t;
		}
		printf("\n");
		//軸の交換は行わない
		//左部分に対する再帰
		quick2(a, left, i - 1, y + 1);
		//右部分に対する再帰
		quick2(a, j + 1, right, y + 1);
	}

}
Exemple #2
0
void quick2(struct node a[20],int l,int h)//µÝ¹éµÄ¿ìËÙÅÅÐò
{
	int i;
	if(l<h)
	{
		i=hoare(a,l,h);
		quick2(a,l,i-1);
		quick2(a,i+1,h);
	}
}//quick2 end
Exemple #3
0
int
main (void)
{
  int k;
  struct PAIR output = {-1, -1};

  srand (time (NULL));

  for (k = 0; k < n; k++) {
    printf ("%d  ", array[k]);
  }
  printf ("\n");

//  output = part2 (array, 0, n-1); 
//  printf ("output: q = %d, t = %d\n", output.q, output.t);
//  output = randpart2 (array, 0, n-1); 
//  printf ("output: q = %d, t = %d\n", output.q, output.t);
  quick2 (array, 0, n-1);

  for (k = 0; k < n; k++) {
    printf ("%d  ", array[k]);
  }
  printf ("\n");

  return 0;
}
Exemple #4
0
void quick_main2(){
	static int a[] = {41, 24, 76, 11, 45, 64, 21, 69, 19, 36};
	int k;
	printf("\n");
	printf("\n");
	quick2(a, 0, 10 - 1, 0);

	for(k = 0; k < 10; k++){
		printf("%4d", a[k]);
	}
	printf("\n");
}
Exemple #5
0
void
quick2 (
  int *A,
  int p,
  int r
  )
{
  struct PAIR idx = {-1, -1};
  int k;
  //base case:
  if (r <= p) {
    return;
  }

  //for debug
  printf ("Before randpart2:\n");
  for (k = p; k <=r; k++) {
    printf ("%d  ", A[k]);
  }
  printf ("\n");

  idx = randpart2 (A, p, r);
//  idx = part2 (A, p, r);

  //for debug
  printf ("After randpart2:\n");
  for (k = p; k <=r; k++) {
    printf ("%d  ", A[k]);
  }
  printf ("\n");

  if (idx.q < p || idx.q > r || idx.t < p || idx.t > r) {
    //error
    return;
  }
  quick2 (A, p, idx.q-1);
  quick2 (A, idx.t+1, r);
}
Exemple #6
0
main()
{
	void print(struct node a[20],int n);
	int creat();
	void shell(struct node a[20],int n);
	int hoare(struct node a[20],int l,int h);
	void quick1(struct node a[20],int n);
	void quick2(struct node a[20],int l,int h);
	void heap(struct node a[20],int i,int m);
	void heapsort(struct node a[20],int n);
	void merges(struct node a[20],struct node a2[20],int h1,int mid,int h2);
	void mergepass(struct node a[20],struct node a2[20],int l,int n);
	void mergesort(struct node a[20],int n);
	int yx(int m,int i);
	int radixsort(struct rnode a[20],int n);
	int num,l,h,c;
	struct rnode s[20];
	c=1;
	while(c!=0)
	{
		printf("        Ö÷²Ëµ¥                       \n");
		printf("   1    ÊäÈë¹Ø¼ü×Ö£¬ÒÔ-9999±íʾ½áÊø¡£\n");
		printf("   2    Ï£¶ûÅÅÐò                     \n");
		printf("   3    ·ÇµÝ¹éµÄ¿ìËÙÅÅÐò             \n");
		printf("   4    µÝ¹éµÄ¿ìËÙÅÅÐò               \n");
		printf("   5    ¶ÑÅÅÐò                       \n");
		printf("   6    ¹é²¢ÅÅÐò                     \n");
		printf("   7    »ùÊýÅÅÐò                     \n");
		printf(" ÊäÈëÑ¡Ôñ    (1--7,0±íʾ½áÊø):         ");
		scanf("%d",&c);
		switch(c)
		{
		case 1:num=creat();print(r,num);break;
		case 2:shell(r,num);print(r,num);break;
		case 3:quick1(r,num);print(r,num);break;
		case 4:l=0;h=num-1;quick2(r,l,h);
			printf("output quick2sort result:\n");
			print(r,num);break;
		case 5:heapsort(r,num);break;
		case 6:mergesort(r,num);print(r,num);break;
		case 7:radixsort(s,num);
		}
	}
}//main end