常见排序算法-JAVA实现

博客介绍了几种常见排序算法及其时间复杂度,包括选择排序和冒泡排序,时间复杂度均为O(n^2);快速排序和归并排序,时间复杂度为O(n*log2n)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package org.nxt.algorithm.search;
/**
 * the bean of comparable
 * @author nanxiaotao
 *
 */
public class ComparableBean implements Comparable<ComparableBean> {

	private int value;
	

	public int getValue() {
		return value;
	}



	public void setValue(int value) {
		this.value = value;
	}



	@Override
	public int compareTo(ComparableBean o) {
		if(this.value<o.getValue()) {
			return -1;
		}else if(this.value==o.getValue()) {
			return 0;
		}else {
			return 1;
		}
	}

}

1、选择排序

      时间复杂度:O(n^2)

package org.nxt.algorithm.sort;

import org.nxt.algorithm.search.ComparableBean;

/**
 * selection sort
 * @author nanxiaotao
 *
 */
public class SelectionSort {
	
	/**
	 * sort
	 * @param datas datas datas aggregate of the data
	 */
	public static void sort(ComparableBean [] datas) {
		
		int min;//the index of the smallest number
		
		for(int i=0;i<datas.length;i++) {
			min=i;//start with the first element on the left
			//find the smallest number to the right of the set
			for(int j=i+1;j<datas.length;j++) {
				if(datas[j].compareTo(datas[min])==-1) {
					//there is number smaller then that
					//replace their index
					min=j;
				}
				
			}
			swap(datas,min,i);
		}
		
	}
	
	/**
	 * swap values by index
	 * @param datas
	 * @param min
	 * @param currentIndex
	 */
	private static void swap(ComparableBean [] datas,int min,int currentIndex) {
		ComparableBean temp=datas[min];
		datas[min]=datas[currentIndex];
		datas[currentIndex]=temp;
	}

}

2、冒泡排序

      时间复杂度:O(n^2)

package org.nxt.algorithm.sort;

import org.nxt.algorithm.search.ComparableBean;

/**
 * bubble sort
 * @author nanxiaotao
 *
 */
public class BubbleSort {
	
	/**
	 * sort
	 * @param datas datas datas aggregate of the data
	 */
	public static void sort(ComparableBean [] datas) {
		
		int needSortLength;//the number of sequences that need to be sorted
		int i;//the elements that need to be compared currently
		for(needSortLength=datas.length-1;needSortLength>=0;needSortLength--) {
			//find the largest number to the right of the set
			for(i=0;i<needSortLength;i++) {
				if(datas[i].compareTo(datas[i+1])==1) {
					//there is number smaller then that
					//replace their index
					swap(datas,i,i+1);
				}
				
			}
			
		}
		
	}
	
	/**
	 * swap values by index
	 * @param datas
	 * @param min
	 * @param currentIndex
	 */
	private static void swap(ComparableBean [] datas,int i1,int i2) {
		ComparableBean temp=datas[i2];
		datas[i2]=datas[i1];
		datas[i1]=temp;
	}

}

3、快速排序

      时间复杂度:O(n*log2n)

package org.nxt.algorithm.sort;

import org.nxt.algorithm.search.ComparableBean;

/**
 * quick sort
 * @author nanxiaotao
 *
 */
public class QuickSort {
	
	/**
	 * sort
	 * @param datas  datas aggregate of the data
	 * @param min 
	 * @param max
	 */
	public static void sort(ComparableBean [] datas,int min,int max) {
		
		int pivot;//the index of partition
		
		if(min<max) {
			pivot=partition(datas,min,max);
			sort(datas,min,pivot-1);
			sort(datas,pivot+1,max);
		}
		
	}
	
	/**
	 * partition
	 * @param datas datas aggregate of the data
	 * @param min
	 * @param max
	 * @return
	 */
	private static int partition(ComparableBean [] datas,int min,int max) {
		
		ComparableBean partitionValue=datas[min];
		
		int left=min;
		int right=max;
		
		while(left<right) {
			while(datas[left].compareTo(partitionValue)<=0) {
				left++;
			}
			while(datas[right].compareTo(partitionValue)==1) {
				right--;
			}
			if(left<right) {
				swap(datas,left,right);
			}
		}
		swap(datas,min,right);
		return right;
	}
	
	/**
	 * swap values by index
	 * @param datas
	 * @param min
	 * @param currentIndex
	 */
	private static void swap(ComparableBean [] datas,int i1,int i2) {
		ComparableBean temp=datas[i2];
		datas[i2]=datas[i1];
		datas[i1]=temp;
	}
	
	public static void main(String [] args) {
		ComparableBean c1=new ComparableBean();
		c1.setValue(5);
		ComparableBean c2=new ComparableBean();
		c2.setValue(2);
		ComparableBean c3=new ComparableBean();
		c3.setValue(3);
		ComparableBean c4=new ComparableBean();
		c4.setValue(1);
		ComparableBean c5=new ComparableBean();
		c5.setValue(7);
		ComparableBean [] datas= {c1,c2,c3,c4,c5};
		sort(datas,0,4);
		for(int i=0;i<datas.length;i++) {
			System.out.println(datas[i].getValue());
		}
	}

}

4、归并排序

      时间复杂度:O(n*log2n)

package org.nxt.algorithm.sort;

import org.nxt.algorithm.search.ComparableBean;

/**
 * merget sort
 * @author nanxiaotao
 *
 */
public class MergeSort {
	
	/**
	 * sort
	 * @param datas datas datas aggregate of the data
	 * @param max
	 * @param min
	 */
	public static void sort(ComparableBean [] datas,int min,int max) {
		if(min<max) {
			
			int mid=(min+max)/2;
			
			sort(datas,min,mid);
			
			sort(datas,mid+1,max);
			
			merge(datas,min,mid,max);
			
		}
		
		
	}
	
	
	private static void merge(ComparableBean [] datas,int first,int mid,int last) {
		
		ComparableBean [] temp=new ComparableBean[datas.length];
		
		int first1=first;
		int last1=mid;
		int first2=mid+1;
		int last2=last;
		
		int index=first1;
		
		while(first1<=last1&&first2<=last2) {
			if(datas[first1].compareTo(datas[first2])==-1) {
				temp[index]=datas[first1];
				first1++;
			}else {
				temp[index]=datas[first2];
				first2++;
			}
			index++;
		}
		
		while(first1<=last1) {
			temp[index]=datas[first1];
			first1++;
			index++;
		}
		
		while(first2<=last2) {
			temp[index]=datas[first2];
			first2++;
			index++;
		}
		
		for(index=first;index<=last;index++) {
			datas[index]=temp[index];
		}
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值