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