//冒泡
int* maopao(int a[], int n)
{
int temp = 0;
for (int i = 0; i<n-1; i++) {
for (int j = 0; j<n-1-i; j++) {
if (a[j] > a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = a[j];
}
}
}
return a;
}
//插入排序
int* insert(int a[],int n)
{
//把一个数组分成两段,前段为插入后新队列,后段为待插入队列
for (int i = 1; i<n; i++) {
//待插入值
int temp = a[i];
//寻找插入位置
int j = i - 1;
for (; j>0; j--) {
if (a[j] > temp) {
a[j+1] = a[j];
}
}
a[j] = temp;
}
return a;
}
//折半插入排序
//也就是在寻找位置的时候,因为带插入队列已经是排序好的,可以采用二分查找,来快速定位待插入位置
int *twoInsert(int a[], int n)
{
//把一个数组分成两段,前段为插入后新队列,后段为待插入队列
for (int i = 1; i<n; i++) {
//待插入值
int temp = a[i];
//寻找插入位置
int k = i - 1;//已排序队列和未排序队列的分界线
//折半定位
int low = 0;
int high = k;
int middle = 0;
while (low < high) {
middle = (high + low)/2;
if (a[middle] > temp) {
//向前找
high = MAX(middle - 1, 0);
}else if (a[middle] <= temp) {
//向后找
low = MIN(middle + 1, k);
}
}
//确定位置
int position = low;
if (a[low] > temp) {
position = low;
}else {
position = low + 1;
}
//后移
int j = k+1;
while (j > position) {
a[j] = a[j - 1];
j--;
}
//插入
a[position] = temp;
}
return a;
}
//二分查找
int twoSearch(int a[], int n, int value)
{
int low = 0;
int hight = n-1;
int middle = 0;
while (low <= hight) {
middle = (hight + low)/2;
if (a[middle] > value) {
//向前找
hight = middle - 1;
}else if (a[middle] < value) {
//向后找
low = middle + 1;
}else {
return middle;
}
}
return -1;
}
//希尔排序,插入排序的基础上进行改造,按分量d来分组,逐渐减少d分量,直至d==1,排序结束
//因为分组的原因,希尔排序不稳定
int *shellSort(int a[], int n)
{
//分量d递减分组递增
for (int d = n/5; d>0; d--) {
//遍历所有组,分组0 - d
for (int j = 0; j < d; j++) {
//组内排序
for (int i = j; i + d < n; i += d) {
//将j+1元素插入0-j有序组中
int k = i; //待插入序列的最后一个
int temp = a[k + d]; //被插入元素
while (k >= 0) {
if (a[k] > temp) {
a[k + d] = a[k];
k = k - d;
}else {
break;
}
}
a[k+d] = temp;
}
}
}
return a;
}
//快速排序
void kuaisu(int a[], int n)
{
if (n <= 1) {
return;
}
int low = 0;
int high = n - 1;
int t = low;
int temp = a[t];
while (low < high) {
while (a[high] >= a[t] && high > low) {
high --;
}
if (a[high] < a[t]) {
a[t] = a[high];
a[high] = temp;
t = high;
}
while (a[low] <= a[t] && low < high) {
low ++;
}
if (a[low] > a[t]) {
a[t] = a[low];
a[low] = temp;
t = low;
}
if (low == high) {
kuaisu(&a[0], low);
kuaisu(&a[high+1], n - high - 1);
}
}
return ;
}
void swap(int a[],int p0,int p1)
{
int temp = a[p0];
a[p0] = a[p1];
a[p1] = temp;
}
#pragma mark -- 堆排序
void balance(int a[],int len,int root/*1-n*/)
{
if (root < 1) {
NSLog(@"balance wrong root");
return;
}
int right = 2 * root + 1;
int left = 2 * root;
//C数组下标从0开始,所以-1
root -= 1;
right -= 1;
left -= 1;
if (len > right) {
int big = right;
if (a[right] <= a[left]) {
big = left;
}
if (a[root] < a[big]) {
swap(a, root, big);
balance(a, len, big + 1);
}
}else if (len > left) {
if (a[root] < a[left]) {
swap(a, root, left);
balance(a, len, left + 1);
}
}
}
void buildHeap(int a[],int len)
{
for (int i = len/2; i > 0; i--) {
balance(a, len, i);
}
}
void heapSort(int a[], int len)
{
buildHeap(a, len);
while (len > 0) {
swap(a, len-1, 0);
len --;
balance(a, len, 1);
}
}
void main()
{
int a[100];
for (int i = 0; i<100; i++) {
a[i] = rand()%1000;
}
heapSort(a, 100);
// kuaisu(a, 100);
// int *b = shellSort(a, 100);
// int *b = maopao(a,100);
// int *b = insert(a, 100);
// int a[] = {4,7,3,8,2,9};
// int *b = twoInsert(a, 6);
// int *b = shellSort(a, 6);
for (int i = 0; i<100; i++) {
NSLog(@"%d",a[i]);
}
}