stia2024_sortiing

Upload: amimul-ihsan

Post on 03-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 STIA2024_Sortiing

    1/88

    Data Structures & Algorithm

    Analysis

    Week 11 & 12: Sorting

  • 7/28/2019 STIA2024_Sortiing

    2/88

    Objectives:

    At the end of this lesson, the student will be able to: sort an array into ascending order by using the following

    methods: selection sort, insertion sort, shell sort, merge

    sort, quick sort and heap sort,

    sort a chain of nodes into ascending order by using aninsertion sort, and

    determine the efficiency of a sort and comparing the

    algorithms of sorting.

  • 7/28/2019 STIA2024_Sortiing

    3/88

    Introduction

    Sorting rearranging data in an array or list sothat it is in ascending or descending order.

    Sorting can be done to any collection of itemsthat can be compared with one another.

    Eg: arrange a row of books on bookshelf bytitle.

    The efficiency of a sorting algorithm issignificant, particularly when large amounts of

    data are involved.

  • 7/28/2019 STIA2024_Sortiing

    4/88

    INSERTION SORT

  • 7/28/2019 STIA2024_Sortiing

    5/88

    Insertion Sort

    If first two books are out of order Remove second book

    Slide first book to right

    Insert removed book into first slot

    Then look at third book, if it is out of order Remove that book

    Slide 2nd book to right

    Insert removed book into 2nd slot

    Recheck first two books again Etc.

  • 7/28/2019 STIA2024_Sortiing

    6/88

    Insertion Sort

    The placement of the

    third book during aninsertion sort.

  • 7/28/2019 STIA2024_Sortiing

    7/88

    Insertion Sort

    An insertion sort of books

  • 7/28/2019 STIA2024_Sortiing

    8/88

    Insertion Sort

    Sort an array partitions that is, divides the array intotwo parts. One part is sorted and initially contains just first element in the

    array

    The second part contains the remaining elements.

    The algorithm removes the first element from theunsorted part and inserts it into its proper sorted positionwithin the sorted part.

    An insertion sort is more efficient when an array is sorted.

  • 7/28/2019 STIA2024_Sortiing

    9/88

    public class Sort

    {

    public static void main(String [] args)

    {

    int [] tsnom = {3, 1, 2, 7, 5};

    System.out.println(Before Insertion Sort");

    print(tsnom);insertionSort(tsnom);

    System.out.println(After Insertion Sort");

    print(tsnom);

    }

    public static void insertionSort (int[] ts)

    {

    for (int x=1; x < ts.length; x++){

    int temp=ts[x];

    int y=x;

    for (; y>0 && temp < ts[y-1];y--)

    ts[y]=ts[y-1];

    ts[y]=temp;

    }

    }public static void print(int[] ts)

    {

    for(int x=0; x< ts.length; x++)

    System.out.print(ts[x]);

    System.out.println();

    }

    }

    Java Code forInsertion Sort

  • 7/28/2019 STIA2024_Sortiing

    10/88

    Insertion Sort

    3 1 2 7 5

    [0] [1] [2] [3] [4]

    3 1 2 7 5

    [0] [1] [2] [3] [4]X=1temp=11 < 3

    1 3 2 7 5

    [0] [1] [2] [3] [4]

    X=2temp=22 < 32 < 1

    temp=77 < 3

    temp=55 < 75 < 3

    1 3 2 7 5

    [0] [1] [2] [3] [4]

    1 3 3 7 5

    [0] [1] [2] [3] [4]

    1 2 3 7 5

    [0] [1] [2] [3] [4]

    1 2 3 7 5

    [0] [1] [2] [3] [4]

    X=3

    1 2 3 7 5

    [0] [1] [2] [3] [4]

    1 2 3 7 5

    [0] [1] [2] [3] [4]

    X=4

    1 2 3 5 7

    [0] [1] [2] [3] [4]

    1 2 3 5 7

    [0] [1] [2] [3] [4]

    int temp=ts[x];

    int y=x;

    for (; y>0 && temp < ts[y-1];y--)

    ts[y]=ts[y-1];

    ts[y]=temp

    x=1y=1y=0

    X=2y=2y=1

    x=3y=3

    x=4y=4y=3

  • 7/28/2019 STIA2024_Sortiing

    11/88

    Insertion Sort

    3 1 2 7 5

    [0] [1] [2] [3] [4]

    int temp=ts[x];

    int y=x;

    for (; y>0 && temp < ts[y-1];y--)

    ts[y]=ts[y-1];

    ts[y]=temp

    X=1temp=11 < 3

    1 3 2 7 5

    [0] [1] [2] [3] [4]

    1 3 2 7 5

    [0] [1] [2] [3] [4]

    x=2temp=22 < 32 < 1

    temp=2

    1 2 3 7 5

    [0] [1] [2] [3] [4]

    1 2 3 7 5

    [0] [1] [2] [3] [4]

    x=3temp=77 < 3

    temp=7

    1 2 3 7 5[0] [1] [2] [3] [4]

    x=4temp=55 < 7

    5 < 3

    temp=5

    1 2 3 5 7

    [0] [1] [2] [3] [4]

  • 7/28/2019 STIA2024_Sortiing

    12/88

    Insertion Sort of Chain of Linked Nodes

    A chain of integers sorted into ascending order

  • 7/28/2019 STIA2024_Sortiing

    13/88

    Insertion Sort of Chain of Linked Nodes

    During the traversal of a chain to locate the

    insertion point, save a reference to the node

    before the current one.

  • 7/28/2019 STIA2024_Sortiing

    14/88

    Insertion Sort of Chain of Linked Nodes

    Breaking a chain of nodes into two pieces as

    the first step in an insertion sort:

    (a) the original chain; (b) the two pieces

    Efficiency of

    insertion sort of

    a chain is O(n2)

  • 7/28/2019 STIA2024_Sortiing

    15/88

    Efficiency of Insertion Sort

    Best time efficiency is O(n) Worst time efficiency is O(n2)

    Average time efficiency is O(n2)

    If array is closer to sorted order

    Less work the insertion sort does

    More efficient the sort is

    Insertion sort is acceptable for small array sizes

  • 7/28/2019 STIA2024_Sortiing

    16/88

    SELECTION SORT

  • 7/28/2019 STIA2024_Sortiing

    17/88

    Selection Sort

    Rearranges the object by interchanging value.

    The sort locates the smallest/largest value in the array.

    Task: rearrange books on shelf by height

    Shortest book on the left

    Approach: Look at books, select shortest book

    Swap with first book

    Look at remaining books, select shortest

    Swap with second book

    Repeat

  • 7/28/2019 STIA2024_Sortiing

    18/88

    Selection Sort

    Before and after exchanging shortest book and

    the first book.

  • 7/28/2019 STIA2024_Sortiing

    19/88

    Selection Sort

    A selection sort of an array

    of integers into ascending

    order.

    (method 1:choose the smallest value)

  • 7/28/2019 STIA2024_Sortiing

    20/88

    Before Sort

    12

    19

    33

    26

    22

    12

    19

    33

    26

    22

    L1

    12

    19

    22

    26

    33

    L2

    12

    19

    22

    26

    33

    L3 L4

    12

    19

    22

    26

    33

    Max=33 Max=26 Max=22 Max=19

    Sorted List

    12

    19

    22

    26

    33

    A selection sort of an array of

    integers into ascending order.

    (method 2: choose the biggest value)

  • 7/28/2019 STIA2024_Sortiing

    21/88

    Selection Sort

    public void selectionSort(Object array[]) {

    int minIndex;Object temp;

    for(int x = 0; x < array.length; x++) {

    minIndex = 0; //initialization the index of smallest value

    for(int y = x + 1; y < array.length; y++){

    //find the index of the smallest value in the list

    if(array[y] < array[minIndex])minIndex = y;

    }

    //move the smallest value into correct position (swap)

    temp = array[x];

    array[x] = array[minIndex];

    array[minIndex] = temp;}

    }//end selectionSort

  • 7/28/2019 STIA2024_Sortiing

    22/88

    The Efficiency of Selection Sort

    Iterative method for loop executes n 1 times For each of n 1 calls, inner loop executes

    n 2 times

    (n 1) + (n2) + + 1 = n(n 1)/2 = O(n2)

    Recursive selection sort performs same operations Also O(n2)

  • 7/28/2019 STIA2024_Sortiing

    23/88

    Efficiency of Selection Sort

    Best time efficiency is O(n

    2

    ) Worst time efficiency is O(n2)

    Average time efficiency is O(n2)

    Sufficient when to sort a small array.

  • 7/28/2019 STIA2024_Sortiing

    24/88

    SHELL SORT

  • 7/28/2019 STIA2024_Sortiing

    25/88

    Shell Sort

    A variation of the insertion sort But faster than O(n2)

    Done by sorting subarrays of equally spaced indices

    Instead of moving to an adjacent location an element

    moves several locations away Results in an almost sorted array

    This array sorted efficiently with ordinary insertion sort

  • 7/28/2019 STIA2024_Sortiing

    26/88

    Shell Sort

    81 94 11 96 12 35 17 95 28 58 41 75 15List (Before Sort)

    Step 1:

    Divide list distance of 5

    Sort using insertion sort

    81

    35

    41

    94

    17

    75

    11

    95

    15

    96

    28

    12

    58

    35

    41

    81

    17

    75

    94

    11

    15

    95

    28

    96

    12

    58

    Combine the list

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

    horizontal

    vertical

    horizontal

    St 2

  • 7/28/2019 STIA2024_Sortiing

    27/88

    Shell Sort 35

    28

    75

    58

    95

    17

    12

    15

    81

    11

    41

    96

    94

    Step 2:

    Divide listdistance of 3

    Sort using insertion sort

    Combine the list

    28

    35

    58

    75

    95

    12

    15

    17

    81

    11

    41

    94

    96

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

  • 7/28/2019 STIA2024_Sortiing

    28/88

    Shell Sort

    Step 3:

    Divide list distance of1

    Sort using insertion sort

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

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

    Combine the list sorted list

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

  • 7/28/2019 STIA2024_Sortiing

    29/88

    Efficiency of Shell Sort

    Best time efficiency is O(n) Worst time efficiency is O(n1.5)

    Average time efficiency is O(n1.5)

  • 7/28/2019 STIA2024_Sortiing

    30/88

    MERGE SORT

  • 7/28/2019 STIA2024_Sortiing

    31/88

    Merge Sort

    Divide an array into halves

    Sort the two halves

    Merge them into one sorted array

    Referred to as a divide and conquer algorithm

    This is often part of a recursive algorithm However recursion is not a requirement

  • 7/28/2019 STIA2024_Sortiing

    32/88

    Merge Sort

    Algorithm mergeSort(a, first, last)

    // Sorts the array elements a[first] through a[last] recursively.

    if(first < last)

    {

    mid = (first + last)/2

    mergeSort(a, first, mid)

    mergeSort(a, mid+1, last)

    Merge the sorted halves a[first..mid] and a[mid+1..last]

    }

  • 7/28/2019 STIA2024_Sortiing

    33/88

    Merge Sort

    The effect of the recursive calls and the merges

    during a merge sort.

  • 7/28/2019 STIA2024_Sortiing

    34/88

    Merge Sort

    Efficiency of the merge sort

    Merge sort is O(n log n) in all cases

    It's need for a temporary array is a disadvantage

    Merge sort in the Java Class Library

    The class Arrays has sort routines that uses the merge sortfor arrays of objects

    public static void sort(Object[] a);

    public static void sort(Object[] a, int first, int last);

    p blic class MergeSort

  • 7/28/2019 STIA2024_Sortiing

    35/88

    public class MergeSort

    {

    public static void main(String [] args)

    {

    int [] tsnom = {34,8,64,51,32,21,30};

    System.out.println(Before Merge Sort");

    print(tsnom);

    mergeSorting(tsnom);

    System.out.println(After Merge Sort");

    print(tsnom);

    }

    private static void mergeSorting(int []a, int[]tsSem, int left, int right)

    {

    if (left < right)

    {int mid = (left + right)/2;

    mergeSorting(a,tsSem,left,mid); // left side

    mergeSorting(a,tsSem,mid + 1, right); // right side

    merge(a,tsSem,left,mid + 1, right);

    }

    }

    public static void mergeSorting(int []a)

    {

    int [] tsSem=new int[a.length];

    mergeSorting(a,tsSem,0,a.length-1);

    }

    Java Code for Merge

    Sort continued

    next slide

    private static void merge(int [] a int [] tsSem int posleft

  • 7/28/2019 STIA2024_Sortiing

    36/88

    private static void merge(int [] a, int [] tsSem, int posleft,

    int posright, int lastright)

    {

    int lastleft=posright - 1;

    int possem=posleft;

    int bilelemen=lastright - posleft + 1;

    while(posleft

  • 7/28/2019 STIA2024_Sortiing

    37/88

    Merge Sort

    Merge Sort has two level:-

    a) Divide the list into halves

    2 5 1 9 3 8 7

    [0] [1] [2] [3] [4] [5] [6]

    L=0R=6mid=(0+6)/2

    =3

    2 5 1 9

    [0] [1] [2] [3]

    L=0R=3mid=(0+3)/2

    =1

    3 8 7

    [4] [5] [6]

    L=4

    R=6mid=(4+6)/2=5

    2 5

    [0] [1]

    L=0R=1mid=(0+1)/2

    =0

    1 9

    [2] [3]

    L=2R=3mid=(2+3)/2

    =2

    3 8

    [4] [5]

    7

    [6]

    L=4R=5mid=(4+5)/2

    =4

    2

    [0]

    5

    [1]

    1

    [2]

    9

    [3]

    3

    [4]

    8

    [5]

    L=0R=0

    L=1

    R=1

    L=2

    R=2

    L=3

    R=3

    L=4

    R=4

    L=5R=5

    L=6R=6

    First example

    Merge Sort

  • 7/28/2019 STIA2024_Sortiing

    38/88

    Merge Sort2) Merge/combine the list from bottom

    1 2 3 5 7 8 9

    [0] [1] [2] [3] [4] [5] [6]

    1 2 5 9

    [0] [1] [2] [3]3 8 7

    [4] [5] [6]

    2 5[0] [1] 1 9[2] [3]

    3 8

    [4] [5]

    7

    [6]

    2

    [0]

    5

    [1]

    1

    [2]

    9

    [3]

    3

    [4]

    8

    [5]

    First example

    Merge Sort

  • 7/28/2019 STIA2024_Sortiing

    39/88

    34 8 64 51 32 21 30

    [0] [1] [2] [3] [4] [5] [6]

    L=0R=6mid=(0+6)/2

    =3

    34 8 64 51

    [0] [1] [2] [3]

    L=0R=3mid =(0+3)/2

    =1

    32 21 30

    [4] [5] [6]

    L=4

    R=6mid =(4+6)/2

    =5

    34 8

    [0] [1]

    L=0R=1

    mid =(0+1)/2=0

    64 51

    [2] [3]

    Ki=2Ka=3Tgh=(2+3)/2

    =2 32 21

    [4] [5]

    30

    [6]

    Ki=4Ka=5Tgh=(4+5)/2

    =4

    34

    [0]

    8

    [1]

    64

    [2]

    51

    [3]

    32

    [4]

    21

    [5]L=0R=0

    L=1R=1 L=2R=2 L=3R=3

    L=4

    R=4

    L=5

    R=5

    L=6R=6

    Second

    example

    Merge Sort

  • 7/28/2019 STIA2024_Sortiing

    40/88

    8 21 30 32 34 51 64

    [0] [1] [2] [3] [4] [5] [6]

    L=0R=6mid=(0+6)/2

    =3

    8 34 51 64

    [0] [1] [2] [3]

    L=0R=3mid=(0+3)/2

    =1

    21 30 32

    [4] [5] [6]

    L=4R=6mid=(4+6)/2

    =5

    8 34[0] [1]

    L=0R=1

    mid=(0+1)/2=0

    51 64[2] [3]

    Ki=2Ka=3Tgh=(2+3)/2

    =2

    21 32

    [4] [5]

    30

    [6]

    Ki=4Ka=5Tgh=(4+5)/2

    =4

    34

    [0]

    8

    [1]

    64

    [2]

    51

    [3]

    32

    [4]

    21

    [5]L=0R=0

    L=1R=1

    L=2R=2

    L=3R=3

    L=4R=4

    L=5

    R=5

    L=6R=6

    Second

    example

    Merge Sort

  • 7/28/2019 STIA2024_Sortiing

    41/88

    Efficiency of Merge Sort

    Best time efficiency is O(n log n)

    Worst time efficiency is O(n log n)

    Average time efficiency is O(n log n)

  • 7/28/2019 STIA2024_Sortiing

    42/88

    QUICK SORT

  • 7/28/2019 STIA2024_Sortiing

    43/88

    Quick Sort

    Divides the array into two pieces

    Not necessarily halves of the array

    An element of the array is selected as the pivot

    Referred to as a divide and conquer algorithm

    Elements are rearranged so that: Elements in positions before pivot are less than the pivot

    Elements after the pivot are greater than the pivot

  • 7/28/2019 STIA2024_Sortiing

    44/88

    Quick Sort

    A partition of an array during a quick sort

  • 7/28/2019 STIA2024_Sortiing

    45/88

    Quick Sort

    Algor i thmquickSort(a, first, last)

    // Sorts the array elements a[first] through a[last]

    recursively.

    if(first < last)

    { Choose a pivot

    Partition the array about the pivot

    pivotIndex = index of pivot

    quickSort(a, first, pivotIndex-1) // sort Smaller

    quickSort(a, pivotIndex+1, last) // sort Larger}

  • 7/28/2019 STIA2024_Sortiing

    46/88

    Quick Sort

    Quick sort is O(n log n) in the average caseand best case

    O(n2) in the worst case

    Worst case can be avoided by careful choice

    of the pivot

  • 7/28/2019 STIA2024_Sortiing

    47/88

    Quick Sort

    Quick sort rearranges the elements in an array during

    partitioning process

    After each step in the process

    One element (the pivot) is placed in its correct sorted

    position

    The elements in each of the two sub arrays

    Remain in their respective subarrays

    The classArrays in the Java Class Library uses

    quick sort for arrays of primitive types

  • 7/28/2019 STIA2024_Sortiing

    48/88

    Quick Sort has two level:-a) Divide the list into halves (the first number is a pivot)

    5 2 1 9 3 8 7

    2 1 3 9 8 7

    1 3 8 7

    7

    Quick Sort

  • 7/28/2019 STIA2024_Sortiing

    49/88

    b) Merge/combine the list from bottom

    1 2 3 5 7 8 9

    1 2 3 7 8 9

    1 3 7 8

    7

    Quick Sort

  • 7/28/2019 STIA2024_Sortiing

    50/88

    public class QuickSort

    {

    public static void main(String [] args)

    {

    int [] tsnom = {33,17,6,21,56,29};

    System.out.println(Before Quick Sort");

    print(tsnom);

    quickSorting(tsnom,0,tsnom.length-1);

    System.out.println(After Quick Sort");

    print(tsnom);

    }

    private static void quickSorting(int []a, int first, int last){

    int indeksPivot;

    if (first < last)

    {

    indeksPivot=choosePivot(a,first,last);

    quickSorting(a,first,indeksPivot-1);quickSorting(a,indeksPivot+1,last);

    }//if

    }//quickSorting

    Java Code for QuickSort.. Continued

    next slide

    // h Pi t th d f h i th fi t b d t

  • 7/28/2019 STIA2024_Sortiing

    51/88

    //choosePivot method for chosing the first number and sort

    private static int choosePivot(int []a,int first, int last)

    {

    int p=first;

    int pivot=a[first];

    //for sort, left is smaller then pivot and right for greater then pivot

    for (int i=first + 1; i

  • 7/28/2019 STIA2024_Sortiing

    52/88

    Quick Sort

    5 6 7 8 9List of Number

    5 6 7 8 9

    5 6 7 8 9

    5 6 7 8 9

    5 6 7 8 9

    5 6 7 8 9

    Pivot = 5 and the value of pivot

    is smaller than other numbers.

    4 comparisons and 0 changing number

    pivot unknown

    pivot

    pivot

    pivot

    pivot

    unknown

    unknown

    unknown

    After first partition

    Worst case for quick sort

  • 7/28/2019 STIA2024_Sortiing

    53/88

    5 6 7 8 9

    [0] [1] [2] [3] [4]

    < 5

    6 7 8 9

    7 8 9

    8 9

    9

    > 5

    < 6

    < 7

    < 8

    > 6

    > 7

    > 8

    Quick Sort

  • 7/28/2019 STIA2024_Sortiing

    54/88

    Efficiency of Quick Sort

    Best time efficiency is O(n log n)

    Worst time efficiency is O(n2)

    Average time efficiency is O(n log n)

  • 7/28/2019 STIA2024_Sortiing

    55/88

    HEAP SORT

  • 7/28/2019 STIA2024_Sortiing

    56/88

    Reprise: The ADT Heap

    A complete binary tree

    Nodes contain Comparable objects

    In a maxheap

    Object in each node objects in descendants

    Note contrast of uses of the word "heap" The ADT heap

    The heap of the operating system from which memory isallocated when new executes

  • 7/28/2019 STIA2024_Sortiing

    57/88

    Reprise: The ADT Heap

    Interface used for implementation of maxheap

    public interface MaxHeapInterface

    { public void add(Comparable newEntry);

    public Comparable removeMax();

    public Comparable getMax();public boolean isEmpty();

    public int getSize();

    public void clear();

    } // end MaxHeapInterface

  • 7/28/2019 STIA2024_Sortiing

    58/88

    Using an Array to Represent a Heap

    Figure-1: (a) A complete binary tree with its nodes

    numbered in level order; (b) its representation as an array.

  • 7/28/2019 STIA2024_Sortiing

    59/88

    Using an Array to Represent a Heap

    When a binary tree is complete

    Can use level-order traversal to store data in consecutive

    locations of an array

    Enables easy location of the data in a node's parent

    or children

    Parent of a node at iis found at i/2

    (unless i is 1)

    Children of node at ifound at indices

    2iand 2i + 1

  • 7/28/2019 STIA2024_Sortiing

    60/88

    Adding an Entry

    Figure-2: The steps in adding 85 to the

    maxheap of Figure-1(a)

  • 7/28/2019 STIA2024_Sortiing

    61/88

    Adding an Entry

    Begin at next available position for a leaf Follow path from this leaf toward root until find

    correct position for new entry

    As this is done

    Move entries from parent to child

    Makes room for new entry

  • 7/28/2019 STIA2024_Sortiing

    62/88

    Adding an Entry

    Figure-3: A revision of steps shown in

    Figure-2 to avoid swaps.

  • 7/28/2019 STIA2024_Sortiing

    63/88

    Adding an Entry

    Figure-4: An array representation of the

    steps in Figure-3 continued

  • 7/28/2019 STIA2024_Sortiing

    64/88

    Adding an Entry

    An array representation of the steps in

    Figure-3.

  • 7/28/2019 STIA2024_Sortiing

    65/88

    Adding an Entry

    Algorithm for adding new entry to a heap

    Algorithm add(newEntry)

    if(the array heap is full)

    Double the size of the array

    newIndex = index of next available array location

    parentIndex = newIndex/2 // index of parent of available locationwhile (newEntry > heap[parentIndex])

    { heap[newIndex] = heap[parentIndex]

    // move parent to available location

    // update indices

    newIndex = parentIndex

    parentIndex = newIndex/2

    } // end while

  • 7/28/2019 STIA2024_Sortiing

    66/88

    Removing the Root

    Figure-5: The steps to remove the entry in the root of the

    maxheap of Figure-3(d)

  • 7/28/2019 STIA2024_Sortiing

    67/88

    Removing the Root

    Figure-6: The steps that transform a semiheap into

    a heap without swaps.

  • 7/28/2019 STIA2024_Sortiing

    68/88

    Removing the Root

    To remove a heap's root Replace the root with heap's last child

    This forms a semiheap

    Then use the method reheap

    Transforms the semiheap to a heap

  • 7/28/2019 STIA2024_Sortiing

    69/88

    Creating a Heap

    Figure-7: The steps in adding 20, 40, 30,

    10, 90, and 70 to a heap.

  • 7/28/2019 STIA2024_Sortiing

    70/88

    Creating a Heap

    Figure-8: The steps in creating a heap by

    using reheap.

    More efficient to use

    reheapthan to

    use add

  • 7/28/2019 STIA2024_Sortiing

    71/88

    Heapsort

    Possible to use a heap to sort an array Place array items into a maxheap

    Then remove them

    Items will be in descending order

    Place them back into the original array and they will be

    in order

  • 7/28/2019 STIA2024_Sortiing

    72/88

    Heapsort

    Figure-9: A trace of heapsort (a c)

  • 7/28/2019 STIA2024_Sortiing

    73/88

    Heapsort

    Figure-9: A trace of heapsort (d f)

  • 7/28/2019 STIA2024_Sortiing

    74/88

    Heapsort

    Figure-9: A trace of heapsort (g i)

  • 7/28/2019 STIA2024_Sortiing

    75/88

    Heapsort

    Figure-9: A trace of heapsort (j l)

  • 7/28/2019 STIA2024_Sortiing

    76/88

    Heapsort

    Implementation of heapsort

    public static void heapSort(Comparable[] array, int n)

    { // create first heap

    for (int index = n/2; index >= 0; index--)

    reheap(array, index, n-1);

    swap(array, 0, n-1);

    for (int last = n-2; last > 0; last--)

    { reheap(array, 0, last);

    swap(array, 0, last);

    } // end for

    } // end heapSor

    private static void reheap(Comparable[] heap, int first, int last)

    { < Statements from Segment 27.11, replacing rootIndex with first and

    lastIndex with last. >

    } // end reheap

    Efficiency is O(n log n).

    However, quicksort is

    usually a better choice.

    Heap Sort

  • 7/28/2019 STIA2024_Sortiing

    77/88

    Creating a heap

    5

    5 2 1 9 3 2 1

    9 3 8 7

    1

    2 3

    45

    6 7

    8 7

    5

    9

    82

    3 1 7

    1

    2 3

    45

    6 7

    5 2 8 9 3 1 7

    reheap(3)

    reheap(2)

    Heap Sort

  • 7/28/2019 STIA2024_Sortiing

    78/88

    Creating a heap

    5

    2 1

    9

    3

    8

    7

    1

    2 3

    45

    6 7

    5 9 8 2 3 1 7

    5

    2 1

    9

    3

    8

    7

    1

    2 3

    4

    5

    6 7

    9 5 8 2 3 1 7

    reheap(1)

    Heap Sort

  • 7/28/2019 STIA2024_Sortiing

    79/88

    Creating a heap

    5

    2 1

    9

    3

    8

    7

    1

    2 3

    45

    6 7

    9 5 8 2 3 1 7

    5

    2 1

    7

    3

    8

    1

    2 3

    45

    6

    7 5 8 2 3 1 9

    remove

    Heap Sort

  • 7/28/2019 STIA2024_Sortiing

    80/88

    Creating a heap

    5

    2 1

    7

    3

    8

    1

    2 3

    45

    6

    7 5 8 2 3 1 9

    5

    2 1

    7

    3

    81

    2 3

    45

    6

    8 5 7 2 3 1 9

    reheap(1)

    remove

    Heap Sort

  • 7/28/2019 STIA2024_Sortiing

    81/88

    Creating a heap

    5

    2

    1

    7

    3

    1

    2 3

    45

    1 5 7 2 3 8 9

    reheap(1)

    Heap Sort

  • 7/28/2019 STIA2024_Sortiing

    82/88

    Creating a heap

    5

    2

    1

    7

    3

    1

    2 3

    45

    7 5 1 2 3 8 9

    5

    2

    1

    31

    2 3

    4

    3 5 1 2 7 8 9

    remove

    reheap(1)

    Heap Sort

  • 7/28/2019 STIA2024_Sortiing

    83/88

    Creating a heap

    5

    2

    13

    1

    2 3

    4

    5 3 1 2 7 8 9

    2

    13

    1

    2 32 3 1 5 7 8 9

    remove

    reheap(1)

    Heap Sort

  • 7/28/2019 STIA2024_Sortiing

    84/88

    Creating a heap

    3 2 1 5 7 8 9

    1 2 3 5 7 8 9

    2 1

    31

    2 3

    2

    11

    2

    remove

    reheap(1)

    Heap Sort

  • 7/28/2019 STIA2024_Sortiing

    85/88

    Creating a heap

    2

    1

    1

    22 1 3 5 7 8 9

    11

    1 2 3 5 7 8 9

    remove

    remove

    1 2 3 5 7 8 9

    Effi i f H S

  • 7/28/2019 STIA2024_Sortiing

    86/88

    Efficiency of Heap Sort

    Best time efficiency is O(n log n)

    Worst time efficiency is O(n log n)

    Average time efficiency is O(n log n)

  • 7/28/2019 STIA2024_Sortiing

    87/88

    Comparing the AlgorithmsBest Average Worst

    Case Case Case

    Insertion sort O(n) O(n2) O(n2)

    Selection sort O(n2

    ) O(n2

    ) O(n2

    )

    Shell sort O(n) O(n1.5) O(n1.5)

    Merge sort O(n log n) O(n log n) O(n log n)

    Quick sort O(n log n) O(n log n) O(n

    2

    )Heap sort O(n log n) O(n log n) O(n log n)

    The time efficiencies of three sorting algorithms,

    expressed in Big Oh notation.

  • 7/28/2019 STIA2024_Sortiing

    88/88

    Conclusion

    Q & A Session