Algorithm-Key Index counting method

Source: Internet
Author: User

The key index counting method is suitable for the integer divided into the smaller simple sorting method, the basic step is divided into four steps:

1. Statistics on the number of occurrences of each classification;

2. Convert the number of classifications to the corresponding index;

3. Sort the original array by the weight of the category in the middle array;

4. Assign the intermediate array after sorting to the original array;

Basic Definition

First, you define the required classes for sorting:

@interface keyindexmodel:nsobject@property  (assign,nonatomic)  nsinteger  key; @property  (Strong, nonatomic)  nsstring  *value;-(instancetype) Initwithkeyvalue: (Nsinteger) Key  value: (NSString *) value;@ End@interface keyindexsort:nsobject-(void) Sort: (Nsmutablearray *) DataSource Categorynumber: (nsinteger) number; @end

To implement a specific sorting method:

-(void) Sort: (Nsmutablearray *) DataSource Categorynumber: (nsinteger) number{Nsinteger len=[datasource Count];    Nsmutablearray *temparr=[[nsmutablearray Alloc]initwithcapacity:1];    Http://www.cnblogs.com/xiaofeixiang/for (Nsinteger i=0; i<len; i++) {[Temparr addobject:[nsnull null]];    } Nsmutablearray *count=[[nsmutablearray alloc]initwithcapacity:1];    for (Nsinteger i=0; i<number+1; i++) {[Count Addobject:[nsnumber numberwithinteger:0]];        }//Count the number of times per classification for (Nsinteger i=0;i<len; i++) {Keyindexmodel *model=datasource[i];    Count[model.key+1]=[nsnumber numberwithinteger:[count[model.key+1] integervalue]+1]; }//To convert the number of occurrences to an index, the first group 3 times, the second group 5 times, so that the index corresponding to the third group starting is 8 for (Nsinteger j=0; j<number; J + +) {Count[j+1]=[nsnumber n    UMBERWITHINTEGER:[COUNT[J] integervalue]+[count[j+1] integervalue];        }//To classify elements from top to bottom for (Nsinteger m=0; m<len; m++) {Keyindexmodel *model=datasource[m]; temparr[[count[Model.key] Integervalue]]=model;    Count[model.key]=[nsnumber Numberwithinteger:[count[model.key] integervalue]+1];    }//Reorder assignment for (Nsinteger i=0; i<len; i++) {datasource[i]=temparr[i]; }    }
sort test
        Nsmutablearray *datasource=[[nsmutablearray Alloc]initwithcapacity:1];        [DataSource addobject: [[Keyindexmodel alloc]initwithkeyvalue:0 value:@ "Flyelephant"]];        [DataSource addobject: [[Keyindexmodel alloc]initwithkeyvalue:1 value:@ "Keso"]];        [DataSource addobject: [[Keyindexmodel alloc]initwithkeyvalue:2 value:@ "Sananoro"]];        [DataSource addobject: [[Keyindexmodel alloc]initwithkeyvalue:3 value:@ "http://www.cnblogs.com/xiaofeixiang/"]];                [DataSource addobject: [[Keyindexmodel alloc]initwithkeyvalue:3 value:@ "ios technology: 228407086"]];        [DataSource addobject: [[Keyindexmodel alloc]initwithkeyvalue:2 value:@ "Beijing"]];        [DataSource addobject: [[Keyindexmodel alloc]initwithkeyvalue:1 value:@ "Shanghai"]];        [DataSource addobject: [[Keyindexmodel alloc]initwithkeyvalue:3 value:@ "Shenzhen"]];        [DataSource addobject: [[Keyindexmodel alloc]initwithkeyvalue:2 value:@ "Guangzhou"]]; [DataSource addobject: [[Keyindexmodel alloc]initwithkeyvalue:0 value:@ "Henan"];        Keyindexsort *indexsort=[[keyindexsort Alloc]init];         [Indexsort Sort:datasource Categorynumber:5]; [DataSource enumerateobjectsusingblock:^ (id _nonnull obj, nsuinteger idx, BOOL * _nonnull stop)             {Keyindexmodel *model=obj;         NSLog (@ "%ld--%@", Idx,model.value); }];

Sort Result:

Algorithm-Key Index counting method

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.