开发者

Analyzing quick sort in C

开发者 https://www.devze.com 2023-02-22 09:18 出处:网络
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

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.

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

Where:

level is the level of recursion.

partitionNumber is the number used to create the partition.

d is '>' if partitionNumber is greater than the elements of the partition otherwise is '<'.

3a_space is a set of 3 space characters for each element of the array before the start of the partition.

x1 x3 ... xn is the set of number in the partition. Note: print these numbers using the "%3d" format descriptor.

3b_spaces is a set of 3 space characters for each element of the array after the end of the partition.

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< [                 开发者_如何学Go       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. 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. 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); inside your quicksort routine. What's up with that? Note: apparently it's from 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?


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.

It should not come as a surprise that you didn't set the value of d, hence all the spaces. Also, why make d global?

Your print loop should traverse the entire list so the part in brackets will always be the same size.

0

精彩评论

暂无评论...
验证码 换一张
取 消