冒泡排序
package com.huke.sort;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class BubbleSort {
public static void main( String[ ] args) {
// int arr[ ] = { 3, 9, -1, 10, 20} ;
//
// System.out.println( "排序前" ) ;
// System.out.println( Arrays.toString( arr)) ;
//为了容量理解,我们把冒泡排序的演变过程,给大家展示
//测试一下冒泡排序的速度O( n^2) , 给80000个数据,测试
//创建要给80000个的随机的数组
int[ ] arr = new int[ 80000] ;
for( int i = 0; i < 80000; i++) {
arr[ i] = ( int) ( Math.random( ) * 8000000) ; //生成一个[ 0, 8000000) 数
}
Date data1 = new Date( ) ;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) ;
String date1Str = simpleDateFormat.format( data1) ;
System.out.println( "排序前的时间是=" + date1Str) ;
//测试冒泡排序
bubbleSort( arr) ;
Date data2 = new Date( ) ;
String date2Str = simpleDateFormat.format( data2) ;
System.out.println( "排序后的时间是=" + date2Str) ;
//System.out.println( "排序后" ) ;
//System.out.println( Arrays.toString( arr)) ;
/*
// 第二趟排序,就是将第二大的数排在倒数第二位
for ( int j = 0; j < arr.length - 1 - 1 ; j++) {
// 如果前面的数比后面的数大,则交换
if ( arr[ j] > arr[ j + 1] ) {
temp = arr[ j] ;
arr[ j] = arr[ j + 1] ;
arr[ j + 1] = temp;
}
}
System.out.println( "第二趟排序后的数组" ) ;
System.out.println( Arrays.toString( arr)) ;
// 第三趟排序,就是将第三大的数排在倒数第三位
for ( int j = 0; j < arr.length - 1 - 2; j++) {
// 如果前面的数比后面的数大,则交换
if ( arr[ j] > arr[ j + 1] ) {
temp = arr[ j] ;
arr[ j] = arr[ j + 1] ;
arr[ j + 1] = temp;
}
}
System.out.println( "第三趟排序后的数组" ) ;
System.out.println( Arrays.toString( arr)) ;
// 第四趟排序,就是将第4大的数排在倒数第4位
for ( int j = 0; j < arr.length - 1 - 3; j++) {
// 如果前面的数比后面的数大,则交换
if ( arr[ j] > arr[ j + 1] ) {
temp = arr[ j] ;
arr[ j] = arr[ j + 1] ;
arr[ j + 1] = temp;
}
}
System.out.println( "第四趟排序后的数组" ) ;
System.out.println( Arrays.toString( arr)) ; */
}
// 将前面额冒泡排序算法,封装成一个方法
public static void bubbleSort( int[ ] arr) {
// 冒泡排序 的时间复杂度 O( n^2) , 自己写出
int temp = 0; // 临时变量
boolean flag = false ; // 标识变量,表示是否进行过交换
for ( int i = 0; i < arr.length - 1; i++) {
for ( int j = 0; j < arr.length - 1 - i; j++) {
// 如果前面的数比后面的数大,则交换
if ( arr[ j] > arr[ j + 1] ) {
flag = true ;
temp = arr[ j] ;
arr[ j] = arr[ j + 1] ;
arr[ j + 1] = temp;
}
}
//System.out.println( "第" + ( i + 1) + "趟排序后的数组" ) ;
//System.out.println( Arrays.toString( arr)) ;
if ( ! flag) { // 在一趟排序中,一次交换都没有发生过
break ;
} else {
flag = false ; // 重置flag! ! ! , 进行下次判断
}
}
}
}
插入如排序
package com.atguigu.sort;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class InsertSort {
public static void main( String[ ] args) {
//int[ ] arr = { 101, 34, 119, 1, -1, 89} ;
// 创建要给80000个的随机的数组
int[ ] arr = new int[ 80000] ;
for ( int i = 0; i < 80000; i++) {
arr[ i] = ( int) ( Math.random( ) * 8000000) ; // 生成一个[ 0, 8000000) 数
}
System.out.println( "插入排序前" ) ;
Date data1 = new Date( ) ;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) ;
String date1Str = simpleDateFormat.format( data1) ;
System.out.println( "排序前的时间是=" + date1Str) ;
insertSort( arr) ; //调用插入排序算法
Date data2 = new Date( ) ;
String date2Str = simpleDateFormat.format( data2) ;
System.out.println( "排序前的时间是=" + date2Str) ;
//System.out.println( Arrays.toString( arr)) ;
}
//插入排序
public static void insertSort( int[ ] arr) {
int insertVal = 0;
int insertIndex = 0;
//使用for循环来把代码简化
for( int i = 1; i < arr.length; i++) {
//定义待插入的数
insertVal = arr[ i] ;
insertIndex = i - 1; // 即arr[ 1] 的前面这个数的下标
// 给insertVal 找到插入的位置
// 说明
// 1. insertIndex >= 0 保证在给insertVal 找插入位置,不越界
// 2. insertVal < arr[ insertIndex] 待插入的数,还没有找到插入位置
// 3. 就需要将 arr[ insertIndex] 后移
while ( insertIndex >= 0 && insertVal < arr[ insertIndex] ) {
arr[ insertIndex + 1] = arr[ insertIndex] ; // arr[ insertIndex]
insertIndex--;
}
// 当退出while循环时,说明插入的位置找到, insertIndex + 1
// 举例:理解不了,我们一会 debug
//这里我们判断是否需要赋值
if( insertIndex + 1 != i) {
arr[ insertIndex + 1] = insertVal;
}
//System.out.println( "第" +i+"轮插入" ) ;
//System.out.println( Arrays.toString( arr)) ;
}
/*
//使用逐步推导的方式来讲解,便利理解
//第1轮 { 101, 34, 119, 1} ; = > { 34, 101, 119, 1}
//{ 101, 34, 119, 1} ; = > { 101,101,119,1}
//定义待插入的数
int insertVal = arr[ 1] ;
int insertIndex = 1 - 1; //即arr[ 1] 的前面这个数的下标
//给insertVal 找到插入的位置
//说明
//1. insertIndex >= 0 保证在给insertVal 找插入位置,不越界
//2. insertVal < arr[ insertIndex] 待插入的数,还没有找到插入位置
//3. 就需要将 arr[ insertIndex] 后移
while( insertIndex >= 0 && insertVal < arr[ insertIndex] ) {
arr[ insertIndex + 1] = arr[ insertIndex] ; // arr[ insertIndex]
insertIndex--;
}
//当退出while循环时,说明插入的位置找到, insertIndex + 1
//举例:理解不了,我们一会 debug
arr[ insertIndex + 1] = insertVal;
System.out.println( "第1轮插入" ) ;
System.out.println( Arrays.toString( arr)) ;
//第2轮
insertVal = arr[ 2] ;
insertIndex = 2 - 1;
while( insertIndex >= 0 && insertVal < arr[ insertIndex] ) {
arr[ insertIndex + 1] = arr[ insertIndex] ; // arr[ insertIndex]
insertIndex--;
}
arr[ insertIndex + 1] = insertVal;
System.out.println( "第2轮插入" ) ;
System.out.println( Arrays.toString( arr)) ;
//第3轮
insertVal = arr[ 3] ;
insertIndex = 3 - 1;
while ( insertIndex >= 0 && insertVal < arr[ insertIndex] ) {
arr[ insertIndex + 1] = arr[ insertIndex] ; // arr[ insertIndex]
insertIndex--;
}
arr[ insertIndex + 1] = insertVal;
System.out.println( "第3轮插入" ) ;
System.out.println( Arrays.toString( arr)) ; */
}
}
选择排序
int element = 10;
int[ ] arr = new int[ element] ;
//选择排序
public void selectSort( ) {
int min = 0;
int temp = 0;
for( int i = 0; i < element -1; i++) {
min = i;
for( int j = i+1; j < element; j++) {
if( arr[ j] < arr[ min] ) {
min = j;
}
}
temp = arr[ i] ;
arr[ i] = arr[ min] ;
arr[ min] = temp;
}
}