iOS Bubble Sort, Direct Insert Sort, Hill Sort

Keywords: Mobile iOS

1. Preface:

As an IOS development engineer, I try to write the order when I'm free at work. I feel that I should write it out quickly. As a result, I debug and correct it in the process of modification and modification. It's necessary to correct the problem of having high eyes and low hands. Everything has to be knocked before it can be remembered more firmly. Let's record it.

2. Bubble Sorting


Principle:
1. Compare adjacent elements. If the first one is bigger than the second one, exchange the two.
2. Do the same work for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. At this point, the last element should be the largest number.
3. Repeat the above steps for all elements except the last one.
4. Continue repeating the above steps for fewer and fewer elements each time until no pair of numbers need to be compared.

- (void)bubbleSort{ //Bubble sort
    for (int k = 0; k<self.sortArray.count; k++) {
        for (int i = 0; i<self.sortArray.count; i++) {
            NSString *fontItem = self.sortArray[i];
            if(i<self.sortArray.count-1){
                NSString *backItem = self.sortArray[i+1];
                if([fontItem integerValue] > [backItem integerValue]){
                    [self.sortArray replaceObjectAtIndex:i withObject:backItem];
                    [self.sortArray replaceObjectAtIndex:i+1 withObject:fontItem];
                }
            }
        }
    }
    NSLog(@"The order after bubbling is:%@",self.sortArray);
}

3 insertion sort


Principle:
By comparing the current element with all the previous elements, insert the current element in front of the minimum element that has been determined, and sort it directly after repeating the operation.

- (void)dirInsert{ //Direct insertion
    for (int i = 0; i<self.sortArray.count; i++) {
        NSString *fontItem = self.sortArray[i];
        if(i<self.sortArray.count-1){
            NSString *backItem = self.sortArray[i+1];
            if([fontItem integerValue] > [backItem integerValue]){
                for (int k =0 ; k<i; k++) {
                    NSString *searchInFont = self.sortArray[k];
                    if([backItem integerValue] <=[searchInFont integerValue]){
                        [self.sortArray removeObjectAtIndex:i+1];
                        [self.sortArray insertObject:backItem atIndex:k];
                        break;
                    }
                }
                
            }
        }
    }
    NSLog(@"The order after direct insertion is:%@",self.sortArray);
}

4 Hill Sort

Principle:
Hill sorting method (reduced increment method) belongs to insertion sort, which divides the whole unordered sequence into several small sub-sequences for insertion sort.

- (void)shellSort{
    int gap = (int)self.sortArray.count / 2;
    while (gap >= 1) {
        for(int i = gap ; i < [self.sortArray count]; i++){
            NSInteger temp = [[self.sortArray objectAtIndex:i] intValue];
            int j = i;
            while (j >= gap && temp < [[self.sortArray objectAtIndex:(j - gap)] intValue]) {
                [self.sortArray replaceObjectAtIndex:j withObject:[self.sortArray objectAtIndex:j-gap]];
                j -= gap;
            }
            [self.sortArray replaceObjectAtIndex:j withObject:[NSNumber numberWithInteger:temp]];
        }
        gap = gap / 2;
    }
    NSLog(@"Hill ranks:%@",self.sortArray);


}

Posted by mysterbx on Thu, 24 Jan 2019 15:57:13 -0800