冒泡
/*************************************************************************
> File Name: buble.cpp
> Author: dulun
> Mail: dulun@xiyoulinux.org
> Created Time:
************************************************************************/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define LL long long
using namespace std;
const int N = 50086;
#define SWAP(a, b, t) ( (t) = (a) , (a) = (b) , (b) = (t) )
int a[] = {3, 4, 5, 6, 2, 1, 9, 8, 7, -1};
int t;
void pArray(int n)
{
for(int i = 0; i < n; i++)
{
printf("%d\n", a[i]);
}
}
int main()
{
int n = sizeof(a) / sizeof(int );
pArray(n);
for(int i = 0; i < n-1; i++)
{
int flag = 0;
or(int j = 0; j < n -1 - i ; j++)
{
if(a[j] > a[j+1])
{
flag = 1;
SWAP(a[j], a[j+1], t);
}
}
if(flag == 0) break;
}
printf("\n\n");
pArray(n);
return 0;
}
选择
/*************************************************************************
> File Name: choose.cpp
> Author: dulun
> Mail: dulun@xiyoulinux.org
> Created Time:
************************************************************************/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define LL long long
using namespace std;
const int N = 50086;
int a[] = {1, 3, 2, 5, 4, 8, 6, 9, 7};
void pArray(int n)
{
for(int i = 0; i < n; i++)
{
printf("%d\n", a[i]);
}
}
void choose_sort(int n)
{
int t, min, k;
for(int i = 0; i < n; i++)
{
min = a[i];
k = i;
for(int j = i+1; j < n; j++)
{
if(a[j] < a[i])
{
min = t;
k = j;
}
}
t = a[i];
a[i] = a[k];
a[k] = t;
}
}
int main()
{
int n = sizeof(a) / sizeof(int);
pArray(n);
printf("\n\n");
choose_sort(n);
pArray(n);
return 0;
}
快排
/*************************************************************************
> File Name: qsort.cpp
> Author: dulun
> Mail: dulun@xiyoulinux.org
> Created Time:
************************************************************************/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define LL long long
using namespace std;
const int N = 50086;
int a[] = {1, 5, 2, 3, 8, 6, 9, 4, 7};
void qsort(int l, int r)
{
if( l >= r ) return ;
int i = l , j = r;
int k = a[l];
while(i < j)
{
while( i < j && a[j] >= k )
{
j--;
}
a[i] = a[j];
while( i < j && a[i] <= k )
{
i++;
}
a[j] = a[i];
}
a[i] = k;
qsort(l, i-1);
qsort(i+1, r);
}
int main()
{
int n = sizeof(a) / sizeof(int);
for(int i = 0; i < n; i++)
{
printf("%d\n", a[i]);
}
qsort(0, n-1);
printf("\n\n");
for(int i = 0; i < n; i++)
{
printf("%d\n", a[i]);
}
return 0;
}
归并
/*************************************************************************
> File Name: merge.cpp
> Author: dulun
> Mail: dulun@xiyoulinux.org
> Created Time:
************************************************************************/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#define LL long long
using namespace std;
const int N = 50086;
int a[] = {1, 5, 2, 3, 7, 4, 9, 8, 6};
void merge(int l, int m, int r)
{
int i = l;
int j = m+1;
int k = 0;
int * tmp = new int[r-l + 1];
while( i <= m && j <= r )
{
if(a[i] < a[j])
{
tmp[k++] = a[i++];
}
else{
tmp[k++] = a[j++];
}
}
while( i <= m )
{
tmp[k++] = a[i++];
}
while( j <= r )
{
tmp[k++] = a[j++];
}
for(int i = 0; i < k; i++)
{
a[l+i] = tmp[i];
}
}
void mergesort(int l, int r)
{
if( l >= r ) return;
int m = (l + r) >> 1;
mergesort(l, m);
mergesort(m+1, r);
merge(l, m, r);
}
void pArray(int n)
{
for(int i = 0; i < n; i++)
{
printf("%d \n", a[i]);
}
return;
}
int main()
{
int n = sizeof(a) / sizeof(int);
pArray(n);
mergesort(0, n-1);
printf("\n\n");
pArray(n);
}