Data Structure Sorting

Post on 14-Apr-2017

37 views 0 download

Transcript of Data Structure Sorting

Data Structures

RecurrencesDivide-and-Conquer Problems

problem

Divide

subproblemsubproblem subproblem

Conquer subproblem subproblem subproblem

Combine problem

RecurrencesDivide-and-Conquer Problems

•Binary Search

• Merge Sort

• Quick Sort

• Selection Sort

• Search Maximum

• Multiplying Large Integers

• Multiplying Chain of Matrices

Divide the problems into a number of sub problems.Conquer the sub problems by solving them recursively. If the sub-problem sizes are small enough, just solve the problems in a straight forward manner.Combine the solutions to the sub problems into the solution for the original problem.

Divide and Conquer Approach

Divide the n element sequence to be sorted into two subsequences of n/2 elements each.Conquer: Sort the two subsequences to produce the sorted answer.Combine: Merge the two sorted sub sequences to produce the sorted answer.

Merge Sort

Merge Sort Base Case: When the sequences to be sorted has length 1.

108 56 1214 89 3466Unsorted

108 56 1466

Divide

10866

Divide

66

Divide

66

BCase

66

Merge

108

Divide

108

BCase

108

Merge

66 108

Merge

56 14

Divide

56Divide

56BCase

56Merge

14

Divide

14

BCase

14

Merge

14 56

Merge

56 66 10814

Merge

1289 34

Divide

1289

Divide

89

Divide

89

BCase

89

Merge12

Divide12

BCase12

Merge

8912

Merge

34

Divide

34

BCase

34

Merge

3412 89

Merge

14 34 8956 66 10812Sorted

Merge Sort AlgorithmMergeSort(A, i, j)

if j > i then

mid ← (i + j)/2

MergeSort(A, i, mid )

MergeSort(A, mid + 1, j )

Merge(A, i, mid, j )

Merge Algorithm

The basic merging algorithms takes

Two input arrays, A[] and B[],

An output array C[]

And three counters aptr, bptr and cptr. (initially set to the beginning of their respective arrays)

The smaller of A[aptr] and B[bptr] is copied to the next entry in C i.e. C[cptr].The appropriate counters are then advanced.When either of input list is exhausted, the remainder of the other list is copied to C.

Merge Algorithm

56 66 10814

A[]

aptr

3412 89

B[]

bptr

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

14 > 12 therefore

C[]

cptr

C[cptr] = 12

12

Merge Algorithm

56 66 10814

A[]

aptr

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

14 > 12 therefore

cptr++

cptr

C[]

bptr

C[cptr] = 12

12

Merge Algorithm

56 66 10814

A[]

aptr

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

14 > 12 therefore

cptr++

cptr

C[]

bptr

bptr++

C[cptr] = 12

12

Merge Algorithm56 66 10814

A[]

aptr

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

14 < 34 therefore

cptr

C[]

bptr

12

C[cptr] = 14

14

Merge Algorithm

56 66 10814

A[]

aptr

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

14 < 34 therefore

cptr++

cptr

C[]

bptr

12

C[cptr] = 14

14

Merge Algorithm56 66 10814

A[]

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

14 < 34 therefore

cptr++

cptr

C[]

bptraptr

aptr++

12

C[cptr] = 14

14

Merge Algorithm56 66 10814

A[]

aptr

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

56 > 34 therefore

cptr

C[]

bptr

12 14

C[cptr] = 34

34

Merge Algorithm56 66 10814

A[]

aptr

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

56 > 34 therefore

cptr++

cptr

C[]

bptr

12

C[cptr] = 34

14 34

Merge Algorithm56 66 10814

A[]

aptr

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

56 > 34 therefore

cptr++

cptr

C[]

bptr

bptr++

12

C[cptr] = 34

14 34

Merge Algorithm56 66 10814

A[]

aptr

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

56 < 89 therefore

cptr

C[]

bptr

12 14 34

C[cptr] = 56

56

Merge Algorithm56 66 10814

A[]

aptr

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

56 < 89 therefore

cptr++

cptr

C[]

bptr

12 14 34

C[cptr] = 56

56

Merge Algorithm56 66 10814

A[]

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

56 < 89 therefore

cptr++

cptr

C[]

bptraptr

aptr++

12 14 34

C[cptr] = 56

56

Merge Algorithm56 66 10814

A[]

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

66 < 89 therefore

cptr

C[]

bptraptr

12 14 34 56

C[cptr] = 66

66

Merge Algorithm56 66 10814

A[]

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

66 < 89 therefore

cptr++

cptr

C[]

bptraptr

12 14 34

C[cptr] = 66

56 66

Merge Algorithm56 66 10814

A[]

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

66 < 89 therefore

cptr++

cptr

C[]

bptraptr

aptr++

12 14 34

C[cptr] = 66

56 66

Merge Algorithm56 66 10814

A[]

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

108 > 89 therefore

cptr

C[]

bptraptr

12 14 34 56 66

C[cptr] = 89

89

Merge Algorithm56 66 10814

A[]

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

108 > 89 therefore

cptr++

cptr

C[]

bptraptr

12 14 34 56 66

C[cptr] = 89

89

Merge Algorithm56 66 10814

A[]

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

108 > 89 therefore

cptr++

cptr

C[]

bptraptr

bptr++

12 14 34 56 66

C[cptr] = 89

89

Merge Algorithm56 66 10814

A[]

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

Array B is now finished, copy remaining elements of array A in array C

cptr

C[]

bptraptr

12 14 34 56 66 89

Merge Algorithm56 66 10814

A[]

3412 89

B[]

If A[aptr] < B[bptr]

C[cptr++] = A[aptr++]

Else

C[cptr++] = B[bptr++]

Array B is now finished, copy remaining elements of array A in array C

cptr

C[]

bptraptr

12 14 34 56 66 89 108

Computation Tree

108 56 1214 89 3466

108 56 1466 1289 34

10866 56 14 1289 34

66 108 56 14 89 12

N = 7

lg 7 = 3

Tree Depth = 3

Problem with Merge Sort

Merging two sorted lists requires linear extra memory.Additional work spent copying to the temporary array and back through out the algorithm.This problem slows down the sort considerably.

Quick Sort

Division of arrays in such a way that

The sorted sub arrays do not need to be later merged.This can be done by rearranging the elements in A(1:n) such that

A(i) <= A(j) for all i between 1 and m and all j between m+1 and n.

Thus the elements in A(1:m) and A(m+1: n) may be independently sorted. No merge is needed.

Quick Sort Algorithm

To sort an array S1. If the number of elements in S is 0 or 1, then return.2. Pick any element V in S. This is called pivot.3. Partition S-{V} (the remaining elements in S) into two

disjoint groups: S1= {x S – {V} | x <= V},

S2 = {x S – {V} | x >= V}4. Return {quickSort(S1) followed by v followed by

quickSort(S2) }

Quick Sort Partitioning

Rearrange the array such thatElement V (pivot) is in its final position None of elements in A[1] .. A[m] is > V None of elements in A[m+1] .. A[n] is < V

pivotelements lower than pivot

elements higherthan pivot

unsorted unsorted

Quick Sort Partitioning

Partition step/strategy is a design decision.

Picking the PivotBest strategy would be to pick the median of the array.Median of three partitioning.

Quick Sort Partitioning

Picking the Pivot:A = 8, 1, 4, 9, 6, 3, 5, 2, 7, 0

Pick three elements randomly and the median of these three numbers is the pivot.

Quick Sort Partitioning

1 4 39 6 58 2 7 0

Left = 8, Right = 0,

Center = (Left + Right)/2 = 4

• 4th elements of the array started with 0 is the pivot => 6

1 4 39 6 58 2 7 0

Quick Sort Partitioning

Replace the pivot with the last element

1 4 39 6 58 2 7 0

1 4 39 0 58 2 7 6

i jTwo indicies, i starts from the first element.

j starts from the second last element

Move all the smaller elements to the left and all the larger elements to the right (small and large is relative to the pivot).

Quick Sort Partitioning

1 4 39 0 58 2 7 6

i j

A[ j ] > pivot therefore decrement j

1 4 39 0 58 2 7 6

i j

A[i] > pivot

Quick Sort Partitioning

Swap A[i] and A[ j ]

1 4 39 0 58 2 7 6

i j

A[ j ] < pivot

1 4 39 0 52 8 7 6

i jA[ i ] < pivot increment i until A[ i ] becomes

greater than pivot

Quick Sort Partitioning

1 4 39 0 52 8 7 6

i j

1 4 39 0 52 8 7 6

i j

i

1 4 39 0 52 8 7 6

jA[ i ] > pivot.A[ j ] > pivot therefore decrement j until A[ j ] < pivot

Quick Sort Partitioning

i

1 4 39 0 52 8 7 6

j

A[ j ] < pivot. Swap A[ i ] and A [ j ]

i

1 4 35 0 92 8 7 6

jA[ i ] < pivot increment i until A[ i ] becomes

greater than pivot

Quick Sort Partitioning

i

1 4 35 0 92 8 7 6

j

i

1 4 35 0 92 8 7 6

j

i

1 4 35 0 92 8 7 6

jA[ i ] > pivot.

A[ j ] > pivot therefore decrement j until A[ j ] < pivot

Quick Sort Partitioning

i

1 4 35 0 92 8 7 6

jA[ j ] < pivot.

i and j crosses each other therefore swap A[ i ] with pivot.

All less then pivot

1 4 35 0 62 8 7 9

All greater then pivot

Quick Sort Partitioning

All less then pivot

Apply same strategy on this sublist separately

1 4 35 0 62 8 7 9

All greater then pivot

Apply same strategy on this sublist separately

Quick Sort Algorithm

QuickSort(A, left, right) {if right > left then {

Pivot = Partition(A, left, right);QuickSort(A, left, pivot-1);QuickSort(A, pivot+1, right);

} }

Partition AlgorithmPartition(a[ ], left, right ) { int i, j; int Pivot = (left+right) / 2; Swap(A[Pivot], A[right]);i = left; j = right - 1; Pivot = right;while ( i < j ) {

while( a[ i ] <= a[Pivot]) i++; while( a[ j ] >= a[Pivot]) j- -; if ( i < j ) SWAP(a[i], a[ j ]);

} Swap(A[ i ], A[Pivot])return i}

Selection SortAlgorithm

To sort an array A[1…n], consisting of n elements, the selection sort procedure is as follows. Scan array A[1..n] to find the minimum element.

Swap minimum element with A[1] Scan sub-array A[2..n] to find the minimum element.Swap minimum element with A[2] Scan sub-array A[3..n] to find the minimum element.

Swap minimum element with A[3]

Scan sub-array A[n-1.n] to find the minimum element. Swap minimum element with A[n-1]

At the end array A[1…n] is sorted