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;
}
9
  • 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. Commented Mar 25, 2016 at 20:35
  • void swap(int A[], int i, int j); will also work. Call with swap(A,i,j). Commented Mar 25, 2016 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. Commented Mar 25, 2016 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. Commented Mar 25, 2016 at 20:42
  • 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. Commented Mar 25, 2016 at 21:01

2 Answers 2

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].

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

8 Comments

I'm asking because the programming course in my college hasn't covered pointers yet.
@user34304 Pass by value will limit you to scoping issues. You'll need to use pointer references.
If you would like an inline in-place swap, try this: A[i] ^= A[j] ^= A[i];
#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]);
@user34304: Strange: they introduced arrays before pointers? In C you cannot do anything useful with array without using a pointer.
|
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.

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.