简体   繁体   English

分析C中的快速排序

[英]Analyzing quick sort in C

What I am trying to do is get the desired output below. 我想要做的是获得下面所需的输出。 I have tried printing in the beginning of quicksort and I am getting some of the correct values but I feel my whole approach is not working so I need some help. 我已经尝试在quicksort的开头打印,我得到了一些正确的值,但我觉得我的整个方法都没有用,所以我需要一些帮助。

level partitionNumber d [3a_spaces x1 x3 ... xn 3b_spaces] level partitionNumber d [3a_spaces x1 x3 ... xn 3b_spaces]

Where : 地点

level is the level of recursion. level是递归的级别。

partitionNumber is the number used to create the partition. partitionNumber是用于创建分区的编号。

d is '>' if partitionNumber is greater than the elements of the partition otherwise is '<'. 如果partitionNumber大于分区的元素,则d为'>',否则为'<'。

3a_space is a set of 3 space characters for each element of the array before the start of the partition. 3a_space是在分区开始之前数组的每个元素的一组3个空格字符。

x1 x3 ... xn is the set of number in the partition. x1 x3 ... xn是分区中的数字集。 Note: print these numbers using the "%3d" format descriptor. 注意:使用“%3d”格式描述符打印这些数字。

3b_spaces is a set of 3 space characters for each element of the array after the end of the partition. 3b_spaces是分区结束后数组中每个元素的一组3个空格字符。

desired output: 期望的输出:

1  0  [23 13 82 33 51 17 45 75 11 27 ]
2 51> [27 13 33 23 17 45 11          ]
3 23> [11 13 17                      ]
3 23> [11 13 17                      ]
3 23< [            33 45 27          ]
4 45> [            27 33             ]
4 45> [            27 33             ]
3 23< [            27 33 45          ]
2 51> [11 13 17 23 27 33 45          ]
2 51< [                        82 75 ]
2 51< [                        75 82 ]
1  0  [11 13 17 23 27 33 45 51 75 82 ]

my output: 我的输出:

1  0  [23 13 82 33 51 17 45 75 11 27 ]
2 51> [27 13 33 23 17 45 11          ]
3 23> [11 13 17                      ]
2 13> [11 13 17                      ]
3 23< [            33 45 27          ]
4 45> [            27 33             ]
3 27? [            27 33             ]
2 45> [            27 33 45          ]
1 23> [11 13 17 23 27 33 45          ]
2 51< [                        82 75 ]
1 82> [                        75 82 ]
0 51> [11 13 17 23 27 33 45 51 75 82 ]






 #include<stdio.h>

int inputLine[10];

void quicksort(int v[], int left, int right, int level, int previousPivotPoint, char d);

int main()
{
  int v[] = { 23, 13, 82, 33, 51, 17, 45, 75, 11, 27 };
  quicksort(v, 0, 9, 0, -1, ' ');



  return 0;
}

void swap(int v[], int i, int j)
{
  int temp;
  temp = v[i];
  v[i] = v[j];
  v[j] = temp;
}

void quicksort(int v[], int left, int right, int level, int previousPivotPoint, char d)
{
  int i, last;

  level++;
  if (previousPivotPoint > v[left]) d = '>';
  else if (previousPivotPoint < v[left]) d = '<';


  if (left >= right)
  {
    level--;
    return;
  }
  if (previousPivotPoint == -1)
  {
    printf("%d  0  [", level);
  } else
  {
    printf("%d %d%c [", level, previousPivotPoint, d);
  }

  previousPivotPoint = v[(left + right) / 2];

  d = '?';
  for (i = 0; i < 10; i++)
  {
    if (i > right || i < left)
    {
      printf("   ");
    } else
    {
      printf("%d ", v[i]);
    }
  }
  printf("]\n");

  swap(v, left, (left + right) / 2);

  last = left;

  for (i = left + 1; i <= right; i++)
  {
    if (v[i] < v[left])
    {
      last++;
      swap(v, last, i);
    }
  }

  swap(v, left, last);
  quicksort(v, left, last - 1, level, previousPivotPoint, d);
  quicksort(v, last + 1, right, level, previousPivotPoint, d);
  level--;


  if (previousPivotPoint > v[left]) d = '>';
  else if (previousPivotPoint < v[left]) d = '<';
  printf("%d %d%c [", level, previousPivotPoint, d);
  previousPivotPoint = v[(left + right) / 2];


  for (i = 0; i < 10; i++)
  {
    if (i > right || i < left)
    {
      printf("   ");
    } else
    {
      printf("%d ", v[i]);
    }
  }
  printf("]\n");

}

A couple things I notice: 我注意到几件事:

  1. If level is going to be a modular variable instead of one passed in to the quick sort routine, it's unlikely to ever go down. 如果level将是一个模块化变量而不是传递给快速排序例程的变量,那么它不太可能永远失效。 Consider incorporating it into the signature of your quick sort function. 考虑将其合并到快速排序功能的签名中。
  2. You start at i = left - this won't print your whole original underlying array. 你从i = left开始 - 这不会打印你原来的整个底层数组。 Consider always going over the array and printing them all, checking if you're in the current array or not. 考虑始终遍历数组并打印它们,检查您是否在当前阵列中。
  3. You make a FORWARD declaration void swap(int v[], int i, int j); 你做一个FORWARD声明void swap(int v[], int i, int j); inside your quicksort routine. 在你的快速排序程序中。 What's up with that? 那是怎么回事? Note: apparently it's from K&R. 注意:显然它来自K&R。 As a matter of personal taste, I'm not the biggest fan of the notation, but you could do worse than following K&R, eh? 作为个人品味的问题,我不是乐谱的最大粉丝,但你可能比跟随K&R做得更糟,是吗?

Didn't have time to delve into your code too deeply but here are a few things. 没有时间深入研究你的代码,但这里有一些东西。

First thing you should do is replace that hardcoded 10 with a #define constant. 你应该做的第一件事是用#define常量替换硬编码的10

It should not come as a surprise that you didn't set the value of d , hence all the spaces. 你没有设置d的值,因此所有的空间都不应该让你感到惊讶。 Also, why make d global? 另外,为什么要使d全局?

Your print loop should traverse the entire list so the part in brackets will always be the same size. 您的打印循环应该遍历整个列表,因此括号中的部分将始终具有相同的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM