数据结构和算法——排序

1、选择排序

/**
 * 选择排序
 * 原理:
 * 1、取给定数组的第一个数为基数
 * 2、取出对一个数的值和值得下标作为参数
 * 比较:
 * 3、第一轮:将第二个以及后面的数逐个与取出的第一个数比价,一轮比较结束,取出最小的一个数
 * 4、第二轮:将第二个数逐个与后面的数比较,取出第二小的数,以此类推
 */
public class SelectSortTest {
	public static void main(String[] args) {
		System.out.println("数据结构和算法--选择排序");
		int[] array = {38,65,97,76,13,27,49,11};
		selectSort(array);
		for(int i = 0;i<array.length;i++) {
			System.out.print(array[i]+"\t");
		}
	}
	public static void selectSort(int[] arr) {
		int i,j;
		int flag = 0;
		int temp = 0;
		int len = arr.length;
		for(i=0;i<len;i++) {//外循环
			//记录第一个数为参数
			flag = i;
			temp = arr[i];
			for(j=i+1;j<len;j++) {//内循环
				/**如果在比较中前一个数大于后一个数,则交互下标
				 * 第一次比较出13小于38,所以此时的将13的值和它的下标作为参数继续与后面的比较,
				 * 检测到11小于13,再次交换下标和值,到此for(j=i+1;j<len;j++)内循环结束,跳出内循环
				 */
				if(temp>arr[j]) {
					flag=j;
					temp=arr[j];
				}
			}
			/**
			 * 跳出第一次内循环后,找出的值肯定是整个数字中最小的,然后将它和此时的外循环的参数下标和值交换
			 */
			if(flag!=i) {
				arr[flag]=arr[i];
				arr[i] = temp;
			}
		}
	}
}

2、插入排序

/**
 * 插入排序算法
 */
public class InsertSortTest {
	
	public static void main(String[] args) {
		int[] array = {38,65,97,76,13,27,49,11};
		System.out.println("数据结构与算法-->插入排序:");
		insertSort(array);
		for(int i=0;i<array.length;i++) {
			System.out.print(array[i]+"\t");
		}
	}
	/**
	 * 执行流程:
	 * 一开始自己设想第一个数为一个数列,然后每次那后面的一个扔进来,扔进来的数重新排列
	 * @param arr
	 */
	public static void insertSort(int[] arr) {
		int i,j;
		int len = arr.length;
		int temp = 0;
		//循环向自己设想的队列中扔进数据,将第i位的数值是扔进来的数据
		for(i=1;i<len;i++) {
			temp = arr[i];
			j = i;
			//如果仍进来的数比前面的一个数小
			if(arr[j-1]>temp) {
				//将扔进来的数与前面的数逐个比较排序
				while(j>=1 && arr[j-1]>temp) {
					arr[j] = arr[j-1];
					j--;
				}
			}
			//前面经过while循环到符合大小顺序的排序,该该数放进适合的位置
			//如:前面已经的排序是2,3,6,7当4放进来时,if和while的循环会将4比较到3,
			//发现不符合while循环条件,跳出if和while循环,将4放在了3的后面
			arr[j] = temp;
		}
	}
}

3、归并排序

public class MergeTest2 {
	public static void main(String[] args) {
		int[] array = {5,4,9,8,7,6,0,1,3,2};
		System.out.println(Arrays.toString(array));
		mergeSort(array,0,array.length-1);
		System.out.println(Arrays.toString(array));
	}
	public static void merge(int[] array,int low,int mid,int height) {
		//第一个归并的开始
		int s1 = low;
		//第二个归并的开始
		int s2 = mid+1;
		//创建一个临时数组temp
		int[] temp = new int[height-low+1];
		//临时数组temp的下标
		int i = 0;
		//当s1和s2都有数据(最少一个)时
		while(s1<=mid && s2<=height) {
			if(array[s1]<=array[s2]) {
				temp[i++] = array[s1++];
			}else {
				temp[i++] = array[s2++];
			}
		}
		//当s1还有数,s2没有数时
		while(s1<=mid) {
			temp[i++] = array[s1++];
		}
		//当s2还有数,s1没有数时
		while (s2 <= height) {
			temp[i++] = array[s2++];
		}
		//将临时数组拷贝到array数组中
		for(int j= 0;j<temp.length;j++) {
			array[j+low] = temp[j];
		}
	}
	
	public static void mergeSort(int[] array,int low,int height) {
		if(low>=height) {
			return;
		}
		int mid = (low+height)>>>1;
		mergeSort(array,low,mid);
		mergeSort(array,mid+1,height);
		//合拼
		merge(array,low,mid,height);
	}
}

4、冒泡排序

/**
 * 冒泡排序
 */
public class BubbleSortTest {
	public static void main(String[] args) {
		int[] array = {5,4,9,8,7,6,0,1,3,2};
		bubbleSort(array);
		System.out.println("冒泡排序:");
		for(int k=0;k<array.length;k++) {
			System.out.print(array[k]+"\t");
		}
	}
	public static void bubbleSort(int[] arr) {
		int i,j;
		int len = arr.length-1;
		int temp;
		//总共遍历数组i次
		for(i=0;i<len;i++) {
			//反向冒泡:从后面向前面比较,每次都将比较出的最小数放在第i个位置
			//j>i:经过一轮的比较后,整个数列的最小的数肯定放在了第i个位置,之后i++,
			//第0~i个数此时是有序排列的,不需要再进行比较
			for(j=len;j>i;--j) {
				//如果j个数值比j-1的数值小,交换
				if(arr[j]<arr[j-1]) {
					temp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = temp;
				}
			}
		}
	}
}

5、快速排序

public class QuickSortTest {
	public static void main(String[] args) {
		int[] array = {5, 4, 9, 8, 7, 6, 0, 1, 3, 2};
		quickSort(array,0,array.length-1);
		for(int i=0;i<array.length;i++) {
			System.out.print(array[i]+"  ");
		}
	}
	//参数:传递的数组,开始位置,末尾位置
	public static void quickSort(int[] arr,int left,int right) {
		//判断:如果左边检索的索引值比右边的索引要大,是不合法的,结束跳出该方法
		if(left>right) {
			return;
		}
		//定义一个变量作为保存基准数
		int base = arr[left];
		//定义的 i 指向做左边的数,后面 i 会向右移动
		int i = left;
		//定义的 j 指向做右边的数,后面 j 会向右移动
		int j = right;
		//当i 和 j不相遇时,在循环中进行检索
		while(i !=j) {
			//从右边向左边检索,当arr[j]的值大于基数时
			//继续向左检索
			while(arr[j]>=base && i<j) {
				j--;
			}
			//从左边向右边检索,当arr[i]的值大于基数时
			//继续向右检索
			while(arr[i]<=base && i< j) {
				i++;
			}
			//当 i 和 j 都停下是,进行交换
			int temp = arr[i];
			arr[i] = arr[j];
			arr[j] = temp;
		}
		/**
		 * while变量i 和 j 相等(相遇)时,
		 * 将基准数和变量i 和 j 相等(相遇)时的元素交换
		 */
		arr[left] = arr[i];
		arr[i] = base;
		/**
		 * 递归调用方法
		 */
		quickSort(arr,left,i-1);
		quickSort(arr,i+1,right);

	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值