Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

arrays - C: Sort vector elements in descending order

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

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

1 Reply

0 votes
by (71.8m points)

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 !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...