C:按降序对向量元素进行排序 [英] C: Sort vector elements in descending order

查看:125
本文介绍了C:按降序对向量元素进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个函数,该函数将重新排列数组,使其以降序排列.该数组由正整数组成,没有两个相等的元素.这就是我所拥有的:

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.

推荐答案

我尝试从上面的代码开始,遵循您在评论中陈述的想法,并进行了一些更改,这是最后两个功能

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];
}

这是测试

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;
}

可提供所需的输出

152 24 11 9 1

注意:您可以通过在同一数组上进行交换而不是分配另一个数组来改善代码!

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

这篇关于C:按降序对向量元素进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆