1 / 41

Computation for Physics 計算物理概論

Computation for Physics 計算物理概論. Algorithm. Algorithm. An algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning .

feo
Download Presentation

Computation for Physics 計算物理概論

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computation for Physics計算物理概論 Algorithm

  2. Algorithm • An algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning. • An algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Starting from an initial state and initial input (perhaps empty), the instructions describe a computation that, when executed, proceeds through a finite number of well-defined successive states, eventually producing “output” and terminating at a final ending state. The transition from one state to the next is not necessarily deterministic; some algorithms, known as randomized algorithms, incorporate random input. • --From Wikipedia

  3. Basic Algorithms • Search • Find largest and/or smallest numbers • Binary search • Sort • Selection sort • Insertion sort • Bubble sort • Quick sort

  4. Find the Largest Number • Given n numbers, we want to find the largest number • Example • Given 8 numbers: [16,77,25,85,12,8,36,52] • Largest number (=85)

  5. Algoritm-1 85 85 85 85 85 77 77 16 77 25 85 12 8 36 52

  6. Algoritm-2 85 85 52 85 12 77 52 16 77 25 85 12 8 36 52

  7. Complexity • Algorithm-1 • n-1 comparisons • Algorithm-2 • n/2+n/2^2+n/2^3+...=n-1 comparisons

  8. Find the Largest and Smallest Numbers • Given n numbers, we want to find the largest and the smallest number • Example • Given 8 numbers: [16,77,25,85,12,8,36,52] • Largest number (=85). • Smallest number (=8)

  9. Algoritm-1 85 85 85 85 85 77 77 16 77 25 85 12 8 36 52

  10. Algoritm-1 8 8 8 12 16 16 16 77 25 12 8 36 52

  11. Algoritm-2 85 85 52 85 12 77 52 16 77 25 85 12 8 36 52

  12. Algoritm-2 8 16 8 25 16 8 36

  13. Complexity • Algorithm-1 • (n-1)+(n-2)=2n-3 comparisons • Algorithm-2 • (n-1)+(n/2-1)=3n/2-2 comparisons

  14. Find the Largest and Second Largest Numbers • Given n numbers, we want to find the largest and the second largest number • Example • Given 8 numbers: [16,77,25,85,12,8,36,52] • Largest number (=85). • Second largest number (=77)

  15. Algoritm-1 85 85 85 85 85 77 77 16 77 25 85 12 8 36 52

  16. Algoritm-1 77 77 77 77 77 77 16 77 25 12 8 36 52

  17. Algoritm-2 85 85 52 85 12 77 52 16 77 25 85 12 8 36 52

  18. Algoritm-2 77 77 25 77 52

  19. Complexity • Algorithm-1 • (n-1)+(n-2)=2n-3 comparisons • Algorithm-2 • (n-1)+(log2n-1) comparisons

  20. Selection Sort • Divide the input list into two sub-lists • Sorted sub-list (Start with empty list) • Un-Sorted sub-list • Find the smallest (largest) element in the unsorted list. • Swap with the leftmost unsorted element • Move the sub-list boundaries

  21. Selection Sort

  22. Selection Sort /* a[0] to a[n-1] is the array to sort */ inti,j; intiMin; /* advance the position through the entire array */ /* (could do j < n-1 because single element is also min element) */ for (j = 0; j < n-1; j++) { /* find the min element in the unsorted a[j .. n-1] */ /* assume the min is the first element */ iMin = j; /* test against elements after j to find the smallest */ for ( i = j+1; i < n; i++) { /* if this element is less, then it is the new minimum */ if (a[i] < a[iMin]) { /* found new minimum; remember its index */ iMin = i; } } /* iMin is the index of the minimum element. Swap it with the current position */ if ( iMin != j ) { swap(a[j], a[iMin]); } }

  23. Insertion Sort • Divide the input list into two sub-lists • Sorted sub-list (Start with leftmost element) • Un-Sorted sub-list • Insert the leftmost element of the unsorted sub-list into the proper position of the sorted sub-list. • You can move leftmost element to the left until it • Move the sub-list boundaries

  24. Insertion Sort

  25. Insertion Sort for i ← 1 to i ← length(A)-1 { //The values in A[ i ] are checked in-order, starting at the second one // save A[i] to make a hole that will move as elements are shifted // the value being checked will be inserted into the hole's final position valueToInsert ← A[i] holePos ← i // keep moving the hole down until the value being checked is larger than // what's just below the hole <!-- until A[holePos - 1] is <= item --> while holePos > 0 and valueToInsert < A[holePos - 1] { //value to insert doesn't belong where the hole currently is, so shift A[holePos] ← A[holePos - 1] //shift the larger value up holePos ← holePos - 1 //move the hole position down } // hole is in the right position, so put value being checked into the hole A[holePos] ← valueToInsert }

  26. Bubble Sort • Divide the input list into two sub-lists • Sorted sub-list (Start with empty list) • Un-Sorted sub-list • Start from the rightmost element and compare it to its left element. Swap them if the right element is smaller. Continue the process to the leftmost two elements. • Repeat until there is no swap during the sweep.

  27. Bubble Sort: Pass-1

  28. Bubble Sort: Pass-2

  29. Bubble Sort: Pass-3

  30. Bubble Sort: Pass-4

  31. Bubble Sort: Pass-5; No Swap Sorted

  32. Bubble Sort procedure bubbleSort( A : list of sortable items ) repeat swapped = false for i = 1 to length(A) - 1 inclusive do: /* if this pair is out of order */ if A[i-1] > A[i] then /* swap them and remember something changed */ swap( A[i-1], A[i] ) swapped = true end if end for until not swapped end procedure

  33. Try: Sorting • Write following functions • random_list(N) • Create a list of random integers of size N where the integer is in the range of 0..N-1. • Use randint(a,b). • selection_sort(list) • insertion_sort(list) • bubble_sort(list) • Convert a unsorted list into a sorted list. • Use build-in function sorted(list) to check results.

  34. Try: Scaling of Soring • Generate a plot with three lines • x-axis = size of the list. • y-axis = time used to sort the list using • Selection sort. • Insertion sort. • Bubble sort. • Plot using • plot(x,y). • loglog(x,y).

  35. Quick SortDivide and Conquer • Pick an element, called a pivot, from the list. • Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. • Recursively apply the above steps to the sub-list of elements with smaller values and separately the sub-list of elements with greater values.

  36. Quick Sort

  37. Quick Sort function quicksort('array') if length('array') ≤ 1 return 'array' // an array of zero or one elements is already sorted select and remove a pivot value 'pivot' from 'array' create empty lists 'less' and 'greater' for each 'x' in 'array' if 'x' ≤ 'pivot' then append 'x' to 'less' else append 'x' to 'greater' return concatenate(quicksort('less'), 'pivot', quicksort('greater')) // two recursive calls

  38. Complexity • Selection sort • O(n2) • Insertion sort • df • Bubble sort • Quick sort

  39. Sequential Search on a Un-Sorted List

  40. Binary Search on a Sorted List Search for X Mid X>Mid X<Mid Mid=X Search this side Search this side

  41. Binary Search intbinary_search(int A[], int key, intimin, intimax) { // test if array is empty if (imax < imin): // set is empty, so return value showing not found return KEY_NOT_FOUND; else { // calculate midpoint to cut set in half intimid = midpoint(imin, imax); // three-way comparison if (A[imid] > key) // key is in lower subset return binary_search(A, key, imin, imid-1); else if (A[imid] < key) // key is in upper subset return binary_search(A, key, imid+1, imax); else // key has been found return imid; } }

More Related