快排
package main;
import java. util. Scanner;
public class QuickSort {
public static void main ( String[ ] args) {
Scanner in = new Scanner ( System. in) ;
while ( true ) {
int len = in. nextInt ( ) ;
int [ ] nums = new int [ len] ;
for ( int i = 0 ; i < len; i++ ) {
nums[ i] = in. nextInt ( ) ;
}
quickSort ( nums, 0 , len- 1 ) ;
for ( int i: nums
) {
System. out. print ( i + " " ) ;
}
}
}
private static void quickSort ( int [ ] nums, int l, int r) {
if ( l < r) {
int partitionIndex = partition ( nums, l, r) ;
quickSort ( nums, l, partitionIndex- 1 ) ;
quickSort ( nums, partitionIndex+ 1 , r) ;
}
}
private static int partition ( int [ ] nums, int l, int r) {
int pivot = l;
int index = pivot+ 1 ;
for ( int i = index; i <= r; i++ ) {
if ( nums[ pivot] > nums[ i] ) {
swap ( nums, i, index) ;
index++ ;
}
}
swap ( nums, pivot, index- 1 ) ;
return index- 1 ;
}
private static void swap ( int [ ] nums, int i, int index) {
int temp = nums[ i] ;
nums[ i] = nums[ index] ;
nums[ index] = temp;
}
}
堆
堆的定义 堆是一棵节点含有内部比较器的完全二叉树。(说白了,堆就是完全二叉树,只不过它的节点对象实现了comparable接口)。也有种说法是,一个可以自我调整的完全二叉树. 堆的特性 每个父节点都大于等于(或者小于等于)其所有后代结点。 堆的根节点含有堆中最大(或者最小)的对象。 堆中以任意节点为根节点的子树仍然是堆。 堆的分类 最大堆(大根堆,大顶堆):根节点对象是最大对象的堆 最小堆(小根堆,小顶堆):根节点对象是最小对象的堆
堆排序
public class HeapSort {
public static void main ( String[ ] args) {
int [ ] nums = { 16 , 7 , 3 , 20 , 17 , 8 } ;
headSort ( nums) ;
for ( int num : nums) {
System. out. print ( num + " " ) ;
}
}
public static void headSort ( int [ ] list) {
for ( int i = ( list. length) / 2 - 1 ; i >= 0 ; i-- ) {
headAdjust ( list, list. length, i) ;
}
for ( int i = list. length - 1 ; i >= 1 ; i-- ) {
int temp = list[ 0 ] ;
list[ 0 ] = list[ i] ;
list[ i] = temp;
headAdjust ( list, i, 0 ) ;
}
}
private static void headAdjust ( int [ ] list, int len, int i) {
int k = i, temp = list[ i] , index = 2 * k + 1 ;
while ( index < len) {
if ( index + 1 < len) {
if ( list[ index] < list[ index + 1 ] ) {
index = index + 1 ;
}
}
if ( list[ index] > temp) {
list[ k] = list[ index] ;
k = index;
index = 2 * k + 1 ;
} else {
break ;
}
}
list[ k] = temp;
}
}
插入排序
public class InsertSort {
public static void sort ( int [ ] arr) {
if ( arr. length >= 2 ) {
for ( int i = 1 ; i < arr. length; i++ ) {
int x = arr[ i] ;
int j = i - 1 ;
while ( j >= 0 && arr[ j] > x) {
arr[ j + 1 ] = arr[ j] ;
j-- ;
}
arr[ j + 1 ] = x;
}
}
}
}
冒泡排序
public class MaoPao {
public static void sort ( int [ ] arr) {
for ( int i = 1 ; i < arr. length; i++ ) {
for ( int j = 0 ; j < arr. length- 1 ; j++ ) {
if ( arr[ j] > arr[ j+ 1 ] ) {
int temp = arr[ j] ;
arr[ j] = arr[ j+ 1 ] ;
arr[ j+ 1 ] = temp;
}
}
}
}
}
public static void sortPlus ( int [ ] arr) {
if ( arr != null && arr. length > 1 ) {
for ( int i = 0 ; i < arr. length - 1 ; i++ ) {
boolean flag = true ;
for ( int j = 0 ; j < arr. length - i - 1 ; j++ ) {
if ( arr[ j] > arr[ j+ 1 ] ) {
int temp;
temp = arr[ j] ;
arr[ j] = arr[ j+ 1 ] ;
arr[ j+ 1 ] = temp;
flag = false ;
}
}
if ( flag) {
break ;
}
}
}
}
选择排序
public static void sort ( int [ ] arr) {
for ( int i = 0 ; i < arr. length - 1 ; i++ ) {
int min = i;
for ( int j = i + 1 ; j < arr. length ; j++ ) {
if ( arr[ j] < arr[ min] ) {
min = j;
}
}
if ( min != i) {
int temp = arr[ min] ;
arr[ min] = arr[ i] ;
arr[ i] = temp;
}
}
}
归并排序
public static void mergeSort ( int [ ] a, int s, int e) {
int m = ( s + e) / 2 ;
if ( s < e) {
mergeSort ( a, s, m) ;
mergeSort ( a, m+ 1 , e) ;
merge ( a, s, m, e) ;
}
}
private static void merge ( int [ ] a, int s, int m, int e) {
int [ ] temp = new int [ ( e - s) + 1 ] ;
int l = s;
int r = m+ 1 ;
int i = 0 ;
while ( l <= m && r <= e) {
if ( a[ l] < a[ r] ) {
temp[ i++ ] = a[ l++ ] ;
} else {
temp[ i++ ] = a[ r++ ] ;
}
}
while ( l <= m) {
temp[ i++ ] = a[ l++ ] ;
}
while ( r <= e) {
temp[ i++ ] = a[ r++ ] ;
}
for ( int n = 0 ; n < temp. length; n++ ) {
a[ s+ n] = temp[ n] ;
}
}