Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to create a function that will rearrange an array so it is in descending order. The array is made up from positive integers, with no two equal elements. This is what I have:

int check (int *v, int n){
int i;
for (i=0; i<n; i++){
    if (v[i] != -1){
        return -1;
        break;
    }
    else return 1;
}
}

void sortVector (int *v, int n){
    int i, k, j=0, vp[n];

while (check(v,n) == -1){   
    for (i=0; i<n; i++){
        for (k=i+1; k<n; k++){
            if (v[k] > v[i]) break;
            else if (k == n-1){
                vp[j] = v[i];
                v[i] = -1;
                j++;
            }               
        }
    }
}

for (i=0; i<n; i++)
    v[i] = vp[i];
}

Which is not working correctly. I've been thinking about this for the past week so some pointers would be great. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
227 views
Welcome To Ask or Share your Answers For Others

1 Answer

I tried to follow the idea that you have stated in your comment by starting from your code above and made few changes and here is the two final functions

#include <stdio.h>

int check (int *v, int n)
{
    int i;
    for (i=0; i<n; i++)
    {
        if (v[i] != -1)
        {
            return -1; // break is useless because return has the same effect 
        }
    }
    return 1; // you need to add a return here to handle all the cases
              // the case if the loop is not entered you need to return a value 
}

void sortVector (int *v, int n)
{
    int i, k, j=0, vp[n];
    int maxIndex=0;//you need to add this variable in order to keep track of the maximum value in each iteration

    while (check(v,n) == -1)
    {
        for (i=0; i<n; i++)
        {
            maxIndex=i; //you suppose that the maximum is the first element in each loop 
            for (k=i+1; k<n; k++)
            {
                if (v[k] > v[maxIndex])
                  maxIndex=k; // if there is another element greater you preserve its index in the variable 
            }
           //after finishing the loop above you have the greatest variable in the array  which has the index stored in maxIndex
                    vp[i] = v[maxIndex]; // put it in vp array
                    v[maxIndex]=v[i];//put it in treated elements zone
                    v[i]=-1;// make it -1
                    j++;
        }
    }

    for (i=0; i<n; i++)
        v[i] = vp[i];
}

This is the test

int main()
{
    int tab[]= {1,152,24,11,9};
    sortVector (tab, 5);
    int i=0;
    for(i=0; i<5; i++)
    {
        printf("%d ",tab[i]);
    }
    return 0;
}

which gives the desired output

152 24 11 9 1

Note: You can improve your code by making swaps on the same array instead of allocating another array !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...