数据结构-排序笔记(一)

 以下如果有错误,希望能够指出,只是学习笔记,下次看都能改良不,再深入学习,(*^__^*) 嘻嘻……

package test2;

/**
 * 冒泡排序
 * 稳定算法
 * 平均时间复杂度:O(n*n)
 * 空间复杂度:O(1)
 * @author 卢林
 *
 */
public class BubbleSort {
	
	private static int NUMBER=10000;
	//从小到大排序
	public static void bubbleSort(int[] a) {
		int i, j, flag = 1; //flag标识变量,0:没有发生交换 , 1:发生了交换 
		int temp;
		int n = a.length;
		//i 1~7	外层: 1+7-1=7(次)
		for(i=1; i<n && flag==1; i++) {//控制循环次数,如果i=0,这样会使数组越界(发生在最后一次交换的a[j+1]上)
			flag = 0;
			System.out.println("flag_outer: " + flag);
			//j 0~n-i i++ i>>7	内层:7+6+5+4+3+2+1=[(1+7)*7]/2=18(次) 依次将最大的选到最后面
			for(j=0; j<n-i; j++) {//控制数组的索引index
				if(a[j] > a[j+1]) {//每一次比较都是使用较大的那一个值与后面的每一个依次比较,如果没有这一个比较,只是将数据倒序输出
					flag = 1; //表示满足交换的条件
					temp = a[j];
					a[j] = a[j+1];
					a[j+1] = temp;
				}	
				System.out.println("flag_inner: " + flag);
			}
		}
	}
	
	public static void main(String[] args) {
		int[] test = new int[10000];
		System.out.println(NUMBER);
		
		for (int i = 0; i < test.length; i++) {
			test[i] = NUMBER;
			NUMBER--;
		}
		
		//int[] test = {34,12,543,63,62,72,6,78};
		//System.out.println(test.length);
		
		long before = System.currentTimeMillis();
		BubbleSort.bubbleSort(test);
		long last = System.currentTimeMillis() - before;
		
		for (int i = 0; i < test.length; i++) {
			System.out.print(test[i] + "  ");
			//调整测试数据排序后的格式
			if(test[i] % 10 == 0) {
				System.out.println();
			}
		}
		System.out.println("冒泡排序时间: " + last + " ms");
	}
}


 

package test2;

/**
 * 直接插入排序
 * 不稳定算法
 * 平均时间复杂度:最差O(n*n)
 * 空间复杂度: 一个临时变量temp,O(1)
 * @author 卢林
 *
 */
public class InsertSort {
	//初始化数组时候使用的最大值
	private static int NUMBER = 10000;
	
	public static void insertSort(int[] a) {
		int i, j, temp;
		int n = a.length;
		
		for(i=0; i<n-1; i++) {
			temp = a[i+1];
			j=i;
			//如果后面那个比前面那个小就调换顺序
			while(j>-1 && temp<=a[j]) {
				a[j+1]=a[j];//大的换到后面
				j--;//实现从后向前,依次的比较和交换
			}
			a[j+1]=temp;//小的换到后面
		}
	}
	
	public static void main(String[] args) {
		int[] test = new int[10000];
		int n = test.length;
		System.out.println(NUMBER);
		
		for (int i = 0; i < test.length; i++) {
			test[i] = NUMBER;
			NUMBER--;
		}
		//计算算法所花费的时间ms
		long before = System.currentTimeMillis();
		InsertSort.insertSort(test);
		long last = System.currentTimeMillis()-before;
		
		for (int i = 0; i < n; i++) {
			System.out.print(test[i] + "  ");
			//调整测试数据排序后的格式
			if(test[i] % 10 == 0) {
				System.out.println();
			}
		}
		System.out.println();
		
		System.out.println("直接插入排序时间: " + last + " ms");
	}
}


 

package test2;

/**
 * 快速排序
 * 不稳定算法
 * 平均时间复杂度:O(n*lbn)
 * 空间复杂度:O(lbn)
 * @author 卢林
 */
public class QuickSort {
	
	private static int NUMBER = 10000;
	
	public static void quickSort(int[] a, int low, int high) {
		int i, j;
		int temp;
		
		//这样做好于直接使用low和high的值
		i = low; //将低位的值存到i中,方便递归中保留值
		j = high; //同理将高位的值保留下来
		temp = a[low];//将第一位数作为标准数,保留在temp中,同理方便操作,则左端a[low]即可用于存放右端比较时比标准元素小的数组元素
		
		while(i<j) {
			while(i<j && temp<=a[j]) { //(低位的数)小于/等于(高位的数)
				j--; //高位的index--(感觉有点像数据库游标的感觉。。。不知道正确不)
			}
			if(i<j) {//如果满足条件就交换相应位置的值,所以会导致不稳定
				a[i] = a[j];
				i++;
			}
			
			while(i<j && a[i]<temp) { //[低位的数(变化中)]小于之前(低位的数),在这里可以移动到temp左边第一个大于它的数,然后交换
				i++; //同上面j--,但是是低位index++
			}
			if(i<j) {//具体实现交换
				a[j] = a[i];
				j--;
			}
		}
		a[i] = temp; //交换完成后,就将之前a[low]的值放到a[i]的位置,这样就分出了左端和右端的子数组了		
		if(low<i) { //如果i发生了变化
			quickSort(a, low, i-1); //主要是i-1,这里指的是在之前的a[low]交换到a[i]的值的前一个位置的值进行递归排序
		}
		if(i<high) { //如果左边排好啦,那么就开始排右边
			quickSort(a, j+1, high);
		}
	}
	
	/**
	 * 测试方法
	 * @param args
	 */
	public static void main(String[] args) {
		int[] test = new int[10000];
		System.out.println(NUMBER);
		
		for (int i = 0; i < test.length; i++) {
			test[i] = NUMBER;
			NUMBER--;
		}
		
		long before = System.currentTimeMillis();
		//QuickSort.quickSort();
		long last = System.currentTimeMillis() - before;
		
		for (int i = 0; i < test.length; i++) {
			System.out.print(test[i] + "  ");
			//调整测试数据排序后的格式
			if(test[i] % 10 == 0) {
				System.out.println();
			}
		}
		System.out.println("快速排序时间: " + last + " ms");
	}
}


 

package test2;

/**
 * 不稳定直接选择排序
 * 不稳定算法
 * 平均时间复杂度:O(n*n)
 * 空间复杂度:O(1)
 * @author 卢林
 *
 */
public class SelectSort {
	
	private static int NUMBER = 10000;

	public static void selectSort(int[] a) {
		int i, j, small; //数组中,最小数的索引index
		int temp;
		int n = a.length;
		
		for(i=0; i<n-1; i++) {
			small = i; //排除掉之前已经排好序的部分,从未排到序的第一个元素开始比对
			for(j=i+1; j<n; j++) {
				//将最小的数的下标,依次从后面(已经去除掉排好序的部分)比较,遇到符合条件的即交换位置
				if(a[j] < a[small]) {
					small = j;
				}
			}
			//如果符合条件了,就交换
			if(small != i) {
				temp = a[i];
				a[i] = a[small];
				a[small] = temp;
			}
		}
	}
	
	public static void main(String[] args) {
		int[] test = new int[10000];
		System.out.println(NUMBER);
		
		for (int i = 0; i < test.length; i++) {
			test[i] = NUMBER;
			NUMBER--;
		}
		
		long before = System.currentTimeMillis();
		SelectSort.selectSort(test);
		long last = System.currentTimeMillis() - before;
		
		for (int i = 0; i < test.length; i++) {
			System.out.print(test[i] + "  ");
			//调整测试数据排序后的格式
			if(test[i] % 10 == 0) {
				System.out.println();
			}
		}
		System.out.println("不稳定直接选择排序时间: " + last + " ms");
	}
}


 

package test2;

/**
 * 稳定直接选择排序
 * 稳定的算法
 * 平均时间复杂度:O(n*n)
 * 空间复杂度:temp, O(1)
 * @author 卢林
 *
 */
public class SteadySelectSort {
	
	private static int NUMBER = 10000;

	public static void steadySelectSort(int[] a) {
		int i, j, small; //数组中,最小数的索引index
		int temp;
		int n = a.length;
		for(i=0; i<n-1; i++) {
			small = i; //排除掉之前已经排好序的部分,从未排到序的第一个元素开始比对
			for(j=i+1; j<n; j++) {
				//将最小的数的下标,依次从后面(已经去除掉排好序的部分)比较,遇到符合条件的即交换位置
				if(a[j] < a[small]) {
					small = j;
				}
			}
			//如果符合条件了,就交换
			if(small != i) {
				temp = a[small];//先将最近的那个数存储在temp临时变量里面
				for(j=small; j>i; j--) {
					a[j] = a[j-1];
				}
				a[i] = temp;
			}
		}
	}
	
	public static void main(String[] args) {
		int[] test = new int[10000];
		System.out.println(NUMBER);
		
		for (int i = 0; i < test.length; i++) {
			test[i] = NUMBER;
			NUMBER--;
		}
		
		long before = System.currentTimeMillis();
		SteadySelectSort.steadySelectSort(test);
		long last = System.currentTimeMillis() - before;
		
		for (int i = 0; i < test.length; i++) {
			System.out.print(test[i] + "  ");
			//调整测试数据排序后的格式
			if(test[i] % 10 == 0) {
				System.out.println();
			}
		}
		System.out.println("稳定直接选择排序时间: " + last + " ms");
	}
}


 

package test2;

/**
 * 希尔排序
 * 不稳定算法
 * 平均时间复杂度:O(n*(lbn)^2)
 * 空间复杂度: 一个临时变量temp, O(1)
 * @author 卢林
 *
 */
public class ShellSort {
	//初始化数组时候使用的最大值
	private static int NUMBER = 10000;
	
	/**
	 * 
	 * @param a 要排序的数组对象
	 * @param d 存放每组的元素的个数的数组
	 * @param numOfD 要分的组数是多少个
	 */
	public static void shellSort(int[] a, int[] d, int numOfD) {
		int i, j, k, m, span; //m可以看做是d[]的长度,span为每组的个数值
		int temp; //用来存放临时变量
		int n = a.length; 
		
		for(m=0; m<numOfD; m++) {
			//取得当前组的元素的个数
			span = d[m];
			for(k=0; k<span; k++) {
				for(i=k; i<n-span; i=i+span) {
					temp = a[i+span];
					j=i;
					//和插入排序思想一样,但是细节不同
					while(j>-1 && temp<=a[j]) {
						a[j+span] = a[j];//j+1
						j=j-span;//j--
					}
					a[j+span] = temp;
				}
			}
		}
	}
	
	public static void main(String[] args) {
		int[] test = new int[10000];
		System.out.println(NUMBER);
		
		for (int i = 0; i < test.length; i++) {
			test[i] = NUMBER;
			NUMBER--;
		}
		//分组的方式
		int[] span = {10, 5, 1};
		//计算算法所花费的时间ms
		long before = System.currentTimeMillis();
		ShellSort.shellSort(test, span, 3);
		long last = System.currentTimeMillis() - before;
		
		for (int i = 0; i < test.length; i++) {
			System.out.print(test[i] + "  ");
			//调整测试数据排序后的格式
			if(test[i] % 10 == 0) {
				System.out.println();
			}
		}
		System.out.println("希尔排序时间: " + last + " ms");
	}
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值