七大排序算法收藏版

来源:互联网 发布:数据挖掘导论 mobi 编辑:程序博客网 时间:2024/06/04 19:38

1.冒泡
void BubbleSort(int a[],int len){
int i,j;
bool change = true;
for(i=len-1;i>=1&&change;i--){
change = false;
for(j=0;j<i;j++){
if(a[j]>a[j+1]){
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
change = true;
}
}
}

}


2.插入排序


void InsertSort(int a[],int len){
for(int i=1; i < len; i++){
if(a[i] < a[i-1]){
int temp = a[i];
for(int j=i-1;j>=0&&a[j]>temp;j--){
a[j+1] = a[j];
}
a[j+1] = temp;
}
}
}


3. 堆排序


/* 堆排序: 
 * |len| data序列中最后一个元素的索引号, 也是参加排序的元素个数, 
 * 注意不是data序列的元素个数 
 * 1.  
 *  data序列中第1个元素(data[0])不参加排序 
 * 2.  
 *  总共执行|len - 1|趟排序,每一趟排序确定未排序序列中的最大值的位置,并执行一次adjust函数 
 * 3. 
 *  无论是最坏情况还是平均情况,堆积排序的时间复杂度都是O(n*log2n) 
 * 4. 
 *  堆积排序的空间复杂度是O(1), 适合于len较大的序列排序 
 * 5. 
 *  堆积排序不适合在链表上实现 
 * 6. 
 *  堆积排序是不稳定排序,例如(5, 4, 3, 2, 2, 2, 1) 
 */  
void adjust(int a[],int root,int len){
a[0] = a[root];
int i = root*2;
while(i <= len){
if(i<len && a[i]<a[i+1])
i++;
if(a[i] <= a[0])
break;
a[i/2] = a[i];
i*=2;
}
a[i/2] = a[0];
}


void HeapSort(int a[],int len){
if(a==NULL || len < 1)
return;
int i;
for(i=len/2;i>0;i--)
adjust(a,i,len);
for(i = len; i >1; --i){
a[0] = a[1];
a[1] = a[i];
a[i] = a[0];

adjust(a,1,i-1);
}
}


4. 归并排序


void mergeArray(int a[],int first,int mid,int last,int temp[]){
int i=first,j=mid+1;
int m=mid,n=last;
int k=0;


while(i<=m && j<=n){
if(a[i] < a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while(i<=m)
temp[k++] = a[i++];
while(j<=n)
temp[k++] = a[j++];
for(i=0;i<k;i++)
a[first+i] = temp[i];
}


void mergesort(int a[],int first,int last,int temp[]){
if(first < last){
int mid = (first+last)>>1;
mergesort(a,first,mid,temp);
mergesort(a,mid+1,last,temp);
mergeArray(a,first,mid,last,temp);
}
}




bool MergeSort(int a[],int len){
if(a==NULL||len<=0){
cout<<"invalid parameter!" <<endl;
return false;
}
int *pTemp = new int[len];
if(pTemp==NULL)
return false;
mergesort(a,0,len-1,pTemp);
delete[] pTemp;
return true;
}


5. 快速排序

#include <ctime>
#include <cstdlib>
#include <iostream>


using namespace std;


int randomInRange(int start,int end){
srand(time(NULL));
return start+rand()%(end-start+1);
}
/*inline void swap(int *elem1,int *elem2){
int temp = *elem1;
*elem1 = *elem2;
*elem2 = temp;
}
*/
inline void swap(int &a, int &b){
int temp =a;
a = b;
b = temp;
}
int Partition(int a[],int len, int start, int end){
if(a==NULL||len < 1 || start<0|| end >= len){
cout << "invalid parameters." <<endl;
exit(1);
}


int index = randomInRange(start,end);
swap(a[end],a[index]);
int small = start -1;


for(index = start; index < end; ++index){
if(a[index] < a[end]){
++small;
if(small != index){
swap(a[small],a[index]);
}
}
}
swap(a[++small],a[end]);


return small;
}


void QuickSort(int a[],int len, int start, int end){
if(start >= end)
return;


int index = Partition(a,len,start,end);
if(index > start)
QuickSort(a,len,start,index-1);
if(index < end)
QuickSort(a,len,index+1,end);
}


6. 选择排序

void SelectSort(int a[],int len){
int i,j,min,temp;
for(i=0;i<len;i++){
min = i;
for(j=i+1;j<len;j++){
if(a[j] < a[min]){
min = j;
}
}
if(min != i){
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
}


7.希尔排序

#include <iostream>
#include "stdlib.h"
using namespace std;


void ShellSort(int a[],int len){
int i,j,gap;
for(gap=len/2;gap>0;gap>>=1){
for(i = gap;i < len; i++){
for(j=i-gap;j>=0&&a[j]>a[j+gap];j-=gap){
int temp = a[j];
a[j] = a[j+gap];
a[j+gap] = temp;
}
}
}
}

原创粉丝点击