Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C:按降序排列向量元素_C_Arrays_Sorting_Vector - Fatal编程技术网

C:按降序排列向量元素

C:按降序排列向量元素,c,arrays,sorting,vector,C,Arrays,Sorting,Vector,我正在尝试创建一个函数来重新排列数组,使其按降序排列。数组由正整数组成,没有两个相等的元素。这就是我所拥有的: 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,

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

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

工作不正常。我在过去的一周里一直在考虑这个问题,所以一些建议会很好。谢谢。

有很多排序算法。一个简单的算法是找到数组中的最小元素并将其放在第一个位置,方法是将其与第一个位置的任何项目交换,然后递归地对数组排序,但这次从下一个位置开始

void sort(int a[], int l, int r)
{   if(l == r) return; /* 1-elemnt array is already sorted */

    int min = l;
    for(int i = l+1; i <= r; i++)
    {   if(a[i] < a[min])
        {   min = i;
        }
    }

    swap(a[l], a[min]);
    sort(a, l+1, r);
}

您也可以迭代执行。

也许,其目的如下

int check (int *v, int n){
    int i;
    for (i=0; i<n; i++){
        if (v[i] != -1){
            return -1;
            //break;  //This code that does not reach
        }
        //else return 1;  //move to after for-loop
    }
    return 1;
}

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

    while (check(v,n) == -1){
        for (i=0; i<n; i++){
            if(v[i]<0) continue;//Element of -1 excluded. v[k] too.
            for (k=i+1; k<n; k++){
                if (v[k]>=0 &&  v[i] < v[k]) break;
            }
            if (k == n){
                vp[j] = v[i];
                v[i] = -1;
                j++;
                break;//start over
            }
        }
    }

    for (i=0; i<n; i++){
        v[i] = vp[i];
    }
}
我试着按照您在评论中陈述的想法,从上面的代码开始,做了一些修改,下面是最后两个函数

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

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

您可以使用现有的排序实现,而不是重新发明轮子:

#include <stdlib.h>

int desc(void const *a, void const *b)
{
    if ( *(int *)a < *(int *)b ) return 1;
    return -1;
}

void sortVector (int *v, int n)
{
    qsort(v, n, sizeof *v, desc);
}

选中“始终返回第一个元素”。主元素在哪里?排序算法背后的想法是什么?很难推断出你想要实现什么样的排序算法?如果数组中的所有整数都是正数,那么为什么在check函数中你要将它们与-1进行比较?事实上,我问这个问题时犯了一个错误,我的意思是降序而不是升序。sortVector函数查找向量v的最大值,将其复制到vp,并将向量v中的最大值替换为-1。这就是我使用check函数的原因:其思想是,当它完成时,v的所有元素都将为-1,程序将退出while循环,并将vp复制到v。谢谢!我不知道为什么我不记得这一点——我以前多次使用临时变量来寻找最大值。我没有得到的是仅仅做v[maxIndex]=-1和v[maxIndex]=v[I]之间的区别;v[i]=-1;,就像你所做的,在找到最大值之后,让我们以v[]={1152,24,11,9}为例;对于第一个循环i=0,我们有v[0]=1,最大值是152,所以如果你把152变成-1,那么在下一个循环中你会有1-1241119,我的意思是,对于i=1,你会忽略第一个元素,这里是1,这就是为什么你需要在v[i]和v[maxIndex]之间交换`第一个元素的最大值,如152 1 24 11 9,然后将其转换为-1,如-1 1 24 11 9,因此1可以包含在下一个循环的排序算法中。如果您仍然需要解释,请随意询问@mcpca!:是的,我明白了——这似乎也使得我已经可疑的check函数变得无用,这使得它变得更加简单。谢谢你的帮助,我试过了,但引发这个问题的是,我需要根据每个结构中int变量的大小对结构数组中的元素进行排序。在那种情况下,这个功能似乎不起作用。我问这个问题是因为我似乎无法得到算法。@mcpca在这种情况下,您仍然可以使用qsort,您只需要更改comparator函数的内容,以包含进行比较的逻辑。
152 24 11 9 1
#include <stdlib.h>

int desc(void const *a, void const *b)
{
    if ( *(int *)a < *(int *)b ) return 1;
    return -1;
}

void sortVector (int *v, int n)
{
    qsort(v, n, sizeof *v, desc);
}