Sorting Linked Lists - PowerPoint PPT Presentation

About This Presentation
Title:

Sorting Linked Lists

Description:

... to identify the telephone customer who is using a particular phone number, as ... The number of comparisons is the same as for the array bubble sort which is N2/2, ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 14
Provided by: richar219
Category:

less

Transcript and Presenter's Notes

Title: Sorting Linked Lists


1
Sorting Linked Lists
  • Introduction
  • Approach 1 recreate the list in the sort order
  • Approach 2 bubble sort
  • Approach 3 sort using linked list/array hybrid

2
Introduction 1
  • We've seen that insert and delete record
    operations maintaining sort order are much faster
    when the data is in the form of a linked list
    compared to an array, because the other nodes do
    not need to be moved to create or fill an array
    gap.
  • But sorting is a fundamental data processing
    operation and sometimes data needs to be in a
    different sort order. E.G. we might need to
    identify the telephone customer who is using a
    particular phone number, as opposed to the usual
    directory sort order.

3
Approach 1 Create a new linked list in the
different sort order.
  • Make dummy node for new list 2. (Advanced
    programmers don't need the dummy node.)?
  • From start of list1 to end of list 1
  • Unlink item from list 1 storing node address
    without freeing node.
  • Insert item into list 2 in required sorted
  • position
  • Free dummy node etc. in list 1.
  • Make list 2 the list.

4
Approach 1 efficiency
  • Assuming sort order 2 is unrelated to sort order
    1, finding the correct insert position in list 2
    requires on average N/4 comparisons for each
    node, because the average second list to be
    searched is half the size of the original, and on
    average the insert position will be found half
    way through the list. The total number of
    comparisons expected is
  • N2/4 . The data is not moved, but addresses
    need to be relinked 2N times.

5
Approach 2 Use Bubble Sort within the original
list
  • Count length of list (N) either at start or
    during first pass
  • DO N-1 passes
  • FOR all node pairs on this pass
  • Note address of node prior to pair of nodes to
    compare
  • IF swap required (i.e. keys are not in required
    sort order)
  • EITHER
  • unlink first node (delete but don't free it)?
  • insert unlinked node after second node.
  • OR
  • Leave chain intact but using a temp node,
  • Swap all data fields between 1st and 2nd nodes.

6
Approach 2 efficiency
  • The number of comparisons is the same as for the
    array bubble sort which is N2/2, because the
    average pass involves comparing half the keys
    with the neighbouring key and there are N passes.
    The amount of data to be moved is the same as
    with array bubble sort only if linkage is
    maintained and temporary data is used for swaps.
    Then 3N2/4 moves are needed because a swap
    involves 3 moves. But if the nodes are relinked
    instead to perform a swap, this is expected to
    occur N2/4 times, requiring movement of N2
    pointers.

7
Approach 3 use a linked list/array hybrid data
structure
8
Approach 3 use a linked list/array hybrid data
structure
  • If the linked list node uses a typedef of NODE,
    the pointer array accessing all nodes can be
    headed at a pointer declared and allocated as
    follows
  • NODE temparray
  • int nnodes
  • ...
  • //Count number of nodes as nnodes
  • temparray(NODE)malloc(sizeof(NODE) nnodes)

9
Approach 3 slide 3
  • For each node in list
  • Store node address in temparray
  • e.g. temparrayiNodeAddress
  • Use any conventional array sort algorithm based
    on temparray.
  • When data no longer needed in sorted order
  • Free temp array

10
Approach 3 slide 4
  • If the linked list itself is to be sorted, rather
    than just accessed through the sorted array, the
    swaps will affect data only, and leave linked
    list node order intact. This is made easier if
    you declare your data record as an embedded
    structure within your linked list structure e.g.
  • typedef struct data
  • char student20 float mark
  • DATA
  • typedef struct node
  • DATA d struct node next
  • NODE

11
Approach 3 slide 5
  • Nesting your DATA type in this way enables you to
    swap the DATA items directly as whole records
    without having to swap many DATA members, while
    leaving list linkage unchanged
  • e.g.
  • DATA temp
  • ...
  • temptemparrayi-gtd
  • temparrayi-gtdtemparrayj-gtd
  • temparrayj-gtdtemp

12
Approach 3 slide 6
  • Alternatively, the linked list can be left as in
    the hybrid diagram, and the pointer array
    elements can be swapped
  • e.g.
  • NODE temp
  • ...
  • temptemparrayi
  • temparrayitemparrayj
  • temparrayjtemp

13
Approach 3 efficiency
  • The advantages of using this hybrid approach is
    that the efficiency is the same as that of the
    array sort we use, and if we don't swap the data
    around in the array we only move the array
    pointers. When we study recursive array sorts we
    will observe much better efficiencies than for
    iterative sorts. To obtain this advantage we need
    to add the N 2 pointer operations needed to
    establish the pointer array.
  • For search efficiency, we are not limited to a
    single array indexing a single key. We can have
    multiple sorted arrays indexing multiple keys,
    enabling us to achieve log2N binary split search
    efficiency using any search key.
Write a Comment
User Comments (0)
About PowerShow.com