冒泡、快速、归并排序课程设计
2011-10-26 20:36
169 查看
一、冒泡排序
1 、冒泡排序算法思想:假设有n个序列,在第一趟排序时一次比较两个数(如按递增序列),每次可得到最大数然后依次比较n-1趟。2、冒泡排序算法分析:(第一趟排序)
初始值:12 23 9 2 59 655 215 33 72 30
一:12 23 9 2 59 655 215 33 72 30
二:12 9 23 2 59 655 215 33 72 30
三:12 9 2 23 59 655 215 33 72 30
四:12 9 2 23 59 655 215 33 72 30
五:12 9 2 23 59 655 215 33 72 30
六:12 9 2 23 59 215 655 33 72 30
七:12 9 2 23 59 215 33 655 72 30
八:12 9 2 23 59 215 33 72 655 30
九:12 9 2 23 59 215 33 72 30 655
3、冒泡排序算法:
typedef struct {
int key;
}element;
void swap(int *x,int *y)
{int temp=*x;
*x=*y;
*y=temp;
}
void bubble_sort(element a[ ],int n)
{
int i,j;
while(i>0)
for(j=0;j<n-1;j++)
if(a[j].key>a[j+1].key)
swap(a[j].key,a[j+1].key);
}
4.时间复杂度计算:O(n*n)
二、快速排序
1、快速排序算法思想:首先从待排序记录中选一个分割记录;接着调整所有待排序记录,将分割记录放置在一个位置,使分割记录左边所有所有的关键字不大于分割记录的关键字,使分割记录右边所有记录的关键字不小于分割记录的关键字;最后把分割记录左边的那些冀鲁豫分割记录右边的那些记录分别看作两个独立表,在对这两个表分别进行排序。2、快速排序算法分析(第一趟排序):
初始值:50 23 9 2 59 655 215 33 72 30
一:50 23 9 2 30 655 215 33 72
59
二:50 23 9 2 30
33 215 655 72 59
三:33 23 9 2 30 50 215 655 72 59
四:(33 23 9 2 30) 50( 215 655 72 59)
3、快速排序算法:
void quicksort(element a[ ],int left,int right)
{/*sort a[left:right] into nondecreasing order on the key failed filed;a[left].key is arbitrarily chosen as the pivot key; it is assumedthat a[left].key<=a[right+1].key */
int pivot ; //定义关键字
int i,j;
element temp; //同上
if(left<=right) {
i=left;
j=right;
pivot=a[left].key; //找到关键字,一般是左边第一个
do{/*search for keys from the left and right sublists,swapping out-of-order elements until the legt and legt and right boundaries cross or meet */
do i++;while(a[i].key<pivot); //it will not find untill it finds the key that < pivot
do j--;while(a[j].key>pivot); //it will not find untill it finds the key that >pivot
if(i<j) swap(a[i],a[j]);
}while(i<j)
swap(a[left],a[j]); //we can exchange the pivot with the middle key
quicksort(a,left,j-1);
quicksort(a,j+1,right)
}
4、时间复杂度:O(n*logn)
三、(非递归)归并排序
1、算法思想:归并排序一开始把长度为n的表看作n个子表,每个子表长度是1,已经有序。接下来开始归并,第一次把n个字表归并为n/2个字表,每个字表的长度为2(若n是奇数,则其中有一个长度为1的子表)。第二次把n/2个表归并为n/4个表。每次归并子表的个数减半,直到只有一个表示。2、算法分析:
初始值: 26 5
77 1 61
11 5
99 15 48
一:5 26 1 77
11 61 5 99 15 48
二:1 5 26 77
5 11 61 99 15 48
三:1 5 5 11 15 26 48 61 77 99
3、算法:
void merge(element initList [ ],element mergedList(),int i,int m,int n)
{/*the osrted lists initList [i:m] and initList[m+1:n] are merged to obtain the sorted list mergeList[i:n] */
int j,k,t;
j=m+1;
k=i;
while(i<=m && j<=n) {
if(initList[i].key<=initList[j].key)
mergedList[K++]=initList[i++]; //把关键字更小的放到目标位置
else
mergedList[k++]=initList[j++]; //归并另一个子序列
}
if(i>m) //把未计算完的添加到目标序列后面
for(t=j;t<=n;t++)
mergedList[t]=initList[t];
else
for(t=i;t<=m;t++)
mergedList[k+t-i]=initList[t];
}
void mergepass(element initList [ ],element mergeList [ ],int n,int s)
{/*perform one pass of the merge sort,merge addacent pairs of sorted segments from initList [ ] into megedList [ ].n is the number of elements in the list,s is the isze of each sorted segment */
int i,j;
for(i=1;i<=n-2*s +1;i+=2*s)
mege(initList ,mergedList,i,i+s-1,i+2*s-1);
if(i+s-1<n)
merge(initList,mergedList,i,i+s-1,n);
else
for(j=i;j<=n;j++)
mergedList[j] = initList[j];
}
4、时间复杂度:O(n*logn)
相关文章推荐
- 插入,归并,快速,冒泡,shell,选择排序 用c实现
- 排序算法大集合,插入、选择、冒泡、希尔、堆、归并、快速排序,总有一款适合你
- 数组排序(插入、选择、希尔、堆、归并、快速、冒泡)
- 六种排序方法的学习(直接插入、希尔、冒泡、快速、选择、归并)
- 可视化排序(插入/选择/冒泡/快速/归并/Shell)
- 数据结构各种排序法及核心思想(冒泡、鸡尾酒、选择、插入、二分法、希尔、堆、归并、快速)
- 链表之排序(插入、选择、归并、快速、冒泡)
- 插入 希尔 排序 堆排序 冒泡 快速 归并
- 从零开始_学_数据结构(六)——排序(冒泡、插入、希尔、简单选择、归并、快速)
- Python基础----排序(插入、冒泡、快速、归并)
- C语言排序(冒泡,选择,插入,归并,快速)
- java---插入排序,冒泡,归并,快速,希尔,堆排序
- 第四篇、C_快速、冒泡、选择、插入排序、二分查找排序、归并、堆排序
- 几种排序算法 冒泡、快速排序、归并、选择排序等
- Java算法之排序(快速,冒泡,归并,选择)
- C 各种排序(选择/冒泡/快速/插入/希尔/归并/堆)
- c++实现数据结构中的各种排序方法:直接插入、选择,归并、冒泡、快速、堆排序、shell排序
- 六种排序 冒泡 选择 插入 归并 快速 堆排序
- 冒泡,归并,快速排序
- 排序算法Java描述:选择、冒泡、插入、希尔、归并、快速及三向切分快速排序