Data Structure Sorting

64
Data Structures

Transcript of Data Structure Sorting

Page 1: Data Structure Sorting

Data Structures

Page 2: Data Structure Sorting

RecurrencesDivide-and-Conquer Problems

problem

Divide

subproblemsubproblem subproblem

Conquer subproblem subproblem subproblem

Combine problem

Page 3: Data Structure Sorting

RecurrencesDivide-and-Conquer Problems

•Binary Search

• Merge Sort

• Quick Sort

• Selection Sort

• Search Maximum

• Multiplying Large Integers

• Multiplying Chain of Matrices

Page 4: Data Structure Sorting

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

Page 5: Data Structure Sorting

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

Page 6: Data Structure Sorting

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

Page 7: Data Structure Sorting

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 )

Page 8: Data Structure Sorting

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.

Page 9: Data Structure Sorting

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

Page 10: Data Structure Sorting

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

Page 11: Data Structure Sorting

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

Page 12: Data Structure Sorting

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

Page 13: Data Structure Sorting

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

Page 14: Data Structure Sorting

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

Page 15: Data Structure Sorting

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

Page 16: Data Structure Sorting

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

Page 17: Data Structure Sorting

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

Page 18: Data Structure Sorting

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

Page 19: Data Structure Sorting

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

Page 20: Data Structure Sorting

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

Page 21: Data Structure Sorting

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

Page 22: Data Structure Sorting

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

Page 23: Data Structure Sorting

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

Page 24: Data Structure Sorting

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

Page 25: Data Structure Sorting

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

Page 26: Data Structure Sorting

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

Page 27: Data Structure Sorting

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

Page 28: Data Structure Sorting

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

Page 29: Data Structure Sorting

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

Page 30: Data Structure Sorting

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.

Page 31: Data Structure Sorting

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.

Page 32: Data Structure Sorting

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) }

Page 33: Data Structure Sorting

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

Page 34: Data Structure Sorting

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.

Page 35: Data Structure Sorting

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.

Page 36: Data Structure Sorting

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

Page 37: Data Structure Sorting

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).

Page 38: Data Structure Sorting

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

Page 39: Data Structure Sorting

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

Page 40: Data Structure Sorting

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

Page 41: Data Structure Sorting

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

Page 42: Data Structure Sorting

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

Page 43: Data Structure Sorting

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

Page 44: Data Structure Sorting

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

Page 45: Data Structure Sorting

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

} }

Page 46: Data Structure Sorting

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}

Page 47: Data Structure Sorting
Page 48: Data Structure Sorting

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

Page 49: Data Structure Sorting
Page 50: Data Structure Sorting
Page 51: Data Structure Sorting
Page 52: Data Structure Sorting
Page 53: Data Structure Sorting
Page 54: Data Structure Sorting
Page 55: Data Structure Sorting
Page 56: Data Structure Sorting
Page 57: Data Structure Sorting
Page 58: Data Structure Sorting
Page 59: Data Structure Sorting
Page 60: Data Structure Sorting
Page 61: Data Structure Sorting
Page 62: Data Structure Sorting
Page 63: Data Structure Sorting
Page 64: Data Structure Sorting