Algorithm--Sort

Source: Internet
Author: User

Bubble Sort:

Nsmutablearray *p = [[Nsmutablearray alloc] Initwithobjects:@"3",@"5",@"4",@"1", nil];  for(inti =0; I<[p Count]; i++)    {         for(intj=i+1; J<[p Count]; J + +)        {            intA =[[P objectatindex:i] intvalue]; //NSLog (@ "a =%d", a);            intb =[[P objectatindex:j] intvalue]; //NSLog (@ "B =%d", b); //NSLog (@ "------");            if(A >b) {[P replaceobjectatindex:i withobject:[nsstring stringWithFormat:@"%d", b]]; [P replaceobjectatindex:j withobject:[nsstring stringWithFormat:@"%d", A]]; }}} NSLog (@"%@", p);

Nsarray * Testarray = @[@ A,@188,@ -,@ -,@ -,@ the]; Nsmutablearray* NA =[Nsmutablearray Arraywitharray:testarray];  for(Nsinteger i =0; i < Na.count; i++) {//number of Outer loops         for(Nsinteger j =0; J < Na.count-1I J + +) {//The number of inner loops, (the outer layer does not loop 1 times the last position in the array to get 1 maximum values, all here the number of cycles is (total number-1-i) times)Nsinteger temp = [Na[j +1] IntegerValue]; NSLog (@"%d", temp); if([Na[j] integervalue] > [Na[j +1] IntegerValue]) {//If the previous one is greater than the next one, the swap positionNa[j +1]  =Na[j]; NA[J]=[NSNumber numberwithint:temp]; } NSLog (@"%@", NA); }} NSLog (@"%@", NA);

Algorithm--Sort

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.