Test1 has the highest time complexity. test2 has better time complexity than test1. test3 has the least time complexity and the least time used.
[Cpp]
/************************************ Test1 ** ********************/
// Program function: put all the negative numbers in the sequential storage mode before the integer
# Include <iostream>
Using namespace std;
/*
* Algorithm Description: locate the negative number and save it with a temporary variable. First, move all the numbers before the negative number to the back, and then place the negative number at the beginning.
*/
Int main ()
{
Int Array [10] = {2,-3, 4,-5, 6,-7, 8,-9, 10,-11 };
For (int I = 0; I <10; I ++)
{
Int temp1; // temp1 is used to store the negative number found
If (Array [I] <0)
{
Temp1 = Array [I];
For (int j = I; j> 0; j --)
{
Array [j] = Array [J-1];
}
Array [0] = temp1;
}
}
For (int k = 0; k <10; k ++)
{
Cout <Array [k] <"";
}
}
/*********************************** Test2 *** *******************************/
// Program function: put all the negative numbers in the sequential storage mode before the integer
# Include <iostream>
Using namespace std;
/*
* Algorithm Description: a variable is used to store the position of the negative number before the array.
*/
Int main ()
{
Int Array [10] = {2,-3, 4,-5, 6,-7, 8,-9, 10,-11}, k = 0;
For (int I = 0; I <10; I ++)
{
Int temp1; // temporary variable that temp1 uses to exchange data
If (Array [I] <0)
{
Temp1 = Array [I];
Array [I] = Array [k];
Array [k] = temp1;
K ++;
}
}
For (int k = 0; k <10; k ++)
{
Cout <Array [k] <"";
}
}
/*************************************** ** Test3 ************************************* ******/
// Program function: put all the negative numbers in the sequential storage mode before the integer
# Include <iostream>
Using namespace std;
/*
* Algorithm Description: It is equivalent to two pointers pointing to the header and the end of the table respectively.
*/
Int main ()
{
Int Array [10] = {2,-3, 4,-5, 6,-7, 8,-9, 10,-11}, k = 0;
For (int I = 9; I> k; I --)
{
Int temp1; // temp1 is used to store temporary variables for exchange.
If (Array [I] <0 & Array [k] <0)
{
K ++;
I ++;
}
If (Array [I] <0 & Array [k]> 0)
{
Temp1 = Array [I];
Array [I] = Array [k];
Array [k] = temp1;
K ++;
}
If (Array [I]> 0 & Array [k]> 0)
{}
If (Array [I]> 0 & Array [k] <0)
{}
}
For (int k = 0; k <10; k ++)
{
Cout <Array [k] <"";
}
}