缘由如何用C++解决一下问题_编程语言-CSDN问答
int a[]{1, 4, 7, 8, 5, 2, 3, 6, 9, 7}, n = 10, x = n, jh = 0, j = 0;px:if (j < n)
{//缘由https://ask.csdn.net/questions/8099444if (--x < 2 + j)x = n - 1, j += 3;if (x < n - 1 && a[x + 1] > a[x - 2])jh = a[x + 1], a[x + 1] = a[x - 2], a[x - 2] = jh;if (a[x] > a[x - 2])jh = a[x], a[x] = a[x - 2], a[x - 2] = jh;if (a[x] > a[x - 1])jh = a[x], a[x] = a[x - 1], a[x - 1] = jh;if (a[x - 1] > a[x - 2])jh = a[x - 1], a[x - 1] = a[x - 2], a[x - 2] = jh;goto px;
}
int a[]{1, 4, 7, 8, 5, 2, 3, 6, 9, 7}, n = 10, x = n, jh = 0;
//获取第三大的数,小于O(N)的自创算法.
px:if (x-- > 2)
{if (x < n - 1 && a[x + 1] > a[x - 2])jh = a[x + 1], a[x + 1] = a[x - 2], a[x - 2] = jh;if (a[x] > a[x - 2])jh = a[x], a[x] = a[x - 2], a[x - 2] = jh;if (a[x] > a[x - 1])jh = a[x], a[x] = a[x - 1], a[x - 1] = jh;if (a[x - 1] > a[x - 2])jh = a[x - 1], a[x - 1] = a[x - 2], a[x - 2] = jh;goto px;
}
void 新三个数排序的自创算法()
{int a[]{1, 4, 7, 8, 5, 2, 3, 6, 9, 7}, n = 10, x = n, jh = 0, j = 0, s = 0;px:if (j < n)
{//缘由https://ask.csdn.net/questions/8099444if (--x < 2 + j)x = n - 1, j += 3;if (x < n - 1 && a[x + 1] < a[x - 2])jh = a[x + 1], a[x + 1] = a[x - 2], a[x - 2] = jh;if (a[x] < a[x - 2])jh = a[x], a[x] = a[x - 2], a[x - 2] = jh;if (a[x] < a[x - 1])jh = a[x], a[x] = a[x - 1], a[x - 1] = jh;if (a[x - 1] < a[x - 2])jh = a[x - 1], a[x - 1] = a[x - 2], a[x - 2] = jh;++s;goto px;
}cout << s << "\n";
}
这是我厉年来很满意的一次排序算法设计,最好小于O(N)最坏O((NN/3)/2),若再写成双向效率更高,再写为递归速度更快。