简体   繁体   English

哪种排序可以有效地将数据从升序排序到降序?

[英]Which sorting is efficient to sort data from ascending order to descending order?

I want to sort a set of data using C implementation. 我想使用C实现对一组数据进行排序。 I wonder which sorting is efficient and best case in time-complexity. 我想知道哪种排序是有效的,时间复杂度最好的情况。 Note that, the data are in ascending order only. 请注意,数据仅按升序排列。 I want to sort it to descending order. 我想按降序排序。 Which one is more efficient & least efficient and why? 哪一个效率更高,效率最低,为什么? Can anybody explain it with reason? 任何人都可以解释它吗?

In that specific case you don't have to use a sorting algorithm as such. 在这种特定情况下,您不必使用排序算法。 For example you can just swap the i th element with the n - i th element: 例如,您可以将第i个元素与第n - i个元素交换:

for(i = 0; i < size/2; ++i)
{
    tmp = arr[i];
    arr[i] = arr[size - 1 - i];
    arr[size - 1 - i] = tmp;
}

This has always the complexity O(n/2) . 这总是复杂度O(n/2) I dont't think that there is a much faster way. 我不认为有更快的方法。 Apart from just reading the data in the other direction of course. 当然,只是在另一个方向阅读数据。

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

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