交大資工 蔡文能 計概 sorting-1 sorting & searching 蔡文能 tsaiwn@csie.nctu.edu.tw...

Post on 21-Dec-2015

268 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

交大資工 蔡文能 計概 Sorting-1

Sorting & Searching

蔡文能tsaiwn@csie.nctu.edu.tw交通大學資訊工程學系

交大資工 蔡文能 計概 Sorting-2

Problem Solving Steps

1. Understand the problem2. Get an idea3. Formulate the algorithm and represent it

as a program (or pseudo code)4. Evaluate the program

1. For accuracy2. For its potential as a tool for solving other

problems

交大資工 蔡文能 計概 Sorting-3

Sorting 排序 ( 排列 )• Take a set of items, order unknown

• Return ordered set of the items

• For instance:Sorting names alphabeticallySorting by scores in descending orderSorting by height in ascending order

Issues of interest:– Running time in worst case, average/other cases

– Space requirements

交大資工 蔡文能 計概 Sorting-4

常見簡單 Sorting 技巧• Insertion Sort 插入排列法• Selection Sort 選擇排列法• Bubble Sort 氣泡排列法 (Sibling exchange sort; 鄰近比較交換法 )

• Other Sorting techniques– Quick Sort, Heap Sort, Merge Sort, ..

– Shell Sort, Fibonacci Sort

交大資工 蔡文能 計概 Sorting-5

Sorting the list Fred, Alice, David, Bill, and Carol alphabetically

Insertion Sort

交大資工 蔡文能 計概 Sorting-6

Sorting the list Fred, Alice, David, Bill, and Carol alphabetically

(continued)

交大資工 蔡文能 計概 Sorting-7

Sorting the list Fred, Alice, David, Bill, and Carol alphabetically (cont.)

交大資工 蔡文能 計概 Sorting-8

The insertion sort algorithm expressed in pseudocode

Key idea: Keep part of array always sorted

交大資工 蔡文能 計概 Sorting-9

Insertion Sort in C Language

void sort( double x[ ], int nox) { int n = 1, k; double tmp; /* C array 從 0 開始 */ while(n <= nox-1) { k=n-1; tmp = x[n]; /* 我先放到 tmp */ while( k>=0 && x[k]>tmp){ x[k+1] = x[k]; /* 前面的 copy 到下一個 */ --k; } x[k+1] = tmp; ++n; /* check next element */ }}

0

nox-1

Lazy evaluation(short-cut evaluation)

1

Ascending order

交大資工 蔡文能 計概 Sorting-10

Test the Sort Algorithmdouble y[ ] = {15, 38, 12, 75, 20, 66, 49, 58};#include<stdio.h>void pout(double*, int); void sort(double*, int);int main( ) { printf("Before sort:\n"); pout(y, 8); sort(y, sizeof(y)/sizeof(double) ); printf(" After sort:\n"); pout(y, 8);}void pout(double*p, int n) { int i; for(i=0; i<=n-1; ++i) { printf("%7.2f ", p[i]); } printf(" \n");}

Before sort:

15.00 38.00 12.00 75.00 20.00 66.00 49.00 58.00

After sort:

12.00 15.00 20.00 38.00 49.00 58.00 66.00 75.00

交大資工 蔡文能 計概 Sorting-11

Insertion Sort Summary

• Best case: Already sorted O(n)• Worst case:

– # of comparisons : O(n2)

– # of exchanges: O(n2) : 剛好相反順序時• Space: No external storage needed• In practice, good for small sets (<30 items)• Very efficient on nearly-sorted inputs• 想要減少 data 交換次數 : Selection Sort

交大資工 蔡文能 計概 Sorting-12

Selection Sort 選擇排列法

void sort( double x[ ], int nox) { int i, k, candt; double tmp; for(i = 0; i < nox-1; ++i) { candt = i; /* assume this is our candidate */ for( k= i+1; k<=nox-1; ++k) { if(x[k] < x[candt]) candt = k; /* that is it */ } tmp=x[i]; x[i]=x[candt]; x[candt]=tmp; /* 第 i 個到定位 */ }}

array index 由 0 到 n-1

選出剩下中最小的

Ascending order

交大資工 蔡文能 計概 Sorting-13

SelectSort(array A,length n)

1. for in-1 to 1 // note we are going down

2. largest_index 0 // assume 0-th is largest

3. for j 1 to i // loop finds max in [1..i]

4. if A[j] > A[largest_index]

5. largest_index j

6. next j

7. swap(A[i],A[largest_index]) //put max in i

8. Next i

array index 由 0 到 n-1

選出的放最後 ( 第 i 個 )

Another version of selection sort

交大資工 蔡文能 計概 Sorting-14

Selection Sort Summary

• Best case: Already sorted– Passes: n-1 – Comparisons each pass: (n-k) where k pass number– # of comparisons: (n-1)+(n-2)+…+1 = O(n2)

• Worst case: O(n2)

• Space: No external storage needed

• Very few exchanges: – Always n-1 (better than Bubble Sort)

交大資工 蔡文能 計概 Sorting-15

Bubble Sort-v1 氣泡排列法

void sort( double x[ ], int nox) {

int i, k; double tmp;

for(i = nox-1; i >=1; --i) {

for( k= 0; k< i; ++k) {

if(x[k] > x[k+1]) { /* 左大右小 , 需要調換 */

tmp= x[k]; x[k]=x[k+1]; x[k+1]=tmp;

}

} // for k

} // for i

}

Sibling exchange sort

鄰近比較交換法

Ascending orderarray index 由 0 到 n-1

交大資工 蔡文能 計概 Sorting-16

Bubble Sort-v1 example(1/2)15 38 12 75 20 66 49 58

15 38 12 75 20 66 49 58

15 12 38 75 20 66 49 58

15 12 38 75 20 66 49 58

15 12 38 20 75 66 49 58

15 12 38 20 66 75 49 58

15 12 38 20 66 49 75 58

15 12 38 20 66 49 58 75

第一回合 ( pass 1 ) : 7 次比較

第一回合後 75 到定位

交大資工 蔡文能 計概 Sorting-17

Bubble Sort-v1 example (2/2)15 38 12 75 20 66 49 58 (original)

第一回合後 75 到定位 :

15 12 38 20 66 49 58 75

第二回合後 66 到定位 :

12 15 20 38 49 58 66 75

第三回合 ( pass 3 ) ?

12 15 20 38 49 58 66 75

剛剛都沒換 ; 還需要再做下一回合 (pass) 嗎 ?

sorted

交大資工 蔡文能 計概 Sorting-18

Bubble Sort-v1 Features• Time complexity in Worst case: Inverse sorting

– Passes: need n-1 passes– Comparisons each pass: (n-k) where k is pass number– Total number of comparisons:

(n-1)+(n-2)+(n-3)+…+1 = n(n-1)/2=n2/2-n/2 = O(n2)

• Space: No auxilary storage needed• Best case: already sorted

– O(n2) Still: Many redundant passes with no swaps– Can be improved by using a Flag

交大資工 蔡文能 計概 Sorting-19

Bubble Sort-v2 氣泡排列法

void sort( double x[ ], int nox) { int i, k, flag; double tmp; for(i = nox-1; i >=1; --i) { flag = 0; /* assume no exchange in this pass */ for( k= 0; k< i; ++k) { if(x[k] > x[k+1]) { /* 需要調換 */ tmp= x[k]; x[k]=x[k+1]; x[k+1]=tmp; flag=1; } // if } // for k if(flag==0) break; /* 剛剛這回合沒交換 , 不用再做 */ } // for i}

array index 由 0 到 n-1改良式氣泡排序法

交大資工 蔡文能 計概 Sorting-20

Bubble Sort –v2 Features

• Best case: Already sorted– O(n) – one pass

• Total number of exchanges– Best case: 0– Worst case: O(n2) ( 資料相反順序時 )Lots of exchanges:

A problem with large data items

交大資工 蔡文能 計概 Sorting-21

BubbleSort(array A[ ], int n)

1. in-1 2. quitfalse3. while(i>0 AND NOT quit)// note: going down4. quittrue5. for j=1 to i // loop does swaps in [1..i]6. if (A[j-1] > A[j]) {7. swap(A[j-1],A[j]) // put max in I8. quitfalse }9. next j10. ii-111.wend

array index 由 0 到 n-1

Another version of Bubble sort (in pseudo code)

交大資工 蔡文能 計概 Sorting-22

Selection Sort vs. Bubble Sort• Selection sort:

– more comparisons than bubble sort in best case• Always O(n2) comparisons : n(n-1)/2

– But fewer exchanges : O(n)

– Good for small sets/cheap comparisons, large items

• Bubble sort-v2:– Many exchanges : O(n2) in worst case

– O(n) on sorted input (best case) : only one pass

交大資工 蔡文能 計概 Sorting-23

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 AOutput: 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);

Quick Sort (1/6)

交大資工 蔡文能 計概 Sorting-24

• Quick sortmain idea:

1st step: 3 1 6 5 4 8 10 7

2nd step: 3 2 1 5 8 9 10 7

3rd step: 3 2 1 4 5 6 8 9 10 7

Choose 5 as pivotfrom to

2299

66 44

Smaller than any integerSmaller than any integerright to 5right to 5

greater than any integergreater than any integerleft to 5left to 5

ii jj

Quick Sort (2/6)

交大資工 蔡文能 計概 Sorting-25

• Quick sort

4th step: 2 4 5 6 10 9

5th step: 1 2 3 4 5

pivotfrom to

33 11 88 77

5 6 7 8 10 95 6 7 8 10 96th step:6th step:

pivotfrom to

7 8 10 97 8 10 97th step:7th step:

9 109 108th step:8th step:

Quick Sort (3/6)

交大資工 蔡文能 計概 Sorting-26

public class QuickSorter { // Java function should be in a classpublic 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); exchange(a, i, (from+to)/2 ); /***/if (from < j) sort(a, from, j);if (i < to) sort(a, i, to);

}}

Quick Sort (4/6)

交大資工 蔡文能 計概 Sorting-27

14

3, 4, 6, 1, 10, 9, 5, 20, 19, 141, 12, 2, 15, 21, 13, 18, 17, 8, 16,

3, 4, 6, 1, 10, 9, 5, 8, 19, 1, 12, 2, 15, 21, 13, 18, 17, ,16,

3, 4, 6, 1, 10, 9, 5, 8, 13 ,1, 12, 2, 15, 21, 19, 18, 17, 20, 16,

i

j3, 4, 6, 1, 10, 9, 5, 8, 13 , 1, 12, 2

ii jj

Quick Sort (5/6)3, 4, 6, 1, 10, 9, 5, 20, 19, 14, 12, 2, 15, 21, 13, 18, 17, 8, 16, 1

jjii20,

14

ii jj3, 4, 6, 1, 10, 9, 5, 8, 13 , 1, 12, 2, 14, 21, 19, 18, 17, 20, 16, 1514

交大資工 蔡文能 計概 Sorting-28

void qsort (int a[ ], int from, int to) { int n = to – from + 1;

if ( (n < 2) || (from >= to) ) return;int k = (from + to)/2; int tmp =a[to]; a [to] = a[k]; a[k] = tm

p;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] = a[j]; a[j] = tmp;}

}; tmp =a[i]; a [i] = a[to]; a[to] = tmp; // exchange

if (from < i-1) qsort(a, from, i-1);if (i < to) qsort(a, i+1, to);

}

Quick Sort (6/6)

交大資工 蔡文能 計概 Sorting-29

qsort( ) in C Library

• There is a library function for quick sort in C Language.

• #include <stdlib.h>void qsort(void *base, size_t num, size_t size,

int (*comp_func)(const void *, const void *) ) void * base --- a pointer to the array to be sorted

size_t num --- the number of elements

size_t size --- the element size

int (*cf) (…) --- is a pointer to a function used to compare

交大資工 蔡文能 計概 Sorting-30

Quick sort is NOT stable

• Definition of Stable sort?– A sorting algorithm is stable if whenever there

are two records R and S with the same key and with R appearing before S in the original list, R will appear before S in the sorted list.

– stable sorting algorithms maintain the relative order of records with equal keys.

(http://en.wikipedia.org/wiki/Sorting_algorithm)

交大資工 蔡文能 計概 Sorting-31

Merging means the combination of two or more ordered sequence into

a single sequence. For example, can merge two sequences: 503, 703, 765

and 087, 512, 677 to obtain a sequence: 087, 503, 512, 677, 703, 765.

A simple way to accomplish this is to compare the two smallest items,

output the smallest, and then repeat the same process.

503 703 765087 512 677 087

503 703 765512 677

087 503703 765512 677

Merge Sort (1/3)

交大資工 蔡文能 計概 Sorting-32

Algorithm Merge(s1, s2)Input: two sequences: s1 - x1 x2 ... xm and s2 - y1 y2 ... yn

Output: a sorted sequence: z1 z2 ... zm+n.1.[initialize] i := 1, j := 1, k := 1;2.[find smaller] if xi yj goto step 3, otherwise goto step 5;3.[output xi] zk.:= xi, k := k+1, i := i+1. If i m, goto step 2;4.[transmit yj ... yn] zk, ..., zm+n := yj, ..., yn. Terminate the algorith

m;5.[output yj] zk.:= yj, k := k+1, j := j+1. If j n, goto step 2;6.[transmit xi ... xm] zk, ..., zm+n := xi, ..., xm. Terminate the algorith

m;

Merge Sort (2/3)

交大資工 蔡文能 計概 Sorting-33

Algorithm Merge-sorting(s)

Input: a sequences s = < x1, ..., xm>Output: a sorted sequence.1. If |s| = 1, then return s;2. k := m/2;3. s1 := Merge-sorting(x1, ..., xk);4. s2 := Merge-sorting(xk+1, ..., xm);5. return(Merge(s1, s2));

Merge Sort (3/3)

交大資工 蔡文能 計概 Sorting-34

Binary Search

交大資工 蔡文能 計概 Sorting-35

Binary Search Algorithm

交大資工 蔡文能 計概 Sorting-36

Binary Search Algorithm in Pseudocode

交大資工 蔡文能 計概 Sorting-37

Searching for Bill

交大資工 蔡文能 計概 Sorting-38

Searching for David

David

交大資工 蔡文能 計概 Sorting-39

Searching for David

David David

交大資工 蔡文能 計概 Sorting-40

Software Efficiency

• Measured as number of instructions executed

• notation for efficiency classes– O( ? )– ( ? ) ( ? )

• Best, worst, and average case

交大資工 蔡文能 計概 Sorting-41

Asymptotic Upper Bound (Big O)

f(n)

c g(n)• f(n) c g(n) for all n n0 • g(n) is called an asymptotic upper bound of f(n).• We write f(n)=O(g(n))• It reads f(n) equals big oh of g(n).

n0

交大資工 蔡文能 計概 Sorting-42

Asymptotic Lower Bound (Big Omega)

f(n)

c g(n)

• f(n) c g(n) for all n n0 • g(n) is called an asymptotic lower bound of f(n).• We write f(n)=(g(n))• It reads f(n) equals big omega of g(n).

n0

交大資工 蔡文能 計概 Sorting-43

Asymptotically Tight Bound (Big Theta)

f(n)

c1 g(n)

• f(n) = O(g(n)) and f(n) = (g(n))• g(n) is called an asymptotically tight bound of f(n).• We write f(n)=(g(n))• It reads f(n) equals theta of g(n).

n0

c2 g(n)

交大資工 蔡文能 計概 Sorting-44

Insertion Sort in Worst Case

交大資工 蔡文能 計概 Sorting-45

Worst-Case Analysis Insertion Sort

交大資工 蔡文能 計概 Sorting-46

Time complexity of Mergesort

• Takes roughly n·log2 n comparisons.

• Without the shortcut, there is no best or worst case.

• With the optional shortcut, the best case is when the array is already sorted: takes only (n-1) comparisons.

交大資工 蔡文能 計概 Sorting-47

Worst-Case Analysis Binary Search

交大資工 蔡文能 計概 Sorting-48

Big-theta Notation

• Identification of the shape of the graph representing the resources required with respect to the size of the input data– Normally based on the worst-case analysis– Insertion sort: (n2)– Binary search: (log n)

交大資工 蔡文能 計概 Sorting-49

Formal Definition

(n2): complexity is kn2+o(n2 )– f(n)/n2 k, n

• o(n2 ): functions grow slower than n2

– f(n)/n2 0, n

交大資工 蔡文能 計概 Sorting-50

Thank You!Thank You!

謝謝捧場tsaiwn@csie.nctu.edu.tw

蔡文能

交大資工 蔡文能 計概 Sorting-51

交大資工 蔡文能 計概 Sorting-52

top related