We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
public class Besesort { public void sort(int []a){ System.out.println("排序算法"); } } //选择排序 public class SelectSort extends Besesort { public void sort(int[] a) { System.out.println("选择排序"); int pos; for (int i = 1; i <= a.length - 1; i++) { pos = 0; for (int j = 1; j <= a.length - i; j++) { if (a[pos] < a[j]) pos = j; } int t = a.length - i; if (pos != t) { int temp = a[t]; a[t] = a[pos]; a[pos] = temp; } } for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } } //快速排序 public class QuickSort extends Besesort{ public void sort(int []a){ qusort(a,0,a.length - 1); System.out.println("快速排序"); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } void qusort(int[] a, int start, int end){ int i,j; i = start; j = end; a[0] = a[start]; while (i < j){ while (i < j && a[0] < a[j]) j --; if (i < j){ a[i] = a[j]; i ++; } while (i < j && a[0] >= a[i]) i ++; if (i < j){ a[j] = a[i]; j --; } } a[start] = a[i]; if (start < i) qusort(a,start,j - 1); if (j < end) qusort(a,j + 1,end); } } //结果错了 //输入9 8 7 6 5 4 3 2 1 0 //输出9 1 2 3 4 5 6 7 8 9 //插入排序 public class InsertSort extends Besesort { public void sort(int[]a){ int target; int j; int n = a.length; for (int i = 0; i < n; i++) { j = i; target = a[i]; while (j > 0 && target < a[j - 1]){ a[j] = a[j - 1]; j --; } a[j] = target; } System.out.println("插入排序"); for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } } //冒泡排序 public class BubbleSort extends Besesort { public void sort(int[] a) { System.out.println("冒泡排序"); int temp; for (int i = 1; i <= a.length - 1; i++) { for (int j = 1; j <= a.length - i ; j++) { if (a[j - 1] > a[j]) { temp = a[j - 1]; a[j - 1] = a[j]; a[j] = temp; } } } for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } } //结果错了 //输入9 8 7 6 5 4 3 2 1 0 //输出1 2 3 4 5 6 7 8 9 9 //策略类 public class Factory { private Besrsort sort; public void setSort(Besrsort sort) { this.sort(a) = sort; } public void doSort(int[] a) { sort.sort(a); } } Test //输入10个整数9 8 7 6 5 4 3 2 1 0 public class Test { public static void main(String[] args){ //从键盘输入10个整数 Scanner sc = new Scanner(System.in); int []a = new int[10]; for (int i = 0; i < a.length; i++) { a[i] = sc.nextInt(); } //选择排序 Factory factory1 = new Factory(); Besesort select_sort = new SelectSort(); factory1.setSort(select_sort); factory1.doSort(a); //快速排序 Factory factory3 = new Factory(); Besesort quicksort = new QuickSort(); factory3.setSort(quick_sort); factory3.doSort(a); //插入排序 Factory factory2 = new Factory(); Besesort insertsort = new InsertSort(); factory2.setSort(insert_sort); factory2.doSort(a); //冒泡排序 Factory factory4 = new Factory(); Besesort bulle_sort = new BubbleSort(); factory4.setSort(bulle_sort); factory4.doSort(a); }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
The text was updated successfully, but these errors were encountered: