• No results found

Sorting. Applications of sorting: - searching - list verification

N/A
N/A
Protected

Academic year: 2021

Share "Sorting. Applications of sorting: - searching - list verification"

Copied!
33
0
0

Loading.... (view fulltext now)

Full text

(1)

Chapter 7

Sorting

(2)

Sorting

Applications of sorting:

- searching

- list verification

Given a list of records (R0, R1,…., Rn-1),

- each record, Ri, has a key value Ki

- ordering relation (<) on the keys

Sorting is to find a permutation  such that

K(i-1) <= K(i) , 0 < i <= n-1

(3)

Structure of records

typedef struct element{

int key;

/* other field */

} element;

(4)

Characteristics of sorting

• Internal sort: the list is small enough to sort

entirely in main memory

- bubble sort, insertion sort, quick sort,

heap sort, merge sort

External sort: too much information to fit

into main memory

- the file must be brought into the main

memory in pieces until the entire file is

sorted

(5)

For i=2 to n,

for the sequence of already ordered records,

R

1

, R

2,….,

R

i-1

, insert R

i

so that the resulting

sequence is also ordered

-

n = 5, input sequence (3, 2, 5, 4, 1)

Insertion Sort

i [1] [2] [3] [4] [5] - 2 3 4 5 3 2 2 2 1 2 3 3 3 2 5 5 5 4 3 4 4 4 5 4 1 1 1 1 5

(6)

Insertion Sort

void insertion_sort(element a[ ], int n) {

/* sort a[1:n] into nondecreasing order */

int i, j; element temp; for (i = 2; i <=n; i++) {

temp = a[i];

for (j = i - 1; j >= 1 && temp.key < a[j].key; j--) a[j + 1] = a[j];

a[j + 1] = temp; }

}

(7)

Example

- Worst case behavior of insertion sort n = 5, input sequence: (5, 4, 3, 2, 1)

• Good to use when only a few records are out of order

ex) n = 5, input sequence: (2, 3, 4, 5, 1) • Also good for small lists

(8)

Divide and Conquer

Three steps in Divide and Conquer method

Divide

the problem into a number of subprolems

Conquer

the subproblems. If it is not still difficult,

then solve them recursively

Combine

the solutions to the subproblems into the

solution for the original problem

(9)

Quick Sort

• divide and conquer

• use recursion

• Best, average time - O(n·log

2

n)

• Worst time: O(n

2

)

• Don’t need additional space

X

pivot elements smaller than

or equal to pivot

elements larger than or equal to pivot

(10)

X’ X X : pivot

0 1 n-1

swap

the first element greater than pivot

the first element smaller than pivot

new pivot X’ X · · · X

Quick Sort

sorted

(11)

input file: 10 records

- (26,5,37,1,61,11,59,15,48,19)

Example of Quick Sort

pivot i j

26 5 37 1 61 11 59 15 48 19

26 5 19 1 61 11 59 15 48 37

i j

26 5 19 1 15 11 59 61 48 37

i j

(12)

26 5 19 1 15 11 59 61 48 37

i j

Example of Quick Sort

11 5 19 1 15

26

59 61 48 37

· · ·

(13)

void quicksort(element a[ ], int left, int right) { int pivot, i, j; element temp; if (left < right) { i = left; j = right + 1; pivot = a[left].key; do { : } while (i < j);

swap(a[left], a[j], temp); quicksort(a, left, j-1); quicksort(a, j+1, right); }

}

(14)

do { do

i++;

while (a[i].key < pivot); do

j--;

while (a[j].key > pivot); if (i < j)

swap(a[i], a[j], temp); } while (i < j);

(15)

R1 26 11 1 1 1 1 1 1 R2 5 5 5 5 5 5 5 5 R3 37 19 11 11 11 11 11 11 R4 1 1 19 19 15 15 15 15 R5 61 15 15 15 19 19 19 19 R6 11 26 26 26 26 26 26 26 R7 59 59 59 59 59 48 37 37 R8 15 61 61 61 61 37 48 48 R9 48 48 48 48 48 59 59 59 R10 19 37 37 37 37 61 61 61 left 1 1 1 4 7 7 10 right 10 5 2 5 10 8 10

input file: 10 records

- (26,5,37,1,61,11,59,15,48,19)

(16)

time complexity

- average case: O(n·log2n) split into “equal size”

T(n): average time to sort n records T(n) <= c·n + 2·T(n/2) <= c·n + 2(c·n/2 + 2·T(n/4)) <= 2·c·n + 4·T(n/4) ··· <= log2n·c·n + n·T(1) = O(n·log2n)

- worst case: O(n2)

Quick Sort

(17)

Merge Sort

Use divide and conquer method

(18)
(19)

divide

divide

divide

combine

(20)

int rmerge(element list[ ], int lower, int upper){

int middle;

if (lower >= upper)

return lower;

else {

middle =(lower+upper)/2;

return listmerge(list, rmerge(list, lower,

middle), rmerge(list, middle+1, upper);

}

}

/* rmerge returns the index of the first

element in the sorted chain */

(21)

Merging two ordered lists

2 5 7 8 1 3 4 6 1 2 5 7 8 3 4 6 1 2 5 7 8 3 4 6 1 2 3 5 7 8 4 6 1 2 3 4 5 7 8 6 1 2 3 4 5 7 8 6 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8

(22)

Merge Sort

Need additional space for the merge step

Complexity: O(nlogn)

If linked list is used for merging,

(23)

Heap Sort

utilize the max heap structure

- implement max heap by using array

time complexity

- average case : O(n·log

2

n)

- worst case : O(n·log

2

n)

(24)

Heap Sort

• Construct a max heap with n records - program 7.12 adjust

* takes a binary tree T whose left and right subtrees satisfy the heap property but

whose root may not

* adjusts T so that the entire binary tree satisfies the heap property

• Exchange the first record in the heap with the last record

• Decrement the heap size and readjust the heap • Repeat n-1 passes

(25)

program 7.12

• adjust the binary tree to establish the heap

- time: O(d) where d: depth of tree

1 48 5 61 15 19 1 48 61 5 15 19 1 48 61 15 5 19

(26)

adjust

void adjust(element a[ ], int root, int n) { int child, rootkey;

element temp = a[root]; rootkey = a[root].key;

child = 2*root; /* left child */ while (child <= n){

if ( (child <n) && (a[child].key < a[child+1].key) ) child++;

if (rootkey > a[child].key) break;

else { a[child/2] = a[child]; child *=2; } }

a[child/2] = temp;

(27)

Heap Sort

void heapsort(element a[ ], int n) {

int i, j;

element temp;

for (i = n / 2; i > 0; i--)

/* initial heap construction */

adjust(a, i, n);

for (i = n - 1; i > 0; i--) { /* heap adjust */

SWAP(a[1], a[i+1], temp);

adjust(a, 1, i);

}

(28)

Complexity of heap sort

for the first for loop : O(n)

Let the height of the tree be k

2

k-1

<= n < 2

k

for the second for loop : O(n·log

2

n)

Worst and average time complexity:

O(n·log

2

n)

           

1 1 1 1 1 1 1 1

2

(

)

2

2

)

(

2

k j j k j j k k i i

k

i

j

n

j

n

O

n

(29)

Example of Heap sorting process

- input list

(26,5,77,1,61,11,59,15,48,19)

array interpreted as a binary tree

48 1 5 61 11 26 59 77 15 19 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 26 5 77 1 61 11 59 15 48 19 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

(30)

Heap Sort

1 48 61 19 11 77 26 59 15 5

(31)

Heap Sort

1 48 61 19 11 77 26 59 15 5 77 61 59 48 19 11 26 15 1 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 5 77 5 1 15 48 19 11 61 26 59 5 77

(32)

Heap Sort

15 48 19 11 59 1 36 5 61 77 15 19 5 11 48 1 26 77 61 59

(33)

Heap Sort

15 19 5 1 26 11 77 61 59 48

References

Related documents

strategies with your IT business model, optimizing the integration of the Oracle E-Business Suite with your current systems and processes.... What

the land. In the 1920s and 1930s, land grabbing was prevalent in agricultural regions in the Philippines. The refusal of tenants to vacate the land could be traced, in some

 The total quantity of PAHs detected in the dry season are higher than those of the rainy season in marshy soil samples collected in Warri city in this study period. 

Building on the marketing analytics in the core part of the program, this course looks at Search Engine Optimization (improving the ranking of web- sites on Google and other

The results of this experiment indicate that reverse flushing of porous pavements with water at relatively low pressure levels should be an effective process for maintaining

Dimitrova R, Gibson J, Turkel C, BONT-A CDH Study Group (2005) Botulinum toxin type A (BOTOX) for the prophylactic treatment of chronic daily headache: a randomized, double-

Finally, we provided a proof-of-concept by combining rep B and placI for expression of GFP gene in Shewanella , and followed by demonstrated expression of Mtr path- way as

In mice subjected to PSNL, repeated once daily intrathecal injection of TAT-GESV (10 nmol i.t.) and MK-801 (5 nmol i.t.) across eight consecutive days elevated (A) mechanical