Sorting1

17
SORTING

Transcript of Sorting1

SORTING

TYPES:•Quick Sort

• Insertion Sort

• Shell Sort

•Merge Sort

INSERTION SORT

INSERTION SORT:

• One of the simplest algorithm.• Requires (n-1) steps…’n’-no. of

elements.• Data movement takes place

with the help of ’tmp’ and not by swapping.

SHELL SORT

SHELL SORT:• Named after Donald shell.

• It compares the elements that are at distance.Shell sort is sometimes referred to as ”diminishing increment” sort.

• The sorting number that is chosen must always be an odd number.

Original 81 94 11 96 12 35 17 95 28 58 41 75 15

After5-sort

35 17 11 28 12 41 75 15 96 58 81 94 95

After3-sort 28 12 11 35 15 41 58 17 94 75 81 96 95

After1-sort 11 12 15 17 28 35 41 58 75 81 94 95 96

MERGE SORT

MERGE SORT:• It is a fine example of a recursive

algorithm.

• Makes use of 2 input array that are sorted among them.

• Almost (n-1) comparisons.

• Divide and conquer strategy.

1 13 24 26 2 15 27 38

1

1 13 24 26 2 15 27 38

1 2

1 13 24 26 2 15 27 38

1 2 13

1 13 24 26 2 15 27 38

1 2 13 15

1 13 24 26 2 15 27 38

1 2 13 15 24

1 13 24 26 2 15 27 38

1 2 13 15 24 26

1 13 24 26 2 15 27 38

1 2 13 15 24 26 27

1 13 24 26 2 15 27 38

1 2 13 15 24 26 27 38

QUICK SORT

8 1 5 14 4 15 12 6 2 11 7 10

6 1 5 7 4 2 8 14 15 11 1 0 12

1 4 2 5 6 7 8 1211 10 14 15

21 4 5 6 1187 15141210

1 2 4 5 6 7 8 10 11 12 14 15

8