【发布时间】:2019-05-11 23:48:32
【问题描述】:
我编写/使用了这种排序。我只是想知道它是否有任何名称,或者它是否类似于任何现有的排序算法。顺便说一句,它是否有效/有价值?
int s = 20;
int unsorted_array[s];
//(adding numbers to unsorted_array)
//We assume that every number is different to avoid any difficulties.
int i, i2, pos;
int sorted_array[s];
//!!! sorting algo starts here:
for(i=0; i<s; i++){
pos = 0;
for(i2=0; i2<s; i2++){
if(unsorted_array[i] > unsorted_array[i2]){
pos += 1;
}
}
sorted_array[pos] = unsorted_array[i];
}
那你怎么看?它比其他类型的排序方法慢/快吗?我还在学习。感谢您的任何回复!
【问题讨论】:
-
int unsorted_array[s];-- 无效的 C++,因为变量s用于表示数组的大小。 -
By the way, is it even efficient/worthy or not really?-- 如果您有 1,000 个元素,那么您将循环最坏的情况总共 1,000,000 次。如果有 1,000,000 个元素,那就是 1,000,000,000,000 次迭代。好不好由你来评判。 -
@PaulMcKenzie 大多数 C++ 编译器仍然会接受它作为扩展。