Download presentation
Presentation is loading. Please wait.
Published byReginald Burke Modified over 9 years ago
1
Computation for Physics 計算物理概論 Algorithm
2
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 167725851283652 77 85
6
Algoritm-2 167725851283652 77 85 12 85 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 167725851283652 77 85
10
Algoritm-1 1677251283652 16 12 8 8 8
11
Algoritm-2 167725851283652 77 85 12 85 52
12
Algoritm-2 1625 836 16 8 8
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 167725851283652 77 85
16
Algoritm-1 1677251283652 77
17
Algoritm-2 167725851283652 77 85 12 85 52
18
Algoritm-2 2577 52 77
19
Complexity Algorithm-1 – (n-1)+(n-2)=2n-3 comparisons Algorithm-2 – (n-1)+(log 2 n-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 1677258512836 8772585121636 8122585771636 8121685772536 8121625778536 8121625368577 8121625367785
22
Selection Sort /* a[0] to a[n-1] is the array to sort */ int i,j; int iMin; /* 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 1677258512836 1677258512836 1625778512836 1625778512836 1216257785836 8121625778536 8121625367785
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 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 1677258512836 1677258512836 1677258581236 1677258851236 1677825851236 1687725851236 8167725851236
28
Bubble Sort: Pass-2 8167725851236 8167725851236 8167725128536 8167712258536 8161277258536 8121677258536 8121677258536
29
Bubble Sort: Pass-3 8121677258536 8121677253685 8121677253685 8121625773685 8121625773685 8121625773685 8121625773685
30
Bubble Sort: Pass-4 8121625773685 8121625773685 8121625367785 8121625367785 8121625367785 8121625367785 8121625367785
31
Bubble Sort: Pass-5; No Swap Sorted 8121625367785 8121625367785 8121625367785 8121625367785 8121625367785 8121625367785 8121625367785
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 Sort Divide 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
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 Mid Search for X Mid=X X<MidX>Mid Search this side
41
Binary Search int binary_search(int A[], int key, int imin, int imax) { // 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 int imid = 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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.