Three algorithms that place all negative numbers before positive numbers in sequential storage mode

Source: Internet
Author: User

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] <"";
}
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.