ios:oc implements sorting algorithm

The following is the quick sort, bubble sort, direct insertion sort and half-insertion sort I implemented with oc, Hill sort, heap sort, direct selection sort



/***************** ****************Quicksort start******************************** */
//Then take the current one and take the first one, first find the position of the first one, then divide it into two subsets of left and right, and continue to perform segmentation on left and right respectively (same as above)

-(void)QuickSort:(NSMutableArray *)list StartIndex:(NSInteger)startIndex EndIndex:(NSInteger)endIndex{
   
    if(startIndex >= endIndex)return;
   
    NSNumber * temp = [list objectAtIndex:startIndex];
    NSInteger tempIndex = startIndex; //Temporary index processing swap position (ie position of the next swapped object)
   
    for(int i = startIndex + 1 ; i <= endIndex ; i++){
       
        NSNumber *t = [list objectAtIndex:i];
       
        if([temp intValue] > [t intValue]){
           
            tempIndex = tempIndex + 1;
           
            [list exchangeObjectAtIndex:tempIndex withObjectAtIndex:i];
           
        }
       
    }
   
    [list exchangeObjectAtIndex:tempIndex withObjectAtIndex:startIndex];
    [self QuickSort:list StartIndex:startIndex EndIndex:tempIndex-1];
    [self QuickSort:list StartIndex:tempIndex+1 EndIndex:endIndex ];

}

/********************************Quicksort end************ ************************/

/**************************** *****Bubble sort start**********************************/
//Take the first one Contrast with its adjacency, if it is larger then swap
-(void)BubbleSort:(NSMutableArray *)list{
   
    for (int j = 1; j<= [list count]; j++) {
           
        for(int i = 0 ;i < j ; i++){
           
            if(i == [list count]-1)return;
           
            NSInteger a1 = [[list objectAtIndex:i] intValue];
            NSInteger a2 = [[list objectAtIndex:i+1] intValue];
           
            if(a1 > a2){
                [list exchangeObjectAtIndex:i withObjectAtIndex:i+1];
            }
           
        }
       
    }
   
}
/********************************Bubble sort end**************** ********************/

/**************************** ****Direct insertion sort start**********************************/
//From the unordered table Take out the first element and insert it into the appropriate position of the sorted list, so that the sorted list is still
sorted-(void)InsertSort:(NSMutableArray *)list{
   
    for(int i = 1 ; i < [list count] ; i++) {
       
       
        int j = i;
        NSInteger temp= [[list objectAtIndex:i] intValue];
       
        while (j > 0 && temp < [[list objectAtIndex:j - 1]intValue]) {
           
            [list replaceObjectAtIndex:j withObject:[list objectAtIndex:(j-1)]];
            //list[j] = list[j -1];
            j--;
       
        }
        [list replaceObjectAtIndex:j withObject:[NSNumber numberWithInt:temp]];
        //list[j] = temp;
       
    }
   
}
/************** ****************Insert sort end**************************** *****/

/********************************Insertion sort start******** ****************************/
//Remove the first element from the unordered table, and use the half to find the appropriate one to insert into the ordered table Position, so that the ordered list is still
ordered-(void)BinaryInsertSort:(NSMutableArray *)list{
   
    //The index starts from 1 and defaults to the first element to be the default ordered list to compare from the second element
    for(int i = 1 ; i < [list count] ; i++){
       
        //binary search start
        NSInteger temp= [[list objectAtIndex:i] intValue];
       
        int left = 0;
        int right = i - 1;
       
        while (left <= right) {
           
            int middle = (left + right)/2;
           
            if(temp < [[list objectAtIndex:middle] intValue]){
               
                right = middle - 1;
               
            }else{
               
                left = middle + 1;
           
            }
           
        }
        // binary search end
       
        //Move 3,5,6,[4] 4 is the current target object. Use binary search to find 4 should be in the position where there is a sequel {3,5,6}, and then move backward i.e. {3,5,6, [4]}-->{3,[4],5,6}
        for(int j = i ; j > left; j--){
           
            [list replaceObjectAtIndex:j withObject:[list objectAtIndex:j-1]];
           
        }
        [list replaceObjectAtIndex:left withObject:[NSNumber numberWithInt:temp]];
       
    }
   
   
}
/**************** ****************Flip Half Insertion Sort end**************************** *****/

/******************************** Hill sort start******** ****************************/
//Optimize direct insertion sorting, create a gap to divide the table, and divide each child after the division Set for direct insertion sorting until gap==1 ends
-(void)shellSort:(NSMutableArray *)list{

    int gap = [list count] / 2;
   
    while (gap >= 1) {
       
       
        for(int i = gap ; i < [list count]; i++){
       
            NSInteger temp = [[list objectAtIndex:i] intValue];
            int j = i;
           
            while (j >= gap && temp < [[list objectAtIndex:(j - gap)] intValue]) {
                [list replaceObjectAtIndex:j withObject:[list objectAtIndex:j-gap]];
                j -= gap;
            }
            [list replaceObjectAtIndex :j withObject:[NSNumber numberWithInt:temp]];
           
           
        }
       
        gap = gap / 2;
    }



}
/**************************** ***Hill sort end**********************************/



/******* ************************Heapsort start************************ **********/
//Create max heap heap max/min priority queue-
(void)CreateBiggestHeap:(NSMutableArray *)list Count:(NSInteger)count{
   
    //int count = [list count ];
    int lastParentIndex = (count - 2)/2;
    for(int i = lastParentIndex; i >= 0 ; i--){
       
        NSInteger parentIndex = i;
        NSInteger parentNode = [[list objectAtIndex:parentIndex] intValue];
       
       
        //Get left The child node is the current child node
        int currentChildIndex = 2*i + 1;
        //
       
        while (currentChildIndex <= count - 1) {
           
            NSInteger leftChildNode = [[list objectAtIndex:(currentChildIndex)] intValue];
           
            if((currentChildIndex + 1 ) <= count-1){//Indicates that there is a right child node
               
                //Read the right child node
                int rightChildIndex =currentChildIndex + 1;
                NSInteger rightChildNode = [[list objectAtIndex:(rightChildIndex)] intValue];
               
                //If the right child node is the largest
                if(rightChildNode > leftChildNode && rightChildNode > parentNode){
                    [list exchangeObjectAtIndex:parentIndex withObjectAtIndex:rightChildIndex];
                    currentChildIndex = rightChildIndex;//The right child node is the current child node and continues to loop
                //left Maximum child node
                }else if(leftChildNode > rightChildNode && leftChildNode > parentNode){
                    [list exchangeObjectAtIndex:parentIndex withObjectAtIndex:currentChildIndex];
                }
               
            }else{
               
                if(leftChildNode > parentNode){
                    [list exchangeObjectAtIndex:parentIndex withObjectAtIndex:currentChildIndex];
                   
                }
               
            }
           
            //Update the parent node and the next child node
            parentIndex = currentChildIndex;
            currentChildIndex = 2*currentChildIndex + 1;
                       
        }

    }
   
}

//The maximum heap is executed each time (the index needs to move forward to exclude the already arranged maximum heap head The element is swapped to the end of the list)
-(void)HeapSort:(NSMutableArray *)list{
   
    for(int i = [list count] ; i > 0; i--){
       
        [self CreateBiggestHeap:list Count:i] ;
       
        //NSLog(@"%@",list);
       
        [list exchangeObjectAtIndex:(i-1) withObjectAtIndex:0];
       
    }
   
}


/******************* ************Heap Sort end**********************************/

/********************************Direct selection sort start************** ********************/
//Select the smallest in the object set, if it is not the first one, then exchange with the first one and select the smallest in the remaining object set Perform previous steps
- (void)SelectSort:(NSMutableArray *)list{
   
    for(int i = 0 ; i<[list count]; i++){
       
        int k = i;
        for(int j = i+1 ; j<[ list count]; j++){
           
            NSInteger jvalue = [[list objectAtIndex:j] intValue];
            NSInteger kvalue = [[list objectAtIndex:k] intValue];
           
            if(jvalue < kvalue){
                k = j;
            }
           
        }
        if(k ! = i){
            [list exchangeObjectAtIndex:i withObjectAtIndex:k];
        }
       
    }
   
}

/********************************Direct selection sort end************** ********************/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326397764&siteId=291194637