嘿伙计们,我明天有一个测验,我遇到了一些问题,寻找选择排序的递归程序的时间复杂度,任何人都可以解释它是如何 n^2 的。还有一个问题,一些排序算法循环的时间复杂度为 n/2,对于新手问题,/2 是什么意思。
#include <iostream>
using namespace std;
// recursive function to perform selection sort on subarray arr[i..n-1]
void selectionSort(int arr[], int i, int n)
{
// find the minimum element in the unsorted subarray[i..n-1]
// and swap it with arr[i]
int min = i;
for (int j = i + 1; j < n; j++)
{
// if arr[j] element is less, then it is the new minimum
if (arr[j] < arr[min])
min = j; // update …Run Code Online (Sandbox Code Playgroud)