0

So I am very new to C programming, and for a project, I have been given a quicksort program, which I will link below, and asked to re-write the quicksort program using pointer arithmetics, i.e without any index operations. How would I go about doing this? What do I do in my code to achieve pointer arithmetics instead of Index Operations? My code is here: http://ideone.com/ku9EhU

#include<stdio.h>
#define N 10

void quicksort(int a[], int low, int high);
int split(int a[], int low, int high);

int main(void)
{
  int a[N], i;

  printf("Enter %d numbers to be sorted: ", N);
  for (i = 0; i < N; i++)
    scanf("%d", &a[i]);

  quicksort(a, 0, N - 1);

  printf("In sorted order: ");
  for (i = 0; i < N; i++)
    printf("%d ", a[i]);
  printf("\n");

  return 0;
}

void quicksort(int a[], int low, int high)
{
  int middle;

  if (low >= high) return;
  middle = split(a, low, high);
  quicksort(a, low, middle - 1);
  quicksort(a, middle + 1, high);
}

int split(int a[], int low, int high)
{
  int part_element = a[low];

  for (;;) {
    while (low < high && part_element <= a[high])
      high--;
    if (low >= high) break;
    a[low++] = a[high];

    while (low < high && a[low] <= part_element)
      low++;
    if (low >= high) break;
    a[high--] = a[low];
  }

  a[high] = part_element;
  return high;
}

3 Answers 3

1

Array index access a[i] equals pointer arithmetic *(a + i).

Sign up to request clarification or add additional context in comments.

1 Comment

And &a[i] -> a + i.
0

First why do want to do pointer arithmetic? Pointer arithmetic is simply a[i] = *(a + i) which gives no performance improvement for your program

I think for performance improvement what you need is references because that is the best practice to do so. Check the revised code.

#include<stdio.h>
#define N 10

void quicksort(int a[], int low, int high);
int split(int a[], int low, int high);

int main(void)
{
  int a[N], i;

  printf("Enter %d numbers to be sorted: ", N);
  for (i = 0; i < N; i++)
    scanf("%d", &a[i]);

  quicksort(&a[0], 0, N - 1);

  printf("In sorted order: ");
  for (i = 0; i < N; i++)
    printf("%d ", a[i]);
  printf("\n");

  return 0;
}

void quicksort(int *a, int low, int high)
{
  int middle;

  if (low >= high) return;
  middle = split(a, low, high);
  quicksort(a, low, middle - 1);
  quicksort(a, middle + 1, high);
}

int split(int *a, int low, int high)
{
  int part_element = a[low];

  for (;;) {
    while (low < high && part_element <= a[high])
      high--;
    if (low >= high) break;
    a[low++] = a[high];

    while (low < high && a[low] <= part_element)
      low++;
    if (low >= high) break;
    a[high--] = a[low];
  }

  a[high] = part_element;
  return high;
}

1 Comment

Are they? In java they are passed by reference, but in C as far as I know you have to create pointers and send by reference explicitly.
0

I'm wondering if the goal here is to redefine the sort function:

quicksort(int *low, int *high)
{
int *middle;
    if((high - low) == 0)
        return;
    middle = low + ((high - low)/2);
    /* ... */
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.