前导:排序算法是计算机编程中常用的算法之一,它在许多的地方都有它的用武之地。我们本篇博客将会着重来讲解一些算法的特性。排序算法是分为比较排序算法和非比较排序算法。但是我们这里只给出比较排序算法,在以后的排序算法中我们会给出非排序算法的内容。
目录
- 冒泡排序
- 选择排序
- 插入排序
- 归并排序
- 堆排序
- 快速排序
对于这些排序算法的性能我们给出一个表格。
1:冒泡排序
冒泡排序是一种比较简单的排序算法,它主要是通过重复的遍历需要排序的元素,比较两个相邻的元素,如果顺序是错误的,那么就把位置调换一下。这样一直工作,一直到没有元素需要排序。(注明:这里我们用java语言描述)
package test2;
public class f {
public static void main(String arguments[]) {
/*
* we will use some data to test the bubble sort
*/
int[] test={1,5,4,3,8,10,-4,0,8,4,64,5565,4};
int n=test.length;
bubble(test,n);
System.out.println("The result of sort is :");
for(int x:test){
System.out.print(x+" ");
}
}
public static void bubble(int test[],int n){
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++){
if(test[j]>test[j+1]){
Swap(test,j,j+1);
}
}
}
}
public static void Swap(int test[],int i,int j){
int temp=test[i];
test[i]=test[j];
test[j]=temp;
}
}
结果如下:
The result of sort is :
-4 0 1 3 4 4 4 5 8 8 10 64 5565
2:选择排序:
选择排序是一种简单的排序算法,他的工作原理也是十分的简单,就是在初始的序列中找到最大的元素,放在排好序的末尾,然后依次往复;
package test2;
public class f {
public static void main(String arguments[]) {
/*
* we will use some data to test the bubble sort
*/
int[] test={1,5,4,3,8,10,-4,0,8,4,64,5565,4};
int n=test.length;
select(test,n);
System.out.println("The result of sort is :");
for(int x:test){
System.out.print(x+" ");
}
}
public static void select(int test[],int n){
for(int i=0;i<n-1;i++){
int min=i;
for(int j=i;j<n;j++){
if(test[j]<test[min]){
min=j;
}
}if(min!=i){
Swap(test,min,i);
}
}
}
public static void Swap(int test[],int i,int j){
int temp=test[i];
test[i]=test[j];
test[j]=temp;
}
}
结果如图:
The result of sort is :
-4 0 1 3 4 4 4 5 8 8 10 64 5565
3:归并排序:归并排序相对于其他的排序算法来说拥有比较好的算法性能,他的时间复杂度是nlogn。这个算法采取了分治的策略进行排序。这个是算法领域极其重要的思想。下面我们给出代码和测试用例。
package test2;
public class f {
public static void main(String arguments[]) {
/*
* we will use some data to test the bubble sort
*/
int[] test={1,5,4,3,8,10,-4,0,8,4,64,5565,4};
int n=test.length;
sortCore(test);
System.out.println("The result of sort is :");
for(int x:test){
System.out.print(x+" ");
}
}
public static void merge(int test[],int low ,int mid, int high){
if(low>=high){
return ;
}
int[] temp=new int[high-low+1];
int index1=low;
int index2=mid+1;
int i=0;
while(index1<=mid&&index2<=high){
if(test[index1]<=test[index2]){
temp[i]=test[index1];
index1++;
i++;
}else{
temp[i]=test[index2];
index2++;
i++;
}
}
while(index2<=high){
temp[i]=test[index2];
index2++;
i++;
}
while(index1<=mid){
temp[i]=test[index1];
index1++;
i++;
}
int j=0;
for(int x:temp){
test[low+j]=x;
j++;
}
}
public static void sortCore(int test[]){
int length=test.length;
int groupSize=1;
while(groupSize<length){
for(int i=0;i<length;i+=(groupSize*2)){
int low=i;
int high=Math.min(i+groupSize*2-1, length-1);
int mid=low+groupSize-1;
merge(test,low,mid>=high?(low+high)/2:mid,high);
}
groupSize*=2;
}
if(groupSize/2<length){
int low=0;
int high=length-1;
merge(test,low,groupSize/2-1,high);
}
}
}
The result of sort is :
-4 0 1 3 4 4 4 5 8 8 10 64 5565
4:快速排序:刚才我们实现了归并排序之后,我们在惊叹于他的时间复杂度降低之外,同时我们也注意 到一个问题,那就是归并排序需要额外的空间作为支撑来实现排序那么对于大量数据的排序这样就显得不那么明智了。下面我们将介绍快速排序,他相对于一般的排序算法会有比较好的性能,同时,这个算法不需要额外的空间作为支撑。下面给出具体的代码:
package test2;
public class f {
static int[] test={1,5,4,3,8,10,-4,0,8,4,64,5565,4};
static int n=test.length;
public static void main(String arguments[]) {
/*
* we will use some data to test the bubble sort
*/
quicksort(0,n-1);
System.out.println("The result of sort is :");
for(int x:test){
System.out.print(x+" ");
}
}
public static void quicksort(int left,int right){
int i,j,temp;
int t;
if(left>right){
return ;
}
temp=test[left];
i=left;
j=right;
while(i!=j){
while(test[j]>=temp&&i<j){
j--;
}
while(test[i]<=temp&&i<j){
i++;
}
if(i<j){
t=test[i];
test[i]=test[j];
test[j]=temp;
}
}
test[left]=test[i];
test[i]=temp;
quicksort(left,i-1);
quicksort(i+1,right);
}
}
下面是测试结果:
The result of sort is :
-4 0 1 1 1 3 3 3 4 4 8 8 5565