Summary objective-c Common algorithm

Source: Internet
Author: User

Today is Sunday, want to sleep until 10 o'clock to get up, the result thinks oneself too extravagant, cannot do so, then set the alarm clock to 6:30; The result finally 9:36 woke up, get up, no reason late, OK, even if too tired bar, Sunday forgive oneself once. Finally to noon, tossing and turning, with objective-c to achieve a calculator program, debugging not arithmetic calculator algorithm, always poor so a little, but still a little bit, not run up, finally decided to go out to do something and then go back to the classroom to ask classmates ..., and later found that these algorithms are very important , even if fall down to climb up also to remember, remember, remember, by the arithmetic algorithm to get the revelation, and then find data summary of objective-c commonly used algorithm is as follows:

This is mainly a summary of the sorting algorithm, other algorithms to be summarized

#import <Foundation/Foundation.h>
10
@interface sort:nsobject{
12
13}
14
15//Select sort
-(void) Selectsortwitharray: (Nsarray *) AData;
17//Insert Sort
-(void) Insertsortwitharray: (Nsarray *) AData;
19//Quick Sort
-(void) Quicksortwitharray: (Nsarray *) AData;
21st
(void) Swapwithdata: (Nsmutablearray *) aData index1: (Nsinteger) Index1index2: (Nsinteger) Index2;
23
24
@end

9 #import "Sort.h"
10
@interface Sort ()
(void) Quicksortwitharray: (Nsarray *) AData Left: (Nsinteger) LeftRight: (Nsinteger) right;
@end
14
@implementation Sort
16
-(ID) init
18 {
self = [super init];
if (self) {
//initialization code here.
22}
23
return self;
25}
26
-(void) Selectsortwitharray: (Nsarray *) adata{
Nsmutablearray *data =[[nsmutablearray Alloc]initwitharray:adata];
for (int i=0; I<[data count]-1;i++) {
-int M =i;
for (Int J =i+1; j<[datacount]; J + +) {
if ([Data objectatindex:j]< [Data objectatindex:m]) {
m = j;
34}
35}
if (m! = i) {
PNS [self swapwithdata:dataindex1:m index2:i];
38}
39}
NSLog (@ "Select results after sorting:%@", [Data description]);
[Data release];
42}
43
-(void) Insertsortwitharray: (Nsarray *) adata{
Nsmutablearray *data =[[nsmutablearray Alloc]initwitharray:adata];
(int i = 1; i < [datacount]; i++) {
ID tmp = [dataobjectatindex:i];
int j = i-1;
(J! =-1 &&[data objectatindex:j] > tmp) {
[Datareplaceobjectatindex:j+1 Withobject:[data objectatindex:j];
Wuyi j--;
52}
[Data replaceobjectatindex:j+1withobject:tmp];
54}
NSLog (@ "Insert sorted Result:%@", [Data description]);
[Data release];
57}
58
-(void) Quicksortwitharray: (Nsarray *) adata{
Nsmutablearray *data = [[Nsmutablearrayalloc] initwitharray:adata];
[Self quicksortwitharray:dataleft:0 right:[adata count]-1];
NSLog (@ "Fast-sorting results:%@", [Data description]);
[Data release];
64
65}
66
-(void) Quicksortwitharray: (Nsmutablearray *) AData Left: (Nsinteger) LeftRight: (Nsinteger) right{
if (right > left) {
Nsinteger i = left;
Nsinteger J = right + 1;
(true) {
(I+1 < [adatacount] && [aData objectatindex:++i] < [aData Objectatindex:left]);
while (J-1 > -1&& [aData objectatindex:--j] > [aData objectatindex:left]);
if (i >= j) {
a break;
76}
[Self swapwithdata:adataindex1:i index2:j];
78}
[Self swapWithData:aDataindex1:left index2:j];
[Self quickSortWithArray:aDataleft:left right:j-1];
Bayi [self quicksortwitharray:adataleft:j+1 right:right];
82}
83}
84
85
dealloc{-(void)
[Super Dealloc];
88}
89
-(void) Swapwithdata: (Nsmutablearray *) aData index1: (Nsinteger) Index1index2: (Nsinteger) index2{
NSNumber *tmp = [ADATAOBJECTATINDEX:INDEX1];
[AData Replaceobjectatindex:index1withobject:[adata Objectatindex:index2]];
[AData replaceObjectAtIndex:index2withObject:tmp];
94}
95
@end

Here's an example to test:


9 #import <Foundation/Foundation.h>
Ten #import "Sort.h"
11
#define KSIZE 20
#define KMAX 100
14
int main (int argc, const char * argv[])
16 {
17
NSAutoreleasePool * Pool =[[nsautoreleasepool alloc] init];
19
//Insert code here ...
NSLog (@ "Hello, world!");
22
Nsmutablearray *data =[[nsmutablearray alloc] initwithcapacity:ksize];
24
(int i =0;i<ksize;i++) {
u_int32_t x = arc4random ()%kmax;//0~kmax
NSNumber *num = [[Nsnumberalloc] initwithint:x];
[Data addobject:num];
[Num release];
30}
31
NSLog (@ "Data before sorting:%@", [datadescription]);
33
*sort sort = [[Sort alloc] init];
[Sort Selectsortwitharray:data];
[Sort Insertsortwitharray:data];
Panax Notoginseng [Sort quicksortwitharray:data];
[Sort release];
[Data release];
[Pool drain];
return 0;
42}

Summary objective-c Common algorithm

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.