简体   繁体   中英

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

I want to sort a set of data using C implementation. 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:

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) . I dont't think that there is a much faster way. Apart from just reading the data in the other direction of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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