交通大学资讯工程学系.pptxVIP

  • 0
  • 0
  • 约4.24千字
  • 约 58页
  • 2021-07-12 发布于河北
  • 举报
Programming in Java;Agenda;Computing Factorial;Factorial -- Iteration;Recursion 遞迴概念;Factorial – Recursion, in Java;N Factorial ( Recursive版 );歐幾里得的最大公約數;最大公約數 non-recursive版;Recursive 也可以很有趣(1/3);Recursive 也可以很有趣(2/3);;問題與思考 (Recursion);Factorial – cache ans in a Table;Computing Primes (1/3);Computing Primes (2/3);Computing Primes (3/3);Example: Find Primes (1/2);Example: Find Primes (2/2);Sorting Numbers;Selection Sort (1/4);Selection Sort (2/4);Selection Sort (3/4);Selection Sort (4/4);;;Insertion Sort;Test the Insertion Sort;Bubble Sort; Algorithm quick_sort(array A, from, to) Input: from - pointer to the starting position of array A to - pointer to the end position of array A Output: sorted array: A’ 1. Choose any one element as the pivot; 2. Find the first element a = A[i] larger than or equal to pivot from A[from] to A[to]; 3. Find the first element b = A[j] smaller than or equal to pivot from A[to] to A[from]; 4. If i j then exchange a and b; 5. Repeat step from 2 to 4 until j = i; 6. If from j then recursive call quick_sort(A, from, j); 7. If i to then recursive call quick_sort(A, i, to);;;; public class QuickSorter { public static void sort (int[ ] a, int from, int to) { if ((a == null) || (a.length 2)) return; int i = from, j = to; int pivot = a[(from + to)/2]; do { while ((i to) (a[i] pivot)) i++; while ((j from) (a[j] = pivot)) j--; if (i j) { int tmp =a[i]; a [i] = a[j]; a[j] = tmp;} i++; j--; }while (i = j); if (from j) sort(a, from, j); if (i to) sort(a, i, to); } } ;14; public class BQSorter { public static void sort (int[ ] a, int from, int to) { if ((a == null) || (a.length 2) || from = to) return; int k = (from + to)/2; int tmp =a[to]; a [to] = a[k]; a[k] = tmp; int pivot = a[to]; int i = from, j = to-1; while(i j ) { while ((i j) (a[i] pivot)) i++; while ((i j) (a[j] = pivot)) j--; if (i j) { tmp =a[i]; a [i]

文档评论(0)

1亿VIP精品文档

相关文档