交换排序
冒泡排序
void BubbleSort1(ElemType A[], int n)
{
int i, j,flag;
for(i=0;i<n-1;i++)
{
flag = 0;
for (j = n - 1; j > i; j--)
{
if (A[j - 1] > A[j])
{
swap(A[j - 1], A[j]);
flag = 1;
}
}
if (0 == flag)
{
break;
}
}
}
快速排序
int Partition(int* arr, int left, int right)
{
int k, i;
for (k = i = left; i < right; i++)
{
if (arr[i] < arr[right])
{
swap(arr[i], arr[k]);
k++;
}
}
swap(arr[k], arr[right]);
return k;
}
void QuickSort(ElemType A[], int low, int high)
{
if (low < high)
{
int pivotpos = Partition(A, low, high);
QuickSort(A, low, pivotpos - 1);
QuickSort(A, pivotpos + 1, high);
}
}
全部代码
#include
#include
#include
#include
typedef int ElemType;
typedef struct {
ElemType* elem;
int TableLen;
}SSTable;
void ST_Init(SSTable& ST, int len)
{
ST.TableLen = len;
ST.elem = (ElemType*)malloc(sizeof(ElemType) * ST.TableLen);
int i;
srand(time(NULL));
for ( i = 0; i < ST.TableLen; i++)
{
ST.elem[i] = rand()%100;
}
}
void ST_print(SSTable ST)
{
for (int i = 0; i < ST.TableLen; i++)
{
printf("%d ", ST.elem[i]);
}
printf("\n");
}
void swap(ElemType& a, ElemType& b)
{
ElemType tmp;
tmp = a;
a = b;
b = tmp;
}
void BubbleSort1(ElemType A[], int n)
{
int i, j,flag;
for(i=0;i<n-1;i++)
{
flag = 0;
for (j = n - 1; j > i; j--)
{
if (A[j - 1] > A[j])
{
swap(A[j - 1], A[j]);
flag = 1;
}
}
if (0 == flag)
{
break;
}
}
}
int Partition(int* arr, int left, int right)
{
int k, i;
for (k = i = left; i < right; i++)
{
if (arr[i] < arr[right])
{
swap(arr[i], arr[k]);
k++;
}
}
swap(arr[k], arr[right]);
return k;
}
void QuickSort(ElemType A[], int low, int high)
{
if (low < high)
{
int pivotpos = Partition(A, low, high);
QuickSort(A, low, pivotpos - 1);
QuickSort(A, pivotpos + 1, high);
}
}
int main()
{
SSTable ST;
ElemType A[10] = { 64,94,95,79,69,84,18,22,12,78 };
ST_Init(ST, 10);
ST_print(ST);
QuickSort(ST.elem, 0, 9);
ST_print(ST);
system("pause");
}
插入排序
直接插入排序
void InsertSort(ElemType A[], int n)
{
int i, j;
for (i = 2; i <= n; i++)
{
if (A[i] < A[i - 1])
{
A[0] = A[i];
for (j = i - 1; A[0] < A[j]; --j)
A[j + 1] = A[j];
A[j + 1] = A[0];
}
}
}
折半插入排序
void MidInsertSort(ElemType A[], int n)
{
int i, j, low, high, mid;
for (i = 2; i <= n; i++)
{
A[0] = A[i];
low = 1; high = i - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (A[mid] > A[0])
high = mid - 1;
else
low = mid + 1;
}
for (j = i - 1; j > high + 1; --j)
A[j + 1] = A[j];
A[high + 1] = A[0];
}
}
希尔排序
步长一直按照约定的减小
void ShellerSort(ElemType A[], int n)
{
int dk, i, j;
for (dk = n / 2; dk >= 1; dk = dk / 2)
{
for (i = dk + 1; i <= n; ++i)
{
if (A[i] < A[i - dk])
{
A[0] = A[i];
for (j = i - dk; j > 0 && A[0] < A[j]; j = j - dk)
A[j + dk] = A[j];
A[j + dk] = A[0];
}
}
}
}
全部代码
#include
#include
#include
typedef int ElemType;
typedef struct {
ElemType* elem;
int TableLen;
}SSTable;
void ST_Init(SSTable& ST, int len)
{
ST.TableLen = len + 1;
ST.elem = (ElemType*)malloc(sizeof(ElemType) * ST.TableLen);
int i;
srand(time(NULL));
for (i = 0; i < ST.TableLen; i++)
{
ST.elem[i] = rand() % 100;
}
}
void ST_print(SSTable ST)
{
for (int i = 0; i < ST.TableLen; i++)
{
printf("%3d ", ST.elem[i]);
}
printf("\n");
}
void InsertSort(ElemType A[], int n)
{
int i, j;
for (i = 2; i <= n; i++)
{
if (A[i] < A[i - 1])
{
A[0] = A[i];
for (j = i - 1; A[0] < A[j]; --j)
A[j + 1] = A[j];
A[j + 1] = A[0];
}
}
}
void MidInsertSort(ElemType A[], int n)
{
int i, j, low, high, mid;
for (i = 2; i <= n; i++)
{
A[0] = A[i];
low = 1; high = i - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (A[mid] > A[0])
high = mid - 1;
else
low = mid + 1;
}
for (j = i - 1; j > high + 1; --j)
A[j + 1] = A[j];
A[high + 1] = A[0];
}
}
void ShellerSort(ElemType A[], int n)
{
int dk, i, j;
for (dk = n / 2; dk >= 1; dk = dk / 2)
{
for (i = dk + 1; i <= n; ++i)
{
if (A[i] < A[i - dk])
{
A[0] = A[i];
for (j = i - dk; j > 0 && A[0] < A[j]; j = j - dk)
A[j + dk] = A[j];
A[j + dk] = A[0];
}
}
}
}
int main()
{
SSTable ST;
ST_Init(ST, 10);
ST_print(ST);
printf("\n希尔插入排序:\n");
ShellerSort(ST.elem,10);
ST_print(ST);
system("pause");
}