阿里实习二面算法题 谁帮我看看 C++
阿里面试的算法题,这个是堆排序吗,谁能讲一讲。改进的堆排序吗?看半天没有很明白。
void SubProc(int a[], int s, int m)
{
//s是当前节点的位置,m是数组长度
int rc, j;
rc = a[s];
for (int j = (s << 1) + 1; j < m; j <<= 1)
{
if (j + 1 < m&&a[j] < a[j + 1]) j++;
if (rc > a[j]) break;
a[s] = a[j];
s = j;
}
a[s] = rc;
}
void MainProc(int a[], int n)
{
for (auto i = (n >> 1) - 1; i >= 0; i--)
SubProc(a, i, n);
for (auto i = n - 1; i >= 0; i--)
{
auto temp = a[0];
a[0] = a[i];
a[i] = temp;
SubProc(a, 0, i);
}
}
#阿里巴巴#{
//s是当前节点的位置,m是数组长度
int rc, j;
rc = a[s];
for (int j = (s << 1) + 1; j < m; j <<= 1)
{
if (j + 1 < m&&a[j] < a[j + 1]) j++;
if (rc > a[j]) break;
a[s] = a[j];
s = j;
}
a[s] = rc;
}
void MainProc(int a[], int n)
{
for (auto i = (n >> 1) - 1; i >= 0; i--)
SubProc(a, i, n);
for (auto i = n - 1; i >= 0; i--)
{
auto temp = a[0];
a[0] = a[i];
a[i] = temp;
SubProc(a, 0, i);
}
}