Skip to content

Commit

Permalink
Objective-C:11.Sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Scarlett Che committed Dec 12, 2018
1 parent 6b50ac0 commit 90afeee
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*.zip
*.tar.gz
*.rar
*.DS_Store

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
Expand Down
24 changes: 24 additions & 0 deletions object-c/11_Sort/Sort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Sort.h
// test1231231
//
// Created by Scarlett Che on 2018/12/12.
// Copyright © 2018 Scarlett Che. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Sort : NSObject
// 冒泡排序
+ (NSArray *)bubbleSortWithArray:(NSArray *)array;

// 插入排序
+ (NSArray *)insertionSortWithArray:(NSArray *)array;

// 选择排序
+ (NSArray *)selectionSortWithArray:(NSArray *)array;
@end

NS_ASSUME_NONNULL_END
86 changes: 86 additions & 0 deletions object-c/11_Sort/Sort.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// Sort.m
// test1231231
//
// Created by Scarlett Che on 2018/12/12.
// Copyright © 2018 Scarlett Che. All rights reserved.
//

#import "Sort.h"

@implementation Sort
// 冒泡排序
+ (NSArray *)bubbleSortWithArray:(NSArray *)array {
if (array.count <= 1) {
return array;
}

NSMutableArray *aryM = array.mutableCopy;

for (int i = 0; i < aryM.count - 1; i++) {
BOOL flag = NO; // 提前结束标记
for (int j = 0; j < aryM.count - i - 1; j++) {
NSInteger value1 = [aryM[j] integerValue];
NSInteger value2 = [aryM[j + 1] integerValue];

if (value1 > value2) {
flag = YES;
[aryM exchangeObjectAtIndex:j withObjectAtIndex:j+1];
}
}


if (flag == NO) {
// 提前结束
break;
}
}
return aryM.copy;
}

// 插入排序
+ (NSArray *)insertionSortWithArray:(NSArray *)array {
NSMutableArray *aryU = array.mutableCopy;

for (int i = 1; i < aryU.count; i++) {
NSInteger value = [aryU[i] integerValue];

for (int j = 0; j < i; j ++) {
NSInteger sortedValue = [aryU[j] integerValue];
if (value < sortedValue) {
id obj = aryU[i];
[aryU removeObjectAtIndex:i];
[aryU insertObject:obj atIndex:j];
break;
}
}
}
return aryU.copy;
}

// 选择排序
+ (NSArray *)selectionSortWithArray:(NSArray *)array {
if (array.count <= 1) {
return array;
}

NSMutableArray *aryM = array.mutableCopy;
for (int i = 0; i < array.count - 1; i++) {
NSInteger minIndex = NSNotFound;
NSInteger minValue = NSNotFound;
for (int j = i + 1; j < array.count - 1; j++) {
NSInteger tmp = [array[j] integerValue];
if (tmp < minValue) {
minValue = tmp;
minIndex = j;
}
}

if (minIndex != NSNotFound && minValue != NSNotFound && minValue < [array[i] integerValue]) {
[aryM exchangeObjectAtIndex:minIndex withObjectAtIndex:i];
}

}
return array;
}
@end

0 comments on commit 90afeee

Please sign in to comment.