移动和键比较在 C++ 中是什么意思
What do move and key comparison mean in c++?
以下是
在我的类中关于插入排序的ppt中写的:
void insertionSort(DataType theArray[], int n) {
for (int unsorted = 1; unsorted < n; ++unsorted) {
DataType nextItem = theArray[unsorted];
int loc = unsorted;
for (;(loc > 0) && (theArray[loc-1] > nextItem); --loc)
theArray[loc] = theArray[loc-1];
theArray[loc] = nextItem;
}
}
-
Running time depends on not only the size of the array but also the contents of the array.
Best-case: O(n)
Array is already sorted in ascending order.
Inner loop will not be executed.
>>>> The number of moves: 2*(n-1) O(n)
>>>> The number of key comparisons: (n-1) O(n)
Worst-case: O(n2)
Array is in reverse order:
Inner loop is executed p-1 times, for p = 2,3, …, n
The number of moves: 2*(n-1)+(1+2+...+n-1)= 2*(n-1)+ n*(n-1)/2 O(n2)
The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2 O(n2)
Average-case: O(n2)
We have to look at all possible initial data organizations.
So, Insertion Sort is O(n2)
移动和关键比较到底是什么?我在谷歌上找不到解释。
让我先说一下算法。
- 假设在给定时间数组有两个部分。 索引
0
到索引loc - 1
按升序排序,索引loc
到n - 1
不排序。 - 从
loc
处的元素开始,在数组的排序部分找到它的正确位置并将其插入其中。
所以现在有两个循环:
- 第一个外循环,从
loc = 1
到loc = n
开始,基本上将数组划分为已排序和未排序部分。 - 第二个内部循环在数组的排序部分(
0
到loc - 1
)中查找元素在loc
的位置。
对于内部循环,要找到正确的位置,您必须将 loc
处的元素与数组排序部分中的所有元素进行比较,在最坏的情况下。这是关键的比较。
要插入,您必须在数组的排序部分中为 loc
处的元素创建一个空隙。这是通过将排序部分中的每个元素交换到下一个元素来完成的。这是感动。
Move 是
它为了对数据进行排序而必须执行的交换次数,键是要执行的数据。
相关文章:
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 标准 N3337 5.2.10 第 7 条中的C++"类型"是什么意思?
- "类模板示例<int>;"语句对 C++11 是什么意思?
- 线应该是什么意思
- "CID"在AT+HTTPPARA= "CID" ,1中是什么意思
- 表达式"b=(b-x)&x"是什么意思?
- 这个表达是什么意思?
- 此代码验证公式是什么意思?
- 跟踪日志中的T.11803()是什么意思?
- 这个额外的关键字在这个 c++ 类声明中是什么意思?
- * 和 ** 在 C++ 函数声明中是什么意思?
- _T("xyz")是什么意思?
- #define Dbg(fmt,..) (0) 是什么意思? 警告:表达式无效
- 这行代码到底是什么意思?
- 在命名空间名称之前加上 :: 是什么意思?
- 从字符数组的元素中减去'a'是什么意思
- "friend"关键字在C++中是什么意思?
- 减法中的"0"是什么意思?
- 字符数组前面的加号是什么意思?
- 这里的字符串函数是什么意思