• Tidak ada hasil yang ditemukan

File Organization and Processing

N/A
N/A
Protected

Academic year: 2024

Membagikan "File Organization and Processing"

Copied!
17
0
0

Teks penuh

(1)

File Organization and Processing

Lecture 9

Divide and Conquer

by: T.Aisha Alasmari

(2)

Topics

– Introduction.

– Divide and Conquer Technique.

– Applications.

– Divide and Conquer Examples.

• Mergesort.

• Quicksort.

• Efficiency.

(3)

Introduction

The most-well known algorithm general design strategy, it works as following:

1. Divide instance of problem into two or more smaller instances, ideally of about the same size.

2. Solving the sub-instances independently.

3. Combining the sub-solutions to obtain the solution of the original instance.

(4)

Divide and Conquer Technique

(5)

Divide and Conquer Technique

• Basic Steps:

Divide: the problem into a number of subproblems that are themselves smaller instances of the same type of problem.

Conquer: Recursively solving these subproblems. If the

subproblems are small enough, solve them straightforward.

Combine: the solutions to the subproblems into the solution of original problem.

(6)

Applications

• Obviously application:

Sort a list of names.

– Organize an MP3 library.

– Display Google PageRank results.

• Non-obvious applications:

– Data compression.

– Computer graphics.

– Computational biology.

– Book recommendations on Amazon.

(7)

Divide and Conquer Examples

1. Sorting: mergesort and quicksort.

2. Binary tree traversals.

3. Binary search.

4. Multiplication of large integers.

5. Matrix multiplication: Strassen’s algorithm.

6. Closest-pair and convex-hull algorithms.

(8)

Mergesort

• Split array A[0..n-1] in two about equal halves and make copies of each half in arrays B and C.

• Sort arrays B and C recursively.

(9)

Mergesort

• Merge sorted arrays B and C into array A as follows:

– Repeat the following until no elements remain in one of the arrays:

• compare the first elements in the remaining unprocessed portions of the arrays

• copy the smaller of the two into A, while incrementing the index indicating the unprocessed portion of that array

– Once all elements in one of the arrays are

processed, copy the remaining unprocessed

elements from the other array into A.

(10)

Mergesort

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

3 8 2 9 1 7 4 5

2 3 8 9 1 4 5 7

1 2 3 4 5 7 8 9

(11)

Quicksort

• Unlike mergesort, which divides its input's

elements according to their position in the array.

• Quicksort divides them according to their value.

• Specifically, it rearranges elements of a given array A[0 .. n - 1] to achieve its partition, a situation where all the elements before some

position s are smaller than or equal to A[s] and all the elements after positions are greater than or

equal to A[s]

(12)

Quicksort

• Obviously, after a partition has been achieved, A[s]

will be in its final position in the sorted array, and we can continue sorting the two subarrays of the

elements preceding and following A[s] independently (e.g., by the same method).

p

A[i]p A[i]p

(13)

Quicksort

• Select an element to divide the subarray, we call this element the pivot.

• There are several different strategies for selecting a

pivot, the simplest strategy is selecting the subarray's

first element p = A[l].

(14)

Quicksort

• Two scans of the subarray each comparing the subarray's elements with the pivot. :

1. left-to-right (i) starts from 2ed one and keep small element to left; stop at first element ≥ pivot.

2. right-to-left (j), starts from last one and keep large element to right; stop at first element ≤ pivot.

(15)

Quicksort

• After both scans stop:

1. i < j (not crossed), exchange A[i] and A[j] and resume the scans by i++ and j--.

2. i ≥ j (crossed or the same element), exchange pivot with A[j] and we will have partitioned the array.

(16)

Quicksort

(17)

Efficiency

• Quicksort makes only 38% more comparisons than in the best case.

• Moreover, it is efficient as it runs faster than

mergesort.

Referensi

Dokumen terkait

hyperbolic until, on the boundary of the tongue, they merge to form a single non-hyperbolic periodic orbit of period r in a (reverse) saddle-node bifurcation (cf. 3a } c): on

The definition Competency Standard, Basic Competency (KD), and Indicator can be explained as follows. Subject Competency Standard is the description of knowledge,

The general methodology used to design a linear array for matrix multiplication given in [19] assuming that we will partition the 2- D array into m-rows is as follows: x The

Merge of GGO and C masks into a single "Lesion" class; both improve the segmentation precision and the accuracy of the classification model built on top of the segmentation model

Instead, the lattices with local domain splitting, as the cases with the type-11 and type-1 1 arrays, show higher deff33than that for the lattice with the type-1 1 array, implying that

CHAPTER 3 HEAPSORT AND ITS OPTIMIZATION WITH THREE CHILD ANALYSIS 3.1 Heapsort Heap sort is a comparison-based sorting algorithm to create a sorted array or list, and is part of the

For the remaining 43 indicators, either population Figure 1: The three elements and 19 domains of the Disability and Wellbeing Monitoring Framework HEALTH Subjective health and

Fanildo Island borders with Palau with Base Point No TD 072 using the Normal baseline However, this provision continues with ’straight baselines will remain effective until there is a