重拾算法,好久没有写C了, 找找感觉。#import "Sort.h"
@implementation Sort
#pragma mark - 快速排序
void fastSort();
// 返回枢轴位置
int partition(int a[],int low,int high);
// 递归方法
void qSort(int a[], int low, int high);
- (void)qSort {
NSLog(@"-------------------快速排序------------------------");
// 快速排序
fastSort();
}
void fastSort() {
int a[10];
for (int i = 9; i >= 0; i--) {
a[9-i] = i+10;
}
NSLog(@"开始排序---");
for (int i = 0; i <= 9; i++) {
printf("%d ",a[i]);
}
qSort(a, 0, 9);
printf("\n");
NSLog(@"排序结束---");
for (int i = 0; i <= 9; i++) {
printf("%d ",a[i]);
}
}
void qSort(int a[], int low, int high) {
if (low < high) {
int p = partition(a, low, high);
qSort(a, low, p-1);
qSort(a, p+1, high);
}
}
// 递归方法
int partition(int a[],int low,int high) {
int key = a[low];
// 保证key的左边比其小 key的右边比其大 (升序的话)
while (low < high) {
// 右半边 只要右边出现比枢轴小的值 就替换 如果一旦不满足条件了,那么就赋值。
while (low < high && a[high] >= key) high--;
a[low] = a[high];
// 左半边 只要左边出现比枢轴大的值 就替换
while (low < high && a[low] <= key) low++;
a[high] = a[low];
}
a[low] = key;
return low;
}
//
//int partition1 (int a[],int low ,int high) {
//
// int key = a[low];
// while (low < high) {
//
// while (low < high && a[high] >= key) high--;
// a[low] = a[high];
//
// while (low < high && a[low] <= key) low++;
// a[high] = a[low];
// }
// a[low] = key;
//
// return low;
//}
//void qSort2(int a[], int low, in high) {
//
// if (low < high) {
// int partition = partition1(a,low,high);
// qSort2(a, low, partition-1);
// qSort2(a, partition+1, high);
// }
//}
#pragma mark - 插入排序
void insSort(int a[], int n);
void print (int a[], int n,int i);
- (void)insertSort {
// 将一个记录插入到已经排好序的有序表中,从而得到一个新的,记录数为1的有序表。
// 关键点是要设置哨兵。
NSLog(@"-------------------插入排序------------------------");
NSLog(@"开始排序---");
int a[100];
for (int i = 100; i >= 0; i--) {
a[100-i] = i+10;
}
insSort(a, 100);
NSLog(@"排序结束---");
}
void insSort(int a[], int n) {
for (int i = 1; i < n; i++) {
// 如果第i个元素大于i-1个元素,直接插入,如果小于的话,则移动有序表。
// 如果后一个元素小于前一个元素
if (a[i] < a[i-1]) {
// 前一个元素的位置 将最新的元素存储起来做哨兵,以便后面方便寻找其插入位置。
int pre = i-1;
int key = a[i];
// 先直接替换,然后再判断应该把这个新的放哪
a[i] = a[i-1];
// 遍历mpre之前的点 以此来判断哨兵到底应该放哪
while (key < a[pre]) {
a[pre+1] = a[pre];
// 因为每次都是先赋值再减索引,索引最后放哨兵时应该索引+1
pre--;
}
a[pre+1] = key;
print(a, n, i);
}
}
}
void insSort1 (int a[],int n) {
for (int i = 1; i < n; i++) {
if (a[i] < a[i-1]) {
// 哨兵前一个索引
int pre = i-1;
// 哨兵
int key = a[i];
// 先将比哨兵大的向后移动
a[i] = a[i-1];
// 寻找哨兵合适的插入位置
while (key < a[pre]) {
a[pre+1] = a[pre];
pre--;
}
a[pre+1] = key;
print(a, n, i);
}
}
}
void print (int a[], int n,int i) {
for (int i = 0; i < 100; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
#pragma mark - 冒泡排序
void bubbleSort(int a[], int n);
- (void)bubbleSort {
NSLog(@"-------------------冒泡排序------------------------");
NSLog(@"开始排序---");
int a[100];
for (int i = 100; i >= 0; i--) {
a[100-i] = i;
}
for (int i = 0; i <= 100; i++) {
printf(" %d", a[i]);
}
printf("\n");
bubbleSort(a, 100);
NSLog(@"排序结束---");
}
void bubbleSort(int a[], int n) {
// 两两比较 依次交换
for (int i = 0; i <= n-1; i++) {
// 将最大值放在最后方
for(int j = 0; j <= n-i-1; j++) {
if (a[j] >= a[j+1]) {
int t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
for (int i = 0; i <= n; i++) {
printf(" %d", a[i]);
}
printf("\n");
}
#pragma mark - 选择排序
void selectSort(int a[], int n);
- (void)selectSort {
NSLog(@"-------------------选择排序------------------------");
NSLog(@"开始排序---");
int a[100];
for (int i = 100; i >= 0; i--) {
a[100-i] = i;
}
for (int i = 0; i <= 100; i++) {
printf(" %d", a[i]);
}
printf("\n");
selectSort(a, 100);
NSLog(@"排序结束---");
for (int i = 0; i <= 100; i++) {
printf(" %d", a[i]);
}
}
void selectSort(int a[], int n) {
for (int i = 0; i <= n-1; i++) {
for(int j = i+1; j <= n; j++) {
if (a[i] >= a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
8大排序 (1)
最新推荐文章于 2023-05-25 14:27:34 发布