Title: Sorting
1Sorting
2Sorting
- Consider list x1, x2, x3, xn
- We seek to arrange the elements of the list in
order - Ascending or descending
- Some O(n2) schemes
- easy to understand and implement
- inefficient for large data sets
3Review Selection Sort O(n2)
- Make passes through a list/sublist
- On each pass reposition correctly some element.
E.g., find the smallest item in the sublist and
put it into a right position
4Simple Selection Sort Algorithm
- Array-based
- Can you implement it?
- templatelttypename Tgt
- void select_sort(T a, int n)
- Q Whats the computing time T(n)?
5Simple Selection Sort Algorithm
- template lttypename Tgt
- void select_sort(int a, int n) for (int i0
i lt n-1 i) int min_pos i for (int
j i1 j lt n j) if (amin_pos gt
aj) min_pos j if (min_pos
! i) T temp ai ai amin_pos
amin_pos temp -
-
- T(n) ?
6Simple Selection Sort Algorithm
7Simple Selection Sort Algorithm
- template lttypename Tgtvoid selection_sort(Node
head) while (head) Node ptr
head-gtnext while (ptr) if
(head-gtdata gt ptr-gtdata) T temp
head-gtdata head-gtdata ptr-gtdata
ptr-gtdata temp ptr
ptr-gtnext head head-gtnext -
8Simple Selection Sort Algorithm
- Linked List-based
- Why not move the nodes around?
- If we wanna move the nodes around, what more
should we do? - What difference from array-based?
9Review of Exchange Sort
- Systematically interchange pairs of elements
which are out of order - Bubble sort does this
Out of order, exchange
In order, do not exchange
10Bubble Sort
- void bubble_sort(int a, int n)
- Can you write one in C function?
11Bubble Sort
- void bubble_sort(int a, int n)
- int num_compares n-1 //first should do n-1
comparisons - while (num_compares gt 0)
- int last 0 //why need this?
- for (int i0 iltnum_compares i)
- if (ai gt ai1)
- swap(ai, ai1) last i
- //end if
- num_compares last
- //end while
- // thinking why need last i??? The
purpose of last?
12Bubble Sort
- Disadvantage?
- Swap of data items, but if data item is large,
swap could be very inefficient - Advantage over selection sort?
- It can detect partially sorted sublist.
13Bubble Sort Algorithm
- What is the worst case for Bubble Sort?
The list of items are in decreasing order.
T(n) O(n2)
14Bubble Sort Algorithm
- What is the best case for Bubble Sort?
T(n) O(n)
15Insertion Sort
- Repeatedly insert a new element into an already
sorted list - Note this works well with a linked list
implementation - Why not good for arrays?
- Dynamic sorting algorithm ?
16Example of Insertion Sort
- Given list to be sorted 67, 33, 21, 84, 49, 50,
75 - Note sequence of steps carried out
17Insertion Sort
- Idea
- Two logical sublists one is sorted and the other
is unsorted - Each iteration chooses one item from the unsorted
list and inserts it into the sorted one. - Dynamically expand/shrink the two sublists
18Algorithm for Linear Insertion Sort
- Can you write one in C function?
- void insertion_sort(int a, int n)
19Algorithm for Linear Insertion Sort
- void insertion_sort(int a, int n)
- int temp
- for (int i 1 iltn i)
- temp ai // keep the item to be inserted
- int j
- for (j i-1 j gt 0 j--) //go through the
sorted sublist - if (temp lt aj) aj1 aj //shift
the items - else break
- aj1 temp
- //end for
-
20Algorithm for Linear Insertion Sort
- T(n) ?
- If we use a linked-list instead of an array, how
do we adapt the previous code? - Write a linked-list based one ?
- Do you know the difference?
21Insertion Sort
- Can we write a recursive insertion sort?
- templatelttypename Tgtvoid insert_sort(T a, int
n)
22Can we have better Sorting Algorithms
- We seek improved computing times for sorts of
large data sets, better than O(n2) - Chapter presents schemes (e.g. heapsort) which
can be proven to have average computing
time O( n log2n ) - Must be said, no such thing as a universally good
sorting scheme - Results may depend just how out of order list is
23Comparison of Sorts
- Sort of a randomly generated list of 500 items
(Note times are on 1970s hardware)
24Indirect Sorts
- Possible that the items being sorted are large
structures - Data transfer/swapping time unacceptable
- Alternative is indirect sort
- Uses index table to store positions of the
objects - Manipulate the index table for ordering
- Where will this technique be used? Name one ?
25Question?