Algorithm comparison
Keywords
- Two points
- recursion
- Dividing
- Backtracking
Bubble sort
Thought: Two cycles, the outer layer controls the number of cycles, the inner layer cycles, compares the data, and large data floats up (sinks)
#pragma mark - Objective-C //Bubbling sort- (void)bubbleSort:(id)array{ if (!([array isKindOfClass:[NSArray class]] || [array isKindOfClass:[NSMutableArray class]])) { NSLog(@"The passed parameter is not an array type"); return; } NSMutableArray *tmpArr; if ([array isKindOfClass:[NSMutableArray class]]) { tmpArr = array; }else{ tmpArr = [array mutableCopy]; } for (int i = 0; i<; i++) { for (int j = 0; j < -1; j++) { if ([tmpArr[j] compare:tmpArr[j+1]] == NSOrderedDescending) { [tmpArr exchangeObjectAtIndex:i withObjectAtIndex:j+1]; } } } NSLog(@"The result of sorting is: %@/n",tmpArr); } #pragma mark - C //Bubbling sortvoid bubble_sort(int arr[], const int size){ for (int i = 0; i < size; i++) { for (int j = 0; j<size -1 ; j++) { if (arr[j] > arr[j+1]) { swap(arr[j], arr[j+1]); } } } } void swap(int i,int j){ i = i + j; j = i - j; i = i - j; }
Quick sort
Thought: (Quick sorting is based on a idea called "binary") From the sequence, select an element as the reference and reorder the sequence. All elements smaller than the reference value are placed in front of the reference, and all elements larger than the reference value are placed behind the reference (the same number can be placed on either side. After this partition exits, the reference is in the middle of the sequence, recursively sorting the sub-sequences smaller than the reference value element and the sub-sequences larger than the reference value element.
/** Quick sort @param array Any type @param low The start position of the array to be sorted @param high End position of the array to be sorted */ - (void)quickSort:(NSMutableArray*)array low:(int)low high:(int)high{ if (array == nil || == 0) { return; } if (low >= high) { return; } //Take the median value int middle = low + (high - low)/2; NSNumber *prmt = array[middle]; int i = low; int j = high; //Start sorting so that left<prmt and right>prmt while (i <= j) { // while ([array[i] compare:prmt] == NSOrderedAscending) { // i++; // } while ([array[i] intValue] < [prmt intValue]) { i++; } // while ([array[j] compare:prmt] == NSOrderedDescending) while ([array[j] intValue] > [prmt intValue]) { j--; } if(i <= j){ [array exchangeObjectAtIndex:i withObjectAtIndex:j]; i++; j--; } } if (low < j) { [self quickSort:array low:low high:j]; } if (high > i) { [self quickSort:array low:i high:high]; } } //Quick sortint a[101],n;//Define global variables, these two variables need to be used in subfunctionsvoid quicksort(int left,int right) { int i,j,t,temp; if(left>right) return; temp=a[left]; //The reference number stored in temp is i=left; j=right; while(i!=j){ //The order is very important, you must start from the right first while(a[j]>=temp && i<j) j--; //Look for the one on the right while(a[i]<=temp && i<j) i++; //Swap the positions of two numbers in the array if(i<j){ t=a[i]; a[i]=a[j]; a[j]=t; } } //Finally return the reference number to the position a[left]=a[i]; a[i]=temp; quicksort(left,i-1);//Continue to process the left, here is a recursive process quicksort(i+1,right);//Continue to process the right side, here is a recursive process}
Select Sort
Thought: First find the smallest element in the unsorted sequence, store it at the starting position of the sorted sequence, and then continue to look for the smallest element from the remaining unsorted elements, then place it at the end of the sorted sequence, and so on until all elements are sorted.
Large column iOS common algorithms and applications s="line">6
- (void)selectSort:(NSMutableArray *)array { if(array == nil || == 0){ return; } int min_index; for (int i = 0; i < ; i++) { min_index = i; for (int j = i + 1; j<; j++) { if ([array[j] compare:array[min_index]] == NSOrderedAscending) { [array exchangeObjectAtIndex:j withObjectAtIndex:min_index]; } } } }
Insert sort
Thought: Starting from the first element, the element can be considered to have been sorted, take out the next element, scan backwards and forwards in the already sorted element sequence. If the element (sorted) is greater than the new element, move the element to the next position, repeat the above steps until the already sorted element is found, and the new element is inserted into the position.
- (void)inserSort:(NSMutableArray *)array { if(array == nil || == 0){ return; } for (int i = 0; i < ; i++) { NSNumber *temp = array[i]; int j = i-1; while (j >= 0 && [array[j] compare:temp] == NSOrderedDescending) { [array replaceObjectAtIndex:j+1 withObject:array[j]]; j--; } [array replaceObjectAtIndex:j+1 withObject:temp]; } }
Shell sort
Thought: First, divide the entire record sequence to be sorted into several sub-sequences for direct insertion and sorting. When the records in the entire sequence are "basically ordered", directly insertion and sorting the whole body one by one.
Optimization: Hill sorting is an improved method based on the following two properties of insert sorting:
(1) Insert sorting is efficient when operating on almost sorted data, which can achieve linear sorting efficiency.
(2) But insertion sort is generally inefficient, because insertion sort can only move data by one at a time
OC code implementation:
//Hill sort, the initial dk value is /2- (void)ShellSort:(NSMutableArray *)array dk:(int)dk { if(array == nil || == 0||dk>=){ return; } for (int i = 0; i < ; i ++) { NSNumber *temp = array[i]; int j = i - dk; //If the i-th element is greater than the i-1 element, insert it directly. If it is less than, move the ordered table and insert it while (j >= 0 && [array[j] compare:temp] == NSOrderedDescending) { [array replaceObjectAtIndex:j+dk withObject:array[j]]; j-=dk; } [array replaceObjectAtIndex:j+dk withObject:temp]; } while (dk>=1) { dk = dk/2; [self ShellSort:array dk:dk]; } }
Practical application
Compressed pictures
+(NSData *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength { // Compress by quality CGFloat compression = 1; NSData *data = UIImageJPEGRepresentation(image, compression); if ( < maxLength) return data; //Use dichotomy to improve performance CGFloat max = 1; CGFloat min = 0; for (int i = 0; i < 6; ++i) { compression = (max + min) / 2; data = UIImageJPEGRepresentation(image, compression); if ( < maxLength * 0.9) { min = compression; } else if ( > maxLength) { max = compression; } else { break; } } UIImage *resultImage = [UIImage imageWithData:data]; if ( < maxLength) return data; // Compress by size NSUInteger lastDataLength = 0; while ( > maxLength && != lastDataLength) { lastDataLength = ; CGFloat ratio = (CGFloat)maxLength / ; CGSize size = CGSizeMake((NSUInteger)( * sqrtf(ratio)), (NSUInteger)( * sqrtf(ratio))); // Use NSUInteger to prevent white blank UIGraphicsBeginImageContext(size); [resultImage drawInRect:CGRectMake(0, 0, , )]; resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); data = UIImageJPEGRepresentation(resultImage, compression); } return data; } +(NSData *)compressImage:(UIImage *)image { NSData *data=UIImageJPEGRepresentation(image, 1.0); if (>300*1024) { if (>1024*1024) {//1M and above data=UIImageJPEGRepresentation(image, 0.5); }else if (>300*1024) {//0.5M-1M data=UIImageJPEGRepresentation(image, 0.8); } } return data; }
The above is all the knowledge points introduced this time. Thank you for your study and support.