You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicclassBaseSort {
publicvoidsort(int []a){
System.out.println("排序算法");
}
publicvoidsort ( int [] a , intstart , intend ) {
System.out.println("重载排序算法");
}
}
InsertSort
publicclassInsertSortextendsBaseSort{
@Overridepublicvoidsort(int[] a) {
super.sort(a);
for ( inti = 0 ; i < a.length ; i ++ ) {
for ( intj = i + 1 ; j < a.length ; j ++) {
if ( a[i] > a[j] ) {
intt = a[i] ;
a[i] = a[j] ;
a[j] = t ;
}
}
System.out.println("冒泡排序");
}
}
}
QuickSort
publicclassQuickSortextendsBaseSort {
@Overridepublicvoidsort(int[] a, intstart, intend) {
super.sort(a, start, end);
inti , j , t , p ;//p放基准数,t用于交换,ij 控制循环if ( start > end ) {
return ;
}//判断是否还需要再排i = start ;
j = end ;
p = a[start] ;
while ( i != j ) {
while (a[j] >= p && i < j ) {
j -- ;
}
while ( a[i] <= p && i < j ) {
i ++ ;
}
if ( i < j ) {
t = a[i] ;
a[i] = a[j] ;
a[j] = t ;
}
}
a[start] = a[i] ;
a[i] = p ;
sort( a , start , i - 1 );//递归,再对左边进行排序sort( a , i +1 , end );//递归,对右边进行排序System.out.println("快速排序");
}
}
SelectSort
publicclassSelectSortextendsBaseSort {
@Overridepublicvoidsort(int[] a) {
super.sort(a);
intk , t ;
for ( inti = 0 ; i <= 9 ; i ++ ) {
k = i ;
for ( intj = i + 1 ; j <= 9 ; j ++ ) {
if ( a[j] < a[k] ) {
k = j ;
}
t = a[k] ;
a[k] = a[i] ;
a[i] = t ;
}
}
System.out.println("选择排序");
}
}
Factory
publicclassFactory {
privateBaseSortsort;
//依赖注入publicvoidsetSort(BaseSortsort){
this.sort = sort;
}
publicvoiddoSort(int []a){
sort.sort(a);
}
publicvoiddoSort ( int [] a , intstart , intend ) {
sort.sort( a , start , end ) ;
}
}
Day1 Work
BaseSort
InsertSort
QuickSort
SelectSort
Factory
homework
The text was updated successfully, but these errors were encountered: