简体   繁体   English

C ++中的订单号

[英]order numbers in C++

I have this code which orders the numbers starting from the biggest one but its kinda confusing for me: 我有这段代码,它从最大的数字开始对数字进行排序,但对我来说有点令人困惑:

    #include <iostream>
using namespace std;

int main()
{
 const int n=5;
 int i, j, t, a[n]={15,9,8,7,5};

 cout << "a[]={";
 for(i=0; i<n; i++) cout << a[i] << " ";
 cout << "} \n\n";

 for(i=0; i<n-1; i++)
   for(j=i+1; j<n; j++)
     if(a[i]>a[j]){
       t=a[i];
       a[i]=a[j];
       a[j]=t;
     }

 cout << "Pas radhitjes inkrementuese \n\n"
      << "a[]={";
 for(i=0; i<n; i++) cout << a[i] << " ";
 cout << "} \n\n";


 cin.get();cin.get();
return 0;
}

Is there any other way to order numbers from 1 array, starting from the biggest/lowest number? 从最大/最低编号开始,还有其他方法可以从1个数组中订购编号吗?

std::sort是排序内容时的常用方法

Well there is the selection sort, insertion sort, merge sort, quick sort, heap sort, etc. You can find a lot of information regarding the particular implementation around the web. 好吧,有选择排序,插入排序,合并排序,快速排序,堆排序等。您可以在网上找到许多有关特定实现的信息。 If you want an easy way without regards to the actual implementation of the sorting algorithm, you can use std::sort to accomplish this. 如果您想要一种简单的方法而不考虑排序算法的实际实现,则可以使用std::sort来实现。

you can just edit your code to this 您只需将代码编辑为此

for(i=0; i<n-1; i++)
   for(j=0; j<n-1; j++)

if you want more optimal sort you can use one of sorting algorithm on 如果您想要更优化的排序,可以使用以下排序算法之一

http://en.wikipedia.org/wiki/Sorting_algorithm http://en.wikipedia.org/wiki/Sorting_algorithm

You have the codings and explanations for all the algorithms - http://mathbits.com/mathbits/compsci/arrays/sorting.htm 你必须对所有的算法的译码,并解释- http://mathbits.com/mathbits/compsci/arrays/sorting.htm

I suggest you to try all the algorithm and it would help if in case of the educational learning. 我建议您尝试所有算法,如果进行教育性学习,它将有所帮助。

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

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