Java Algorithm Learning Summary

1.插入排序 - Simple Insertion Sort

基本思想:将记录插入到一个应经排好序的列表中,从而得到一个新的,个数增加1的有序列表。即先将列表的第一条记录看成一个有序列表,然后从第二个记录开始开始逐个插入。直到整个序列有序为止。

//Sort method one: insert sort
	public static void directInsertSort() {
		int a[] = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62,	99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51 };
		int temp = 0;
		for (int i = 1; i < a.length; i++) {
			int j = i - 1;
			temp = a[i];			
			for (; j >= 0 && temp < a[j]; j--) {
				a[j + 1] = a[j];
			}
		
			a[j + 1 ] = temp;
		}
		for (int i = 0; i < a.length; i++)
			System.out.print(a[i]+ ",");
	}

2.希尔排序 - Shell's Sort

基本思想: 先将整个待排序的序列分成若干子序列进行直接插入排序,待整个序列基本有序时,再对所有记录进行依次直接插入排序。

操作方法:

  1. 选择一个增量序列t1,t2,…,tk,其中ti>tj,tk=1;
  2. 按增量序列个数k,对序列进行k 趟排序;
  3. 每趟排序,根据对应的增量ti,将待排序列分割成若干长度为m 的子序列,分别对各子表进行直接插入排序。仅增量因子为1 时,整个序列作为一个表来处理,表长度即为整个序列的长度。
public static void shellSort() {
		//int a[] = { 1, 54, 6, 3, 78, 34, 12, 45, 56, 100 };
		int a[] = {57,68,59,52,72,28,96,33,24,19};
		double length = a.length;
		int temp = 0;

		while (true) {
			length = Math.ceil(length / 2);
			int d = (int) length;
			for (int i = 0; i < a.length; i++)
				System.out.print("********" + a[i]+",");

			for (int x = 0; x < d; x++) {
				System.out.println("x="+ x);
				for (int i = x + d; i < a.length; i += d) {
					System.out.println("i="+ i);
					int j = i - d;
					temp = a[i];
					System.out.println("j="+ j);
					System.out.println("temp="+ temp);
					System.out.println("a[j]="+ a[j]);
					for (; j >= 0 && temp < a[j]; j -= d) {
						
						a[j + d] = a[j];
						System.out.println("j="+ j);
						System.out.println("temp="+ temp);
					}
					a[j + d] = temp;
				}
				
			}
			for (int i = 0; i < a.length; i++)
				System.out.print("********" + a[i]+",");
			System.out.println("****************d="+ d);
			if (d == 1)
				break;
		}

		for (int i = 0; i < a.length; i++)
			System.out.println(a[i]);
	}
	
	public static void shellSort2(){
		int[] a = { 49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 1 };
		int d = (int)Math.ceil(a.length / 2);
		
		while (true) {
			for (int i = 0; i < d; i++) {
				for (int j = i; j + d < a.length; j += d) {
					int temp;
					if (a[j] > a[j + d]) {
						temp = a[j];
						a[j] = a[j + d];
						a[j + d] = temp;
					}
				}
			}

			if (d == 1) {
				break;
			}
			d--;
		}
		
		System.out.println("New sort: ");
		for (int i = 0; i < a.length; i++) {
			System.out.print(a[i] + " ");
		}
	}


3. 简单选择排序 - Simple Selection Sort

基本思想: 在要排序的一组书中,选出最小(最大)的数与第一个位置的数交换; 然后在剩下的数当中找出最小(最大)的数与第二个位置的数交换; 以此类推,直到第n-1个数与第n个数比较为止。

操作方法:

package order;

public class SelectSort {
	
	public static void main(String[] args) {
		selectSort();
	}
	
	//Simple Selection sort
	public static void selectSort() {
		int a[] = {57,68,59,1,72,28,96,33,24,19};
		
		for (int i = 0; i < a.length - 1; i++) {
			int minValueIndex = selectMinKey(a, i+1, a.length);
			int temp = 0;
			if (a[i] > a[minValueIndex]) {
				temp = a[minValueIndex];
				a[minValueIndex] = a[i];
				a[i] = temp;				
			}					
		}
		
		for (int j=0; j < a.length; j++)
			System.out.print(a[j]+ ",");
	}
	
	public static int selectMinKey(int[] list, int start, int end) {
		int minValue = list[start];
		int minValueIndex = start;
		for (int i = start + 1; i < end; i++)
		{
			if (list [i] < minValue) {
				minValue = list[i];
				minValueIndex = i;
			}
		}
/*		System.out.println("The min value: " + minValue);
		System.out.println("The min index: " + minValueIndex);*/
		return minValueIndex;
	}
}


5. 冒泡排序 - Buddle Sort 

基本思想:对一组要排序的序列数,进行自上而下或自下而上的比较,将大的数往下沉,小的数往上冒。

//Sort method 1: bubble sort
	public static void bubbleSort(int[] a) {
		int temp = 0;
		for (int i = 0; i < a.length - 1; i++) {
			for (int j = 0; j < a.length - 1 - i; j++) {
				if (a[j] < a[j + 1]) {
					temp = a[j];
					a[j] = a[j + 1];
					a[j + 1] = temp;
				}
			}
		}
		for (int i = 0; i < a.length; i++)
			System.out.print(a[i] + ",");
	} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值