冒泡排序:
import java.util.Arrays;
public class Bubbling {
/**
* @param args
* 冒泡排序:
*
* 假设有 N 个数据需要排序,则从第 0 个数开始,依次比较第 0 和第 1 个数据,
*如果第 0 个大于第 1 个则两者交换,否则什么动作都不做,继续比较第 1 个第 2
*个…,这样依次类推,直至所有数据都“冒泡”到数据顶上。
* 冒泡排序的效率O(N*N),比较 N*N/2,交换 N*N/4;
*
*/
public int[] result(int[] array){ //int型可以换成是Object,但数组元素必须实现compareto()方法。
if(array.length==0){
return array;
}else{
int length,temp;
length = array.length;
for(int i=1;i<length;i++){
for(int a=0;a<length-i;a++){
if(array[a]>array[a+1]){
temp =array[a];
array[a]=array[a+1];
array[a+1]=temp;
}
}
}
}
return array;
}
public static void main(String[] args) {
int[] a =new int[5];
a[0]=1;
a[1]=5;
a[2]=3;
a[3]=0;
a[4]=6;
Bubbling bubbling = new Bubbling();
a=bubbling.result(a);
System.out.println(Arrays.toString(a));
}
快速排序:
public class QuickSort
{
/**
* @them 快速排序
* 对冒泡排序的改进:首先选一个轴值,将待排序的数据分为两个独立的两部分,左侧小于轴值,右侧大于轴值
* 然后分别对这两部分重复上述部分,直到整个序列有序。
*/
public static void main(String[] args)
{
quicksort qs = new quicksort();
int data[] = {44,22,2,32,54,22,88,77,99,11};
qs.data = data;
qs.sort(0, qs.data.length-1);
qs.display();
}
}
class quicksort
{
public int data[];
private int partition(int sortArray[],int low,int hight)//划分
{
int key = sortArray[low];
while(low<hight)
{
while(low<hight && sortArray[hight]>=key)
hight--;
sortArray[low] = sortArray[hight];
while(low<hight && sortArray[low]<=key)
low++;
sortArray[hight] = sortArray[low];
}
sortArray[low] = key;
return low;
}
public void sort(int low,int hight)
{
if(low<hight)
{
int result = partition(data,low,hight);//把整体分成独立的两部分,返回轴值位置
sort(low,result-1);//左侧重复
sort(result+1,hight);//右侧重复
}
}
public void display()
{
for(int i=0;i<data.length;i++)
{
System.out.print(data[i]);
System.out.print(" ");
}
}
}
插入排序:
import java.util.Arrays;
public class InsertionSort {
/**
* @param args
* @author mayi
*
* 插入排序 是在部分数据有序的情况下,使用 OUT 标记第一个无序的数据,将
其提取保存到一个中间变量 temp 中去,使用 IN 标记空位置,依次比较 temp 中
的值与 IN‐1 的值,如果 IN‐值大于 temp 的值,则后移,直到遇到第一个比temp
小的值,在其下一个位置插入;
插入排序的效率:O (N*N), 比较 N*N/4,复制 N*N/4;插入排序在随机数的
情况下,比冒泡快一倍,比选择稍快;在基本有序的数组中,插入排序几乎只需
要 O (N);在逆序情况下,并不比冒泡快;
*
*
*/
public int[] getResult(int[] objects){
int temp,in,out;
for(out=1;out<objects.length;out++){
temp =objects[out];
in =out;
while(in>0&&objects[in-1]>temp){
objects[in]=objects[in-1];
--in;
}
objects[in]=temp;
}
return objects;
}
public static void main(String[] args) {
int[] a={1,3,2,4,0};
InsertionSort insertionSort =new InsertionSort();
a=insertionSort.getResult(a);
System.out.println(Arrays.toString(a));
}
}
希尔排序:
import java.util.Arrays;
public class Shell_Sort {
/*
* @them 希尔排序
* 是对插入排序的改进,先将整个分成若干个,再在若干个里分别进行插入排序,然后再整体进行一次插入排序。
* 时间复杂度:O(NlogN)~O(N*N)
*/
public int[] shellSort(int[] theArray)
{
int inner=0,outer=0;
int nElems=theArray.length;
long temp=0;
int h=1; //find initial value of h
while(h<=nElems/3) //1,4,13,40,121,...
h=h*3+1;
while(h>0)
{
//当间隔h>1时,进行小部分插入排序;当间隔h=1时,进行整体插入排序
for(outer=h;outer<nElems;outer++)
{
temp=theArray[outer];
inner=outer;
while(inner>h-1&&theArray[inner-h]>=temp)
{
theArray[inner]=theArray[inner-h];
inner-=h;
}
theArray[inner]=(int) temp;
}
h=(h-1)/3; // 间隔从大减小,一直减小到1,此时因为间隔为1,就相当于是做整体的插入排序
}
return theArray;
}
public static void main(String[] args) {
int[] a={1,4,2,7,3,12,44,21,55,32,11};
Shell_Sort g=new Shell_Sort();
a=g.shellSort(a);
System.out.println(Arrays.toString(a));
}
}
选择排序:
import java.util.Arrays;
public class SelectSort {
/**
* @param args
* @author mayi
* @them 选择排序
* 假设有 N 条数据,则暂且标记第 0 个数据为 MIN(最小),使用 OUT 标记最左
边未排序的数据,然后使用 IN 标记第 1 个数据,依次与 MIN 进行比较,如果比
MIN 小,则将该数据标记为 MIN,当第一轮比较完后,最终的 MIN 与 OUT 标记
数据交换,依次类推;
选择排序的效率:O(N*N),比较 N*N/2,交换<N ; 选择排序与冒泡排序比
较,比较次数没有明显改变,但交换次数明显减少了很多;
*/
public int[] getResult(int[] objects){
int in,out,min,temp;
//如果数组为null,直接返回
if(objects==null||objects.length==0){
return objects;
}
for(out=0;out<objects.length-1;out++){
min =out;
//寻找最小值
for(in=out+1;in<objects.length;in++){
if(objects[min]>objects[in]){
min = in;
}
}
//与out交换位置
temp =objects[out];
objects[out]=objects[min];
objects[min]=temp;
}
return objects;
}
public static void main(String[] args) {
SelectSort ss = new SelectSort();
int[] a ={2,5,4,1};
a=ss.getResult(a);
System.out.println(Arrays.toString(a));
}
}