简单排序算法

 

冒泡排序:

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));
	}

}


 

简单排序算法是指冒泡排序选择排序插入排序这三种基本的排序算法。这些算法都是通过比较和交换数组中的元素来实现排序的。以下是这三种算法简单介绍和Java代码实现: 1. 冒泡排序算法:重复地遍历要排序的数列,比较相邻的两个元素,如果顺序错误就交换它们的位置,直到没有需要交换的元素为止。时间复杂度为O(n^2)。 Java代码实现: ``` public static void bubbleSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } ``` 2. 选择排序算法:遍历数组,找到最小的元素,将其放到数组的起始位置,然后再从剩余的元素中找到最小的元素,放到已排序部分的末尾。时间复杂度为O(n^2)。 Java代码实现: ``` public static void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } ``` 3. 插入排序算法:将数组分成已排序和未排序两部分,遍历未排序部分的元素,将其插入到已排序部分的合适位置。时间复杂度为O(n^2)。 Java代码实现: ``` public static void insertionSort(int[] arr) { int n = arr.length; for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值