1

Given an array I am required to sort it in such a way that the first element is the smallest value, the second element is the largest, the third element is the second smallest element and so on.

But my code just prints the original array and I am not able to figure out why. Any advice would be appreciated.

#include <stdio.h>
void swap(int m, int n);
int main()
{
    int i,j,A[10],n;

    printf ("enter the number of array elements\n");
    scanf ("%d", &n);

    for (i=0;i<n;i++){
       scanf ("%d", &A[i]);
    }


    for (i=0;i<n;i++){

        if (i%2 == 0){
            for (j=i;j<n;j++){
                if (A[j] < A[i]){
                    swap(A[i],A[j]);
                }
            }
        }
        else if (i%2 != 0){
            for (j=i;j<n;j++){
                if (A[j] > A[i]){
                    swap (A[i],A[j]);
                }
            }
        }

    }

    for(i=0;i<n;i++){
        printf ("%d\n", A[i]);
    }
    return 0;
}

void swap( int m, int n)
{
    int temp;
    temp = m;
    m = n;
    n = temp;
}
user34304
  • 123
  • 1
  • 2
  • 7
  • If you would like an inline in-place swap, try this: `A[i] ^= A[j] ^= A[i];` And eliminate the swap function altogether. You can also use a macro for this purpose. – Quirk Mar 25 '16 at 20:35
  • `void swap(int A[], int i, int j);` will also work. Call with `swap(A,i,j)`. – user3386109 Mar 25 '16 at 20:39
  • 1
    @Quirk: Bad advice. Using a function for swap makes the code more readable and the xor-chain is also less intuitive and does not yield any advantage in modern code and is likely less effective with a badly optimising compiler. – too honest for this site Mar 25 '16 at 20:40
  • @Olaf: Agreed. But OP doesn't want anything to do with pointers. And passing entire arrays by value is not a good practice either. Xor swaps are a beautiful way to do efficient swaps under macros. – Quirk Mar 25 '16 at 20:42
  • @Quirk: You cannot pass an array by value in C, thus your comment is pointless. Any way of passing a modifyable object to a function involves pointers. Using arrays without using pointers in C is impossible. Anyway, there is no statement in the question he does not want pointers. – too honest for this site Mar 25 '16 at 20:49
  • @Olaf: "You cannot pass an array by value in C" Ahh shoot! My bad. "Not using pointer in any non-trivial C code is impossible." This is debatable. But I would argue in your favor. But OP hasn't yet been taught pointers in his course, and specifically requested for non-pointer implementations. – Quirk Mar 25 '16 at 20:52
  • @Olaf: http://stackoverflow.com/questions/36227486/sorting-an-array-with-alternate-smallest-largest-values?noredirect=1#comment60086247_36227550 for reference. – Quirk Mar 25 '16 at 20:53
  • 1
    @Quirk: An array decays to a pointer for most usages (and all accesses to its elements). There is nothing to debate, just read the standard. And learning pointer _after_ arrays in C is just plain bad didactics. – too honest for this site Mar 25 '16 at 21:01
  • @Olaf: You win. On a side note, [What does "trivial" mean?](http://programmers.stackexchange.com/questions/244941/what-does-trivial-mean) – Quirk Mar 25 '16 at 21:07

2 Answers2

1

You need to pass by reference using pointers.

void swap( int *m, int *n)
{
    int temp;
    temp = *m;
    *m = *n;
    *n = temp;
}

and change your code to call it like this

swap (&A[i],&A[j]);

For a solution that doesn't use pointers you can use a MACRO like this;

#define swap(x,y) do{int t=(x);(x)=(y);(y)=t;}while(0);

swap(A[i],A[j]);

Just define this at the top of your file and remove the swap function and prototype. It's all about scope, because the MACRO is just a text replace it's in the correct scope to use A[i].

cleblanc
  • 3,678
  • 1
  • 13
  • 16
  • I'm asking because the programming course in my college hasn't covered pointers yet. – user34304 Mar 25 '16 at 20:32
  • @user34304 Pass by value will limit you to scoping issues. You'll need to use pointer references. – Quirk Mar 25 '16 at 20:32
  • 1
    If you would like an inline in-place swap, try this: `A[i] ^= A[j] ^= A[i];` – Quirk Mar 25 '16 at 20:33
  • `#define swap(x,y) {int t = x;x=y;y=t;}` and remove your function. now you call it as swap(A[i],A[j]); – cleblanc Mar 25 '16 at 20:35
  • @user34304: Strange: they introduced arrays **before** pointers? In C you cannot do anything useful with array without using a pointer. – too honest for this site Mar 25 '16 at 20:52
  • @cleblanc: This macro will break some compound statements in a fatal way. At leat one should use the standard `do .. while` wrapper construct. – too honest for this site Mar 25 '16 at 20:54
  • @Olaf: It was so in my case as well. I was taught about arrays and the box operator much before pointers. – Quirk Mar 25 '16 at 20:54
  • @user34304: If you want to use a separate function to swap the values of `A[i]` and `A[j]`, then no, you cannot avoid using pointers in some form. If you're absolutely dead-set against using them, then I'd suggest performing the swap inline instead of calling a separate function. Pointers really aren't that difficult to understand or use, but they are often explained poorly with bad examples, which is why so many people struggle with them. – John Bode Mar 25 '16 at 21:55
0

The first problem I notice in your program is your swap function. In your swap function, your parameters are primitive data types. Thus, the function creates copies of integers "m" and "n", and switches the values within the scope of the function swap. But as soon as the function returns, you haven't really swapped anything. To actually swap the values in the array that you created in main, you need to do a pass by reference(pass in pointers to the variable you are trying to swap). Modify your swap function like this:

void swap( int *m, int *n)
{
   int temp;
   temp = *m;
   *m = *n;
   *n = temp;
}

Then inside your main, pass in the address of that value in the array using the & operator(address of). Here is an example: swap (&A[i],&A[j]);

Other suggestions:

  1. Format your code so there is space between your conditions in your for loops.
  2. Add comments.