410 likes | 509 Vues
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 .
E N D
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. • 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
Basic Algorithms • Search • Find largest and/or smallest numbers • Binary search • Sort • Selection sort • Insertion sort • Bubble sort • Quick sort
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)
Algoritm-1 85 85 85 85 85 77 77 16 77 25 85 12 8 36 52
Algoritm-2 85 85 52 85 12 77 52 16 77 25 85 12 8 36 52
Complexity • Algorithm-1 • n-1 comparisons • Algorithm-2 • n/2+n/2^2+n/2^3+...=n-1 comparisons
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)
Algoritm-1 85 85 85 85 85 77 77 16 77 25 85 12 8 36 52
Algoritm-1 8 8 8 12 16 16 16 77 25 12 8 36 52
Algoritm-2 85 85 52 85 12 77 52 16 77 25 85 12 8 36 52
Algoritm-2 8 16 8 25 16 8 36
Complexity • Algorithm-1 • (n-1)+(n-2)=2n-3 comparisons • Algorithm-2 • (n-1)+(n/2-1)=3n/2-2 comparisons
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)
Algoritm-1 85 85 85 85 85 77 77 16 77 25 85 12 8 36 52
Algoritm-1 77 77 77 77 77 77 16 77 25 12 8 36 52
Algoritm-2 85 85 52 85 12 77 52 16 77 25 85 12 8 36 52
Algoritm-2 77 77 25 77 52
Complexity • Algorithm-1 • (n-1)+(n-2)=2n-3 comparisons • Algorithm-2 • (n-1)+(log2n-1) comparisons
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
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]); } }
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
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 }
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.
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
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.
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).
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.
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
Complexity • Selection sort • O(n2) • Insertion sort • df • Bubble sort • Quick sort
Binary Search on a Sorted List Search for X Mid X>Mid X<Mid Mid=X Search this side Search this side
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; } }