Algorithm-bubble sort and Quick Sort (Object-C)

Source: Internet
Author: User

Algorithm-bubble sort and Quick Sort (Object-C)
Bubble is the same as recursion. No matter what the level of everyone is, it can basically make up for writing and writing. In fact, quick sorting is mainly about data exchange, which is exchange sorting. However, quick sorting requires an understanding of the idea of governance, recursion is required for implementation, which makes it difficult to see the cloud in most cases. Suppose there is an unordered integer array index 0 1 2 3 4 5 6 value 15 32 8 99 12 17 36, ① take the 0-bit 15 as the reference value, then, locate less than 15 from the back and assign 12 to 0 in reverse order. ② locate more than 15 from the back and place 32 to position 4; ③ leave position 1 blank, then, find less than 15 in reverse order, locate more than 15 in positive order, and repeat the above process when the index reaches 3. There is basically nothing to say about Bubble sorting. Let's look at the code and create a new Sort class to process the sorting: // Sort. h // Algorithm // http://www.cnblogs.com/xiaofeixiang// Created by keso on 15/3/15. // Copyright (c) 2015 keso. all rights reserved. // # import <Foundation/Foundation. h> @ interface Sort: NSObject @ property (nonatomic, strong) NSMutableArray * dataSource;-(void) bubbleSort :( NSMutableArray *) dataSource;-(void) quickSort :( NSInteger) start endIndex :( NSInteger) end; @ end Sort. bubbleSort implementation in m: // bubble sort-(void) bubbleSort :( NSMutableArray *) dataSource {NSUInteger count = [d AtaSource count]; for (int I = 0; I <count; I ++) {for (int j = 0; j <count-i-1; j ++) {if ([dataSource [j] intValue]> [dataSource [j + 1] intValue]) {NSString * temp = dataSource [j]; dataSource [j] = dataSource [j + 1]; dataSource [j + 1] = temp ;}}for (NSInteger I = 0; I <[dataSource count]; I ++) {NSLog (@ "sort -- % @", dataSource [I]) ;}} the Bubble Sorting is relatively stable, but it is slow to move only two numbers at a time, if it is in the forward order, the time complexity is O (n). If it needs to be in the forward order, the event complexity O (n * n) will go through n (n-1) /2 comparisons, average event Replay Random degree O (n * n); fast sorting is A sort of Division exchange sorting proposed by C. R. A. Hoare in 1962. It adopts a sub-Governance Policy, usually called Divide-and-ConquerMethod ). The basic idea is as follows: 1. First retrieve a number from the array as the reference value, which can also be understood as an intermediate variable. 2. In the partitioning process, place all the numbers greater than this number to its right, and all the numbers smaller than or equal to it to its left. 3. Repeat the second step for the left and right intervals until each interval has only one number. Quick sorting is often used because the sorting efficiency is relatively high in several sorting methods that are the same as O (n * logn). In addition, the fast sorting method is also practical, it is also an essential Algorithm for getting out of the house. It is easy to understand and understand. The specific implementation can basically be written and understood. As for the deeper understanding, it depends on the actual operation process at work. Sort. h defines a QuickSort method, and an NSMutabArray is used to save the final result. Implementation: // quick sorting-(void) quickSort :( NSInteger) start endIndex :( NSInteger) end {if (start <end) {NSInteger standardValue = [_ dataSource [start] intValue]; NSInteger left = start, right = end; while (start <end) {// locate from the back. If the value following is greater than the reference value, decrease the value while (start <end & [_ dataSource [end] intValue]> standardValue) {end --;} // when the value is less than the benchmark value, assign the start value to the index in the array if (start <end) {_ dataSource [st Art] = _ dataSource [end]; start = start + 1;} // search for data from the past to the future. If the value is smaller than the reference value, incremental while (start <end & [_ dataSource [start] intValue] <standardValue) {start ++;} // greater than the benchmark value, assign the value of if (start <end) {_ dataSource [end] = _ dataSource [start]; end = end-1 ;}} to the data indexed as end in the array ;}} // when exiting, the value start and end are equal _ dataSource [start] = [NSString stringWithFormat: @ "% ld", (long) standardValue]; [self quickSort: left endIndex: end-1]; // process the left [self quickSort: end + 1 endIndex: Right]; // processing right} the call to the main function is as follows: NSMutableArray * data = [[NSMutableArray alloc] initWithObjects: @ "10", @ "88 ", @ "97", @ "33", @ "8", @ "73", @ "18", nil]; [sort bubbleSort: data]; sort. dataSource = data; [sort quickSort: 0 endIndex: [data count]-1]; for (int I = 0; I <[data count]; I ++) {NSLog (@ "sorting: % @", data [I]);} return 0; the Code may not be the most concise, but it should be better understood. If there is a problem, you can contact me at any time in the comment area ~

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.