Common iOS algorithms (Binary bubble selection)

Source: Internet
Author: User

Common iOS algorithms (Binary bubble selection)
Bipartite: Average time complexity: O (log2n) int halfFuntion (int a [], int length, int number) {int start = 0; int end = length-1; int index = 0; while (start <end) {index = start + (end-start)/2 if (a [index] = number) {return index ;} else if (a [index] <number) {start = index + 1;} else {end = index-1 ;}} return index ;}

Bubble Sorting:

/**

Average time complexity: O (n2)

 

Space complexity: O (1) (used for exchange)

 

Stability: Stability

*/

Void paoFuntion (int a [], int length ){

For (int I = 0; I <length-1; I ++ ){

For (int j = 0; j <length-1-I; j ++ ){

If (a [j]> a [j + 1]) {

Int temp = a [j];

A [j] = a [j + 1];

A [j + 1] = temp;

}

}

}

}



Select sort:

/**

Average time complexity: O (n2)

 

Spatial complexity: O (1) (used for exchange and record indexing)

 

Stability: unstable (for example, in sequence [5, 5, 3], the first step is to exchange the first [5] with [3], resulting in the first 5 moving to the second 5)

*/

Void chooseFuntion (int a [], int length ){

For (int I = 0; I <length-1; I ++ ){

For (int j = I + 1; j <length; j ++ ){

If (a [I]> a [j]) {

Int temp = a [j];

A [j] = a [I];

A [I] = temp;

}

}

}

}

Related Article

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.