简体   繁体   English

在 C++ 中对数组和值进行排序

[英]Sorting arrays and values in c++

#include <iostream>
using namespace std;

int main() {
    int a=70, b=40, c=5, d=1, e=-20, f=90, g=2, mid;
    int array[7]={a, b, c, d, e, f, g};
    for (int i = 0; i<=5; i++) {
        while (i=0) {
            if (a>b && a>c && a>d && a>e && a>f && a>g) {
                mid=g;
                g=a;
                a=mid;
            }
                else if (b>a && b>c && b>d && b>e && b>f && b>g) {
                    mid=g;
                    g=b;
                    b=mid;
                }
                else if (c>a && c>b && c>d && c>e && c>f && c>g) {
                    mid=g;
                    g=c;
                    c=mid;
                }
                else if (d>a && d>c && d>b && d>e && d>f && d>g) {
                    mid=g;
                    g=d;
                    d=mid;
                }
                else if (e>a && e>c && e>d && e>b && e>f && e>g) {
                    mid=g;
                    g=e;
                    e=mid;
                }
                else if (f>a && f>c && f>d && f>e && f>b && f>g) {
                    mid=g;
                    g=f;
                    f=mid;
                }
                else if (g>a && g>c && g>d && g>e && g>f && g>b) {
                    mid=g;
                    g=g;
                    g=mid;
                }
        }
        while (i=1) {
            if (a>b && a>c && a>d && a>e && a>f) {
                mid=f;
                f=a;
                a=mid;
            }
                else if (b>a && b>c && b>d && b>e && b>f) {
                    mid=f;
                    f=b;
                    b=mid;
                }
                else if (c>a && c>b && c>d && c>e && c>f) {
                    mid=f;
                    f=c;
                    c=mid;
                }
                else if (d>a && d>c && d>b && d>e && d>f) {
                    mid=f;
                    f=d;
                    d=mid;
                }
                else if (e>a && e>c && e>d && e>b && e>f) {
                    mid=f;
                    f=e;
                    e=mid;
                }
                else if (f>a && f>c && f>d && f>e && f>b) {
                    mid=f;
                    f=b;
                    b=mid;
                }
        }
        while (i=2) {
            if (a>b && a>c && a>d && a>e) {
                mid=e;
                e=a;
                a=mid;
            }
                else if (b>a && b>c && b>d && b>e) {
                    mid=e;
                    e=b;
                    b=mid;
                }
                else if (c>a && c>b && c>d && c>e) {
                    mid=e;
                    e=c;
                    c=mid;
                }
                else if (d>a && d>c && d>b && d>e) {
                    mid=e;
                    e=d;
                    d=mid;
                }
                else if (e>a && e>c && e>d && e>b) {
                    mid=e;
                    e=e;
                    e=mid;
                }

        }
        while (i=3) {
            if (a>b && a>c && a>d) {
                mid=d;
                d=a;
                a=mid;
            }
                else if (b>a && b>c && b>d) {
                    mid=d;
                    d=b;
                    b=mid;
                }
                else if (c>a && c>b && c>d) {
                    mid=d;
                    d=c;
                    c=mid;
                }
                else if (d>a && d>c && d>b) {
                    mid=d;
                    d=d;
                    d=mid;
                }

        }
        while (i=4) {
            if (a>b && a>c) {
                mid=c;
                c=a;
                a=mid;
            }
                else if (b>a && b>c) {
                    mid=c;
                    c=b;
                    b=mid;
                }
                else if (c>a && c>b) {
                    mid=c;
                    c=c;
                    c=mid;
                }


        }
        while (i=5) {
            if (a>b) {
                mid=b;
                b=a;
                a=mid;
            }
                else if (b>a) {
                    mid=b;
                    b=b;
                    b=mid;
                }
        }
    }

    for (int k=0; k<7; k++){
        cout<<array[k]<<" ";
    }

}

This program does not give me an output... What do I do instead?这个程序没有给我输出......我该怎么办? I have tried everything, but it just won't get through to the execution part?我已经尝试了所有方法,但它只是无法进入执行部分? What do I do?我该怎么办?

  1. You confused assignment ( = ) with comaprison ( == ).你混淆了赋值 ( = ) 和 comaprison ( == )。 while(i = x) will assign x to i , then evaluate i , so it's equal to while(x) , which does never run if x is 0 and endless otherwise. while(i = x)会将x分配给i ,然后计算i ,因此它等于while(x) ,如果x为 0 则永远不会运行,否则为无穷大

  2. Your loops are still endless.你的循环仍然是无止境的。 Take for example the (fixed) loop while (i == 3) .以(固定)循环while (i == 3)为例。 i is never modified inside that loop and there is no break or return in it, so even the fixed version will run endlessy. i永远不会在该循环内进行修改,并且其中没有breakreturn ,因此即使是固定版本也会无休止地运行。 But that's a design issue you need to solve.但这是需要解决的设计问题。 Maybe there should be a else { break; }也许应该有一个else { break; } else { break; } after all those if 's? else { break; }毕竟这些if的?

  3. You will still get the wrong result, because you swap the values of the variables, a , b , c , d , e , f and g , but never modify array .您仍然会得到错误的结果,因为您交换了变量abcdefg ,但从不修改array However in the end, you print the values of array.但是最后,您打印了数组的值。 The array does not store items by reference, but copies their values.该数组不通过引用存储项目,而是复制它们的值。 Why do you even use so many variables and don't operate on the array directly?为什么还要使用这么多变量而不直接对数组进行操作?

  4. The last else if(..) block in each while is useless.每个while的最后一个else if(..)块是无用的。 Take a close look at - for example - this code:仔细看看 - 例如 - 这段代码:

     mid=d; d=d; d=mid;

    It assigns the value of d to d .它将d的值分配给d Twice .两次

  5. As already mentioned in the comments, sorting is implemented in the standard library.正如评论中已经提到的,排序是在标准库中实现的。 Use std::sort instead of writing your own sort function:使用std::sort而不是编写自己的排序函数:

     std::sort(std::begin(array), std::end(array));

You can use std or boost library for sorting.您可以使用 std 或 boost 库进行排序。 Or else you can code yourself.或者你可以自己编码。 Please go through the below code.请通过下面的代码。

#include<iostream> 
using namespace std;

void selectionSort(int a[], int n) {
   int i, j, min, temp;
   for (i = 0; i < n - 1; i++) {
      min = i;
      for (j = i + 1; j < n; j++)
         if (a[j] < a[min])
            min = j;
      temp = a[i];
      a[i] = a[min];
      a[min] = temp;
   }
}
int main() {
   int a[] = { 22, 91, 35, 78, 10, 8, 75, 99, 1, 67 };
   int n = sizeof(a)/ sizeof(a[0]);
   int i;
   cout<<"Given array is:"<<endl;
   for (i = 0; i < n; i++)
   cout<< a[i] <<" ";
   cout<<endl;
   selectionSort(a, n);      // calling my function selection sort. This will sort as ascending order

   for (i = 0; i < n; i++)  
   cout<< a[i] <<" ";       // Printing sorted array
   return 0;
}

Above code is for ascending order.以上代码用于升序。

For Descending order refer: https://www.includehelp.com/cpp-programs/sort-an-array-in-descending-order.aspx降序请参考: https : //www.includehelp.com/cpp-programs/sort-an-array-in-descending-order.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM