二分法查找:
public class Find {
public static int find ( int [ ] array, int aim) {
int left = 0 ;
int right = array. length - 1 ;
while ( left<= right) {
int middle = ( left + right) / 2 ;
int middleValue = array[ middle] ;
if ( middleValue == aim) {
return middle;
} else if ( middleValue > aim) {
left = middle + 1 ;
} else {
right = middle - 1 ;
}
}
return - 1 ;
}
public static void main ( String[ ] args) {
int [ ] array = { 100 , 90 , 80 , 75 , 22 , 3 , 2 } ;
int result1 = find ( array, 22 ) ;
if ( result1 == - 1 ) {
System. out. println ( "22 不存在数组中" ) ;
} else {
System. out. println ( "22 存在数组中,索引值是 " + result1) ;
}
int result2 = find ( array, 50 ) ;
if ( result2 == - 1 ) {
System. out. println ( "50 不存在数组中" ) ;
} else {
System. out. println ( "50 存在数组中,索引值是 " + result2) ;
}
}
}
自制数组(支持增删数据,数组扩容)
public class YKDArrayList {
int [ ] array = new int [ 20 ] ;
int size = 0 ;
public YKDArrayList ( ) {
}
public int size ( ) {
return this . size;
}
public int get ( int index) {
return this . array[ index] ;
}
public void add ( int element) {
this . add ( this . size, element) ;
}
public void add ( int index, int element) {
if ( index < 0 || index > this . size) {
return ;
}
this . judgeMemory ( this . size + 1 ) ;
for ( int i = this . size - 1 ; i >= index; i-- ) {
this . array[ i + 1 ] = this . array[ i] ;
}
this . array[ index] = element;
this . size++ ;
}
public void remove ( int index) {
if ( index < 0 || index > this . size - 1 ) {
return ;
}
for ( int i = index; i < this . size - 1 ; i++ ) {
this . array[ i] = this . array[ i + 1 ] ;
}
this . array[ this . size - 1 ] = 0 ;
this . size-- ;
}
private void judgeMemory ( int size) {
if ( size > this . array. length) {
int [ ] newArray = new int [ this . array. length * 2 ] ;
this . copy ( this . array, newArray) ;
this . array = newArray;
}
}
private void copy ( int [ ] source, int [ ] aim) {
for ( int i = 0 ; i < source. length; i++ ) {
aim[ i] = source[ i] ;
}
}
public static void main ( String[ ] args) {
YKDArrayList ykdArrayList = new YKDArrayList ( ) ;
ykdArrayList. add ( 1 ) ;
ykdArrayList. add ( 2 ) ;
ykdArrayList. add ( 3 ) ;
ykdArrayList. add ( 4 ) ;
ykdArrayList. add ( 0 , 5 ) ;
ykdArrayList. remove ( 3 ) ;
}
}
冒泡排序:
import java. util. Arrays;
public class Sort {
public static void bubbleSort ( int [ ] array) {
for ( int i = 0 ; i < array. length; i++ ) {
for ( int j = 0 ; j < array. length - i - 1 ; j++ ) {
if ( array[ j] < array[ j + 1 ] ) {
int temp = array[ j] ;
array[ j] = array[ j+ 1 ] ;
array[ j+ 1 ] = temp;
}
}
}
}
public static void main ( String[ ] args) {
int [ ] array = { 9 , 2 , 4 , 7 , 5 , 3 } ;
System. out. println ( Arrays. toString ( array) ) ;
bubbleSort ( array) ;
System. out. println ( Arrays. toString ( array) ) ;
}
}
选择排序:
import java. util. Arrays;
public class Sort {
public static void selectSort ( int [ ] array) {
for ( int i = 0 ; i < array. length; i++ ) {
int maxIndex = 0 ;
int max = array[ 0 ] ;
for ( int j = 1 ; j < array. length - i; j++ ) {
if ( array[ j] > max) {
max = array[ j] ;
maxIndex = j;
}
}
int temp = array[ maxIndex] ;
array[ maxIndex] = array[ array. length - i - 1 ] ;
array[ array. length - i - 1 ] = temp;
}
}
public static void main ( String[ ] args) {
int [ ] array = { 9 , 2 , 4 , 7 , 5 , 3 } ;
System. out. println ( Arrays. toString ( array) ) ;
selectSort ( array) ;
System. out. println ( Arrays. toString ( array) ) ;
}
}
import java. util. Arrays;
public class Sort {
public static void selectSort ( int [ ] array) {
for ( int i = 0 ; i< array. length- 1 ; i++ ) {
int m = i;
for ( int j = i+ 1 ; j< array. length; j++ ) {
if ( array[ j] < array[ m] ) {
m = j;
}
if ( m!= i) {
int temp = array[ i] ;
array[ i] = array[ m] ;
array[ m] = temp;
}
}
}
}
public static void main ( String[ ] args) {
int [ ] array = { 9 , 2 , 4 , 7 , 5 , 3 } ;
System. out. println ( Arrays. toString ( array) ) ;
selectSort ( array) ;
System. out. println ( Arrays. toString ( array) ) ;
}
}
插入排序:
import java. util. Arrays;
public class Sort {
public static void insertSort ( int [ ] array) {
for ( int i = array. length- 2 ; i>= 0 ; i-- ) {
int temp = array[ i] ;
int j = i+ 1 ;
while ( j< array. length) {
if ( array[ j] < temp) {
array[ j- 1 ] = array[ j] ;
} else {
array[ j- 1 ] = temp;
break ;
}
j++ ;
}
if ( j== array. length) {
array[ j- 1 ] = temp;
}
}
}
public static void main ( String[ ] args) {
int [ ] array = { 9 , 2 , 4 , 7 , 5 , 3 } ;
System. out. println ( Arrays. toString ( array) ) ;
insertSort ( array) ;
System. out. println ( Arrays. toString ( array) ) ;
}
}
二分插入排序:
import java. util. Arrays;
public class Sort {
public static int searchIndex ( int [ ] array, int left, int right, int aim) {
while ( left < right) {
int middle = ( left + right) / 2 ;
int value = array[ middle] ;
if ( value < aim) {
left = middle + 1 ;
} else {
right = middle - 1 ;
}
}
if ( array[ left] > aim) {
return left - 1 ;
}
return left;
}
public static void insertSort ( int [ ] array) {
for ( int i = array. length - 2 ; i >= 0 ; i-- ) {
int temp = array[ i] ;
int index = searchIndex ( array, i + 1 , array. length - 1 , temp) ;
int j = i + 1 ;
while ( j <= index) {
array[ j - 1 ] = array[ j] ;
j++ ;
}
array[ j - 1 ] = temp;
}
}
public static void main ( String[ ] args) {
int [ ] array = { 9 , 2 , 4 , 7 , 5 , 3 } ;
System. out. println ( Arrays. toString ( array) ) ;
insertSort ( array) ;
System. out. println ( Arrays. toString ( array) ) ;
}
}