数据结构 排序 交换排序
2007-04-12 15:36
417 查看
(1)冒泡排序
头文件:
#define MAXSIZE 20
#define EQ(a, b) ((a) == (b))
#define LT(a, b) ((a) < (b))
#define LQ(a, b) ((a) <= (b))
typedef int KeyType;
typedef int InfoType;
typedef struct ...{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct ...{
RedType r[MAXSIZE + 1];
int length;
}SqList;
源文件:
#include "sort.h"
#include "stdio.h"
void init(SqList &s, int w[], int n)...{
s.length = n;
for(int i = 1; i <= n; i++)...{
s.r[i].key = w[i - 1];
}
}
void show(SqList s)...{
for(int i = 1; i <= s.length; i++)...{
printf("%d ", s.r[i]);
}
printf(" ");
}
void bubbleSort(SqList &s)...{
int flag = 1;
for(int i = s.length; i >= 1 && flag; i--)...{
flag = 0;
for(int j = 1; j <= i; j++)...{
if(LT(s.r[j].key, s.r[j - 1].key))...{
RedType rc = s.r[j - 1];
s.r[j - 1] = s.r[j];
s.r[j] = rc;
flag = 1;
}
}
}
}
void main()...{
int w[] = ...{49, 38, 65, 97, 76, 13, 27, 49};
int n = 8;
SqList s;
init(s, w, n);
bubbleSort(s);
show(s);
}
程序执行结果:
13 27 38 49 49 65 76 97
Press any key to continue
程序的复杂度为:O(n * n)。
(2)快速排序
源文件修改:
//use r[low] as pivot to part from low to high.
int partition(SqList &s, int low, int high)...{
s.r[0] = s.r[low];
KeyType key = s.r[low].key;
while(low < high)...{
while(low < high && s.r[high].key >= key)...{
high--;
}
s.r[low] = s.r[high];
while(low < high && s.r[low].key <= key)...{
low++;
}
s.r[high] = s.r[0];
}
return low;
}
void qSort(SqList &s, int low, int high)...{
if(low < high)...{
int pivot = partition(s, low, high);
qSort(s, low, pivot - 1);
qSort(s, pivot + 1, high);
}
}
void quickSort(SqList &s)...{
qSort(s, 1, s.length);
}
void main()...{
int w[] = ...{49, 38, 65, 97, 76, 13, 27, 49};
int n = 8;
SqList s;
init(s, w, n);
quickSort(s);
show(s);
}
程序执行结果:
13 27 38 49 49 65 76 97
Press any key to continue
算法的复杂度为:平均O(n * log(n)),最坏O(n * n)。
头文件:
#define MAXSIZE 20
#define EQ(a, b) ((a) == (b))
#define LT(a, b) ((a) < (b))
#define LQ(a, b) ((a) <= (b))
typedef int KeyType;
typedef int InfoType;
typedef struct ...{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct ...{
RedType r[MAXSIZE + 1];
int length;
}SqList;
源文件:
#include "sort.h"
#include "stdio.h"
void init(SqList &s, int w[], int n)...{
s.length = n;
for(int i = 1; i <= n; i++)...{
s.r[i].key = w[i - 1];
}
}
void show(SqList s)...{
for(int i = 1; i <= s.length; i++)...{
printf("%d ", s.r[i]);
}
printf(" ");
}
void bubbleSort(SqList &s)...{
int flag = 1;
for(int i = s.length; i >= 1 && flag; i--)...{
flag = 0;
for(int j = 1; j <= i; j++)...{
if(LT(s.r[j].key, s.r[j - 1].key))...{
RedType rc = s.r[j - 1];
s.r[j - 1] = s.r[j];
s.r[j] = rc;
flag = 1;
}
}
}
}
void main()...{
int w[] = ...{49, 38, 65, 97, 76, 13, 27, 49};
int n = 8;
SqList s;
init(s, w, n);
bubbleSort(s);
show(s);
}
程序执行结果:
13 27 38 49 49 65 76 97
Press any key to continue
程序的复杂度为:O(n * n)。
(2)快速排序
源文件修改:
//use r[low] as pivot to part from low to high.
int partition(SqList &s, int low, int high)...{
s.r[0] = s.r[low];
KeyType key = s.r[low].key;
while(low < high)...{
while(low < high && s.r[high].key >= key)...{
high--;
}
s.r[low] = s.r[high];
while(low < high && s.r[low].key <= key)...{
low++;
}
s.r[high] = s.r[0];
}
return low;
}
void qSort(SqList &s, int low, int high)...{
if(low < high)...{
int pivot = partition(s, low, high);
qSort(s, low, pivot - 1);
qSort(s, pivot + 1, high);
}
}
void quickSort(SqList &s)...{
qSort(s, 1, s.length);
}
void main()...{
int w[] = ...{49, 38, 65, 97, 76, 13, 27, 49};
int n = 8;
SqList s;
init(s, w, n);
quickSort(s);
show(s);
}
程序执行结果:
13 27 38 49 49 65 76 97
Press any key to continue
算法的复杂度为:平均O(n * log(n)),最坏O(n * n)。
相关文章推荐
- 数据结构例程——交换排序之冒泡排序
- 数据结构-排序: 交换排序(快速排序法)
- 数据结构之单向链表操作1-(插入,删除,交换,反转,排序等操作)
- 数据结构学习系类列十三-交换排序
- C++代码,数据结构-内部排序-交换排序-起泡排序
- 数据结构例程—— 交换排序之快速排序
- 数据结构复习-交换排序
- C++代码,数据结构-内部排序-交换排序-快速排序
- 数据结构-交换排序-冒泡排序
- 数据结构-排序: 交换排序(快速排序法)
- 算法与数据结构之交换(SWAP)排序
- 图解"数据结构--内部排序算法"----交换排序:冒泡排序、快速排序
- 【数据结构】:排序--交换排序
- 数据结构-排序算法之交换排序(冒泡排序快速排序)
- 数据结构-排序: 交换排序(冒泡排序法)
- 数据结构之交换排序之快速排序(参考整理严蔚敏数据结构)
- 【数据结构】对一个数组按给定的下标排序,仅使用两两交换的方式,要求不能对数组进行扩容尽可能使用额外少的空间
- 【数据结构】排序算法(二)之交换排序之快速排序(QuickSort)
- 数据结构几种交换排序
- 数据结构 排序1 交换排序