Sorting 1 (2)

download Sorting 1 (2)

of 23

Transcript of Sorting 1 (2)

  • 8/3/2019 Sorting 1 (2)

    1/23

  • 8/3/2019 Sorting 1 (2)

    2/23

    PREPARED BYyR.MADHUMITHA

    yG.KARTHIKAyV.LAKSHMI PRIYA

    yA.MOHANA PRIYAyU.GNANAMBIGAI

  • 8/3/2019 Sorting 1 (2)

    3/23

    Why we do sorting?y Commonly encountered programming task in

    computing.

    y

    Examples of sorting:y List containing exam scores sorted from Lowest to

    Highest or from Highest to Lowest

    y List containing words that were misspelled and belisted in alphabetical order.

    y List of student records and sorted by studentnumber or alphabetically by first or last name.

  • 8/3/2019 Sorting 1 (2)

    4/23

    SORTINGy Sorting is any process of arranging items in

    some sequence and/or in different sets, and

    accordingly, it has two common, yet distinctmeanings:

    y ordering: arranging items of the same kind,class, nature, etc. in some ordered sequence,

    y categorizing: grouping and labeling items withsimilar properties together (by sorts).

  • 8/3/2019 Sorting 1 (2)

    5/23

    Sortingy Card players all know how to sort

    y First card is already sortedy With all the rest,

    Scan back from the end until you find the first card larger thanthe new one,

    Move all the lower ones up one slot

    insert it

    Q

    2

    9

    A

    K

    10

    J

    2

    2

    9

  • 8/3/2019 Sorting 1 (2)

    6/23

    Insertion sorty Complexity

    y For each card

    y Scan O(n)

    y Shift up O(n)

    y Insert O(1)

    y Total O(n)y First card requires O(1), second O(2),

    y Forn cards operations O(n2)7 i

    i=1

    n

  • 8/3/2019 Sorting 1 (2)

    7/23

    Sorting - Insertion sorty Complexity

    y For each card

    y Scan O(n)

    y Shift up O(n)

    y Insert O(1)y Total O(n)

    y First card requires O(1), second O(2),

    y For n cards operations O(n2)

    7 i

    i=1

    n

  • 8/3/2019 Sorting 1 (2)

    8/23

    Algorithm :Insertion Sorty void Insertionsort (ElementType A[],int N)y {y

    intj,p;y Element Type Tmp;y for(p=1;p

  • 8/3/2019 Sorting 1 (2)

    9/23

    HEAP SORTy At each level in the binary tree created for

    Merge Sort, there are n elements, with O(1)

    time spent at each elementy O(n) running time for processing one level

    y The height of the tree is O(log n)

    y Therefore, the time complexity is O(nlog n)

  • 8/3/2019 Sorting 1 (2)

    10/23

    What is aheap ?y Definitions of heap:

    1. A large area of memory from which the

    programmer can allocate blocks as needed,and deallocate them (or allow them to begarbage collected) when no longer needed

    2. A balanced, left-justified binary tree inwhich no node has a value greater than thevalue in its parent

    y Heapsort uses the second definition

  • 8/3/2019 Sorting 1 (2)

    11/23

    HEAP PROPERTYy A node has the heap property if the value in the node

    is as large as or larger than the values in its children

    12

    8 3

    Blue node has

    heap property

    12

    8 12

    Blue node has

    heap property

    12

    8 14

    Blue node does not have

    heap property

    All leaf nodes automaticallyhave the heap propertyA binary tree is a heap if all nodes in it have theheap property

  • 8/3/2019 Sorting 1 (2)

    12/23

    SHIFTING UPy Given a node that does not have the heap

    property, you can give it the heap property by

    exchanging its value with the value of thelarger child12

    8 14

    Blue node does not haveheap property

    14

    8 12

    Blue node hasheap property

  • 8/3/2019 Sorting 1 (2)

    13/23

    A SAMPLE HEAPyHeres a sample binary tree after it has been

    heapified

    19

    1418

    22

    321

    14

    119

    15

    25

    1722

  • 8/3/2019 Sorting 1 (2)

    14/23

    yNotice that heapified does not meansorted

    yHeapifying does not change the shape ofthe binary tree; this binary tree isbalanced and left-justified because it

    started out that way

  • 8/3/2019 Sorting 1 (2)

    15/23

    QUICK SORT1) Divide : If the sequence S has 2 or more elements,

    select an element x from S to be your pivot. Anyarbitrary element, like the last, will do. Remove all the

    elements of S and divide them into 3 sequences:L, holds Ss elements less than xE, holds Ss elements equal to xG, holds Ss elements greater than x

    2) Recurse: Recursively sort L and G3) Conquer: Finally, to put elements back into S in order,

    first inserts the elements of L, then those of E, andthose of G.

  • 8/3/2019 Sorting 1 (2)

    16/23

    1)Select: pick an element

    2) Divide: rearrange elements

    so that x goes to its final position E

    3)Recurse and Conquer: recursively

    sort

  • 8/3/2019 Sorting 1 (2)

    17/23

  • 8/3/2019 Sorting 1 (2)

    18/23

    QUICK SORT RUNNING TIMEy Worst case: when the pivot does not divide the sequence in twoy At each step, the length of the sequence is only reduced by 1y Total running time

    y General case:y Time spent at level i in the tree is O(n)

    y Running time: O(n) * O(height)

    y Average case:y O(n log n)

  • 8/3/2019 Sorting 1 (2)

    19/23

    SHELL SORTy Founded by Donald Shell and named the sorting

    algorithm after himself in 1959.y 1st algorithm to break the quadratic time barrier but

    few years later, a sub quadratic time bound wasproven

    y Shellsort works by comparing elements that aredistant rather than adjacent elements in an array orlist where adjacent elements are compared.

    y Shellsort uses a sequence h1, h2, , ht called theincrement sequence. Any increment sequence is fineas long as h1 = 1 and some other choices are betterthan others.

  • 8/3/2019 Sorting 1 (2)

    20/23

    y Shellsort makes multiple passes through a listand sorts a number of equally sized sets using

    the insertion sort.y Shellsort improves on the efficiency of

    insertion sort byquickly shifting values totheir destination.

    y Shellsort is also known as diminishingincrement sort.

  • 8/3/2019 Sorting 1 (2)

    21/23

    Empirical Analysis of Shellsort

    (Advantage)y Advantage of Shellsort is that its only

    efficient for medium size lists. For bigger

    lists, the algorithm is not the best choice.Fastest of all O(N^2) sorting algorithms.

    y 5 times faster than the bubble sort and alittle over twice as fast as the insertion sort,

    its closest competitor.

  • 8/3/2019 Sorting 1 (2)

    22/23

    Empirical Analysis of Shellsort

    (Disadvantage)y Disadvantage of Shellsort is that it is a

    complex algorithm and its not nearly as

    efficient as the merge, heap, and quick sorts.y The shell sort is still significantly slower thanthe merge, heap, and quick sorts, but itsrelatively simple algorithm makes it a goodchoice for sorting lists of less than 5000items unless speed important. It's also anexcellent choice for repetitive sorting ofsmaller lists.

  • 8/3/2019 Sorting 1 (2)

    23/23

    THANK YOU