You are on page 1of 102

1

Chapter 7 Sorting
2

OBJECTIVE
• Introduces:
Sorting Concept
Sorting Types
Sorting Implementation
Techniques
3

CONTENTS
7.1 Introductions
7.2 Sorting Methods
7.2.1 Priority Queue Sorting Method (Selection Sort and
Heap Sort)
7.2.2 Insert and Keep Method (Insertion Sort and Tree Sort)
7.2.3 Divide and Conquer Method (Quick Sort and Merge
Sort)
7.2.4 Diminishing Increment Sort Method (Shell Sort)
4

Introduction
• One of the most common applications in computer
science.
• The process through which data are arranged in
either ascending or descending order according to
their values.
• Sorts are generally classified as either internal or
external:
Internal Sort – all of the data are held in primary
storage during the sorting process.
External sort – uses primary storage for the data
currently being sorted and secondary storage for any
data that will not fit in primary memory.
5

• Factors to consider in choosing sorting


technique:
Number of item and load
Number of characters in item or record
Average case and best case
Average case and worst case
Sorter stabilization especially for sorter
that use two keys
• There are a lot sorting techniques, which can be used,
and every technique can be categorized according to
their method group.
6

Sorting Methods
Priority Queue Sorting Method
• Method :
An array A is divided into sub-array P represents priority
queue and sub-array Q represents output queue.
The idea is to find the largest key among unsorted keys in P
and place it at the end of queue of Q. This will result a
sorted queue Q in increasing order.
• Two sorting techniques, which use this method, are
Selection Sort and Heap Sort.
7

Priority Queue Sorting Method: Selection Sort


 Are among the most intuitive of all sorts.
 Suitable for a small table such as for a few
hundreds item.
 Approach:
Select the smallest item and exchanged with the item at
the beginning of the unsorted list. These steps are then
repeated until we have sorted all of the data.
Copyright ©2012 by
Pearson Education, Inc.
All rights reserved

• Example of sorting books by height


▫ Take all books off shelf
▫ Select shortest , replace on shelf
▫ Continue until all books
• Alternative
▫ Look down shelf, select shortest
▫ Swap first with selected shortest
Before and after exchanging the
▫ Move to second slot, repeat process shortest book and the first book
9

• Example :
27 80 02 46 16 12 50
Path:

• Brief Analysis:
~ The inner loop executes 1 + 2 + ... + N – 1 times
or N-1 + N-2 + N-3 + …+ 3 + 2 + 1 which is equal to N(N – 1)/2 ~ O(N2)
~ The sorted input still need N(N-1)/2 comparisons, so it is not good and wasting
time.
A selection sort of an array of integers
into ascending order

Copyright ©2012 by Pearson Education, Inc. All rights reserved


11

Algorithm: Selection Sort


For i = 0 ; i< n-1; i++ do the following:
a. Set smallPos = i Set the i th element to be
b. Set smallest = x[smallPos] compared in next iteration
c. For j= i+1; j < n; j++ do the following
If x[j] < smallest; //smaller element found
i. Set smallPos = j
If the j th smallest element
ii. Set smallest = x[smallPos] then smallest -> x[j]
d. Set x[smallPos] = x[i]
Interchange x[j] and x[i]
e. Set x[i] = smallest.

Other example of Selection Sort algorithm:


http://www.courses.cs.vt.edu/csonline/Algorithms/Lessons/SelectionCardSort/selection
12

Exercise
Use selection sort technique for the following keys:
27 80 02 46 16 12 54
Show the elements in the array for each iteration

Path 1: 02 80 27 46 16 12 54
Find the smallest in unsorted array and swap
Path 2: 02 80 27 46 16 12 54
Path 3: 02 12 27 46 16 80 54
Path 4: 02 12 16 46 27 80 54
Path 5: 02 12 16 27 46 80 54
Path 6: 02 12 16 27 46 54 80
13

Path 1: 02 80 27 46 16 12 54
Find the smallest in unsorted array and
swap
Path 2: 02 80 27 46 16 12 54
Path 3: 02 12 27 46 16 80 54
Path 4: 02 12 16 46 27 80 54
Path 5: 02 12 16 27 46 80 54
Path 6: 02 12 16 27 46 54 80
14

• Try this:
An array contains the elements shown below. The
first two elements have been sorted using a straight
selection sort. What would be the value of the
elements in the array after three more path/passes of
the selection sort algorithm?

7 8 26 44 13 23 98 57
15

Priority Queue Sorting Method: Heap Sort


• An improved version of the selection sort with computing
time O(N log2 N) but not as quick as quick sort in average
cases.
• Heap sort is not using recursive so it saves memory space.
• A Heap is a binary tree which almost complete that is each
level of the tree is completely filled, except possibly the
bottom level, and in this level, the nodes are in the leftmost
positions.
• Also, it satisfies the heap-order property: The data item
stored in each node is greater than or equal to
the data items stored in its children. Thus
the root is the biggest value.
16

Height log2 n

heap-size n

Fig. 1: Heap representations


17

• A node may have one, or two or none of children. If


it has one, the child must be on the left because heap
is not BST.
• The children of the node I is at 2I+1 and 2I+2.
Node(i) => left_Child(i) = 2·i + 1
right_Child(i) = 2·i + 2
• There are two processes to build heap sort: -
BuildHeap
InsertHeap
18

• Example :

1. Process I: Build heap


insert 19

Insert 02
(heap? yes) 19

19

02
19

Insert 46
(heap? no => re-heap) 46
19

02 46 02 19

Insert 16 46
46
(heap? no => re-heap)
02 19
16 19

16
02
20

46 Insert 54
Insert 12 46
(heap? no => re-heap)
(heap? Yes)
16 19
16 19

02 12
02 12 54

Insert 64
64
54
(heap? no => re-heap)
16 46 16 54

02 02 12 19 46
12 19 64
21

Insert 22
(heap? no => re-heap)

64 64

16 54 22 54

02 12 19 46 16 12 19 46

22 02
22

Insert 17
(heap? no => re-heap)
64 64

22 54 22 54

16 12 19 46 17 12 19 46

02 17 02 16
23

Insert 66
(heap? no => re-heap)

64 66

22 54 64 54

17 12 19 46 17 22 19 46

02 16 66 02 16 12
24

Insert 37
(heap? no => re-heap)

66 66

64 54 64 54

17 22 19 46 17 37 19 46

02 16 12 37 02 16 12 22
25

Insert 35
(heap? no => re-heap)
66
66

64 54
64 54

17 37 35 46
17 37 19 46

02 16 12 22 19
02 16 12 22 35

In array format :
26

• Process II: Change to sorted array

66

64 54

17 37 35 46

02 16 12 22 19
27

exchanging the root element and the rightmost leaf element


66
Before

64 54

17 37 35 46
After
02 16 12 22 19
19

64 54

17 37 35 46

02 16 12 22 66
28

exchanging the root with the largest child

before 19

then re-heap if necessary 64 54

17 37 35 46

02 16 12 22 66
after
64

37 54

17 22 35 46

02 16 12 19 66
29

exchange the root element with the last leaf, which is not,
yet highlighted
64

37 54

17 22 35 46
after

02 16 12 19 66 19

37 54

17 22 35 46

02 16 12 64 66
30

 then, do the all three processes until all nodes are sorted.
19

before
then re-heap if necessary 37 54

17 22 35 46

after 02 16 12 64 66
54

37 46

17 22 35 19

02 16 12 64 66
31

exchange the root element and the rightmost leaf element.


54

37 46

17 22 35 19

02 16 12 64 66 12

37 46

17 22 35 19

02 16 54 64 66
32

exchange root with the largest child.


12

37 46 then re-heap if necessary

17 22 35 19

02 16 54 64 66
46

37 35

17 22 12 19

02 16 54 64 66
33

exchange the root element with the last leaf, which is not
yet highlighted.
46

37 35

17 22 12 19

16
02 16 54 64 66

37 35

17 22 12 19

02 46 54 64 66
34

Arrange so that parent > child without considering 46, 54,


64 and 66.

37

22 35

17 16 12 19

02 46 54 64 66
35

exchange the root element and the rightmost leaf element

02

22 35

17 16 12 19

37 46 54 64 66
36

exchange root with the largest child.

35

22 19

17 16 12 02

37 46 54 64 66
37

exchange the root element with the last leaf which is not
yet highlighted

02

22 19

17 16 12 35

37 46 54 64 66
38

Arrange so that parent > child without considering 35, 37,


46, 54, 64 and 66.

22

17 19

02 16 12 35

37 46 54 64 66
39

exchange the root element and the rightmost leaf element.

12

17 19

02 16 22 35

37 46 54 64 66
40

exchange root with the largest child.

19

17 12

02 16 22 35

37 46 54 64 66
41

exchange the root element with the last leaf which is not
yet highlighted.

16

17 12

02 19 22 35

37 46 54 64 66
42

Arrange so that parent > child without considering 19, 22,


35, 37, 46, 54, 64 and 66.

17

16 12

02 19 22 35

37 46 54 64 66
43

exchange the root element and the rightmost leaf element.

02

16 12

17 19 22 35

37 46 54 64 66
44

exchange root with the largest child.

16

02 12

17 19 22 35

37 46 54 64 66
45

exchange the root element with the last leaf which is not
yet highlighted.

12

02 16

17 19 22 35

37 46 54 64 66
46

Arrange so that parent > child without considering 12,


16, 17, 19, 22, 35, 37, 46, 54, 64 and 66.

02

12 16

17 19 22 35

37 46 54 64 66
47

exchange the root element and the rightmost leaf


element.

02

12 16

17 19 22 35

37 46 54 64 66
48

02

12 16

17 19 22 35

37 46 54 64 66

• In an array form:
Sorted!
Algorithm: Heap Sort
1 void sort(int[] a) {
2 for (int i = (a.length-1)/2; i >= 0; i--) build heap, heapify the array a
3 heapify(a, i, a.length); Heap property: ai<=a(i-1)/2 for all i>0
4 for (int j = a.length-1; j > 0; j--) { //n-1
5 swap(a, 0, j);
6 heapify(a, 0, j); root is swapped with last element
re-heap the new root node
7 }
8 }

49
50

The heapify() Method


• Purpose: make the subtree of a starting in node i fulfil the heap
property - The data item stored in each node is greater than or equal
to the data items stored in its children
• Pre-condition: subtrees starting in left_Child(i) and
right_Child(i) must be heaps already

1 void heapify(int[] a, int i, int n) {


2 int ai = a[i];
3 while (i < n/2) { // a[i] is not a leaf
4 int j = 2*i + 1; // a[j] is ai’s left child
5 if (j+1 < n && a[j+1] > a[j]) ++j; // a[j] is ai’s larger child
6 if (a[j] <= ai) break; // a[j] is not out of order
7 a[i] = a[j]; // promote a[j]
8 i = j; // move down to next level
9 a[i] = ai;
10 }
11 }
51

Another example – in an array


Taken from www.cis.upenn.edu/~matuszek/cit594-2008/.../33-heapsort.ppt

25

22 17

19 22 14 15

18 14 21 3 9 11

0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11

Mapping into an array


52

Removing and replacing the root


• The “root” is the first element in the array
• The “rightmost node at the deepest level” is the last
element
• Swap them...
0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11

0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25

• ...And pretend that the last element in the array no longer


exists—that is, the “last index” is 11 (containing the value 9)
53

Reheap and repeat


• Reheap the root node (index 0, containing 11)...
0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25

0 1 2 3 4 5 6 7 8 9 10 11 12
22 22 17 19 21 14 15 18 14 11 3 9 25

0 1 2 3 4 5 6 7 8 9 10 11 12
9 22 17 19 22 14 15 18 14 21 3 22 25

• …and again, remove and place the root node


• Remember, though, that the “last” array index is changed
• Repeat until the last becomes first, and the array is sorted!
54

Exercise
• Build heap sort for the following keys:
5, 3, 1, 9, 8, 2, 4, 7
55

Insert and Keep Method


• Method :
Done by inserting key from an unsorted array A into an
empty holder C, doing the sorting as they are inserted.
Thus, each keys in A that is entered, is placed in its
correct sorted position in C from the start.
• Two sorting techniques that are using this method
are insertion sort and tree sort.
56

Insert and Keep Method: Insertion Sort


• This technique uses less time as opposed to selection sort technique
in comparison-based condition.
• N – 1 path with less data movement.
• Example:
27 80 02 46 16 12 54

Insert 80, compare with 27


Path 1: 27 80 02 46 16 12 54
Insert 02, compare with 27 & 80
Path 2: 02 27 80 46 16 12 54
Insert 46, compare with 02, 27 & 80
Path 3: 02 27 46 80 16 12 54
Insert 16, compare with 02, 27, 80 & 46


Path 4: 02 16 27 46 80 12 54
Path 5: 02 12 16 27 46 80 54
..
Path 6: 02 12 16 27 46 54 80
Copyright ©2012 by
Pearson Education, Inc.
All rights reserved

• When book found taller than one to the right


▫ Remove book to right
▫ Slide taller book to right
▫ Insert shorter book into that spot
• Compare shorter book just moved to left
▫ Make exchange if needed

The placement of the book during an insertion sort


58

Algorithm: Insertion Sort


1 void sort(int[] a) {
2 for (int i = 1; i < a.length; i++) {
3 int temp = a[i], j = i; //hold element ai in a temporary space
4 for (j = i; j > 0 && a[j-1] > temp; j--) //locate the least index j<=i
//for which aj >= ai
5 a[j] = a[j-1]; //shift the subsequence {aj … ai-1} up one position
//into {aj+1 … ai}
6 a[j] = temp; //copy the held value of ai into aj
7 }
8 }this
Try with a[0] a[1] a[2] a[3] a[4]
example: 66 33 99 88 44
Inserting the next unsorted entry into its proper location within the
sorted portion of an array during an insertion sort
Copyright ©2012 by Pearson Education, Inc. All rights reserved
An insertion sort of an array of integers into ascending order

Copyright ©2012 by Pearson Education, Inc. All rights reserved


Inserting the first unsorted
entry into the
sorted portion of the array:

(a) The entry is greater


than
or equal to the last sorted
entry;

(b) the entry is smaller


than the last sorted
entry

Copyright ©2012 by Pearson Education, Inc. All rights reserved


Insertion Sort
• Efficiency
▫ Loop executes at most 1 + 2 + … (n – 1) times

▫ Sum is

▫ Which gives O(n2)


• Best case – array already in order, O(n)

Copyright ©2012 by Pearson Education, Inc. All rights reserved


63

Try this:
An array contains the elements shown below. The first two
elements have been sorted using a straight insertion sort. What
would be the value of the elements in the array after two more
passes of the straight insertion sort algorithm?
3 13 7 26 44 23 98 57
64

Insert and Keep Method: Tree Sort


• If we take a set of key values and insert them into the binary
search tree (BST), an inorder traversal will print out the
nodes in increasing order
▫ This is called treesort
65

Divide And Conquer Method


• Method
The unsorted list A is partitioned into
two sublists, L and R. The two sublists
are then sorted recursively. The sorted
lists then combined into one list in
such a way so that the combined list is
sorted.
Two sorting techniques that used this
method are Quick Sort and Merge
Sort.
66

Divide And Conquer Method: Quick Sort


• Has a good performance in average case and using recursive method.
• Partition (pivot) the list (array-based list) into two sublists.

• Then, partition the sublists into smaller sublists and so on.

• Solve the left hand side first, then the right hand side
• Selection of partition (pivot)
▫ The first element in the list (Hoare, 1962)
▫ The median value of the three elements (left, right, and middle) in the
list (Singleton, 1969)
67

Example Quick Sort : 8, 2, 13, 5, 14, 3, 7

Partition/pivot : first element 8, 2, 13, 5, 14, 3, 7


in the list

8, 2, 13, 5, 14, 3, 7

3, 2, 7, 5 8 14, 13

8, 2, 13, 5, 14, 3, 7 8, 2, 13, 5, 14, 3, 7

3, 2, 7, 5 8 14, 13 3, 2, 7, 5 8 14, 13

2 3 7,5 2 3 7,5
68

8, 2, 13, 5, 14, 3, 7

8, 2, 13, 5, 14, 3, 7
3, 2, 7, 5 8 14, 13

3, 2, 7, 5 8 14, 13
2 3 7,5

2 3 7,5
5 7

5 7

8, 2, 13, 5, 14, 3, 7

3, 2, 7, 5 8 14, 13

2 3 7,5

5 7
69

8, 2, 13, 5, 14, 3, 7
8, 2, 13, 5, 14, 3, 7

3, 2, 7, 5 8 14, 13
3, 2, 7, 5 8 14, 13

2 3 7,5 13 14
2 3 7,5 13 14

5 7
5 7

8, 2, 13, 5, 14, 3, 7

3, 2, 7, 5 8 14, 13

2 3 7,5 13 14 Sorted!

5 7 2, 3, 5, 7, 8, 13, 14
• One entry called the “pivot”
▫ Pivot in position that it will occupy in final sorted
array
▫ Entries in positions before pivot less than or equal to
the pivot
▫ Entries in positions after pivot are greater than or
equal to the pivot

Copyright ©2012 by Pearson Education, Inc. All rights reserved


Partition/pivot : median value of left, right
and middle element in the list

Median-of-three pivot selection: (a) The original array;


(b) the array with its first, middle, and last entries sorted

?
Entries should be <= to the Entries should be >= to the
pivot pivot

The separation is achieved by a rotation of elements that moves the small to the left and the
large to the right.

Copyright ©2012 by Pearson Education, Inc. All rights reserved


72

Algorithm: Quick Sort – first element as pivot/partition

{ap, …, aq-1} size = q - p

1 void sort(int[] a, int p, int q) {


2 // PRECONDITION: 0 <= p < q <= a.length
3 // POSTCONDITION: a[p..q-1] is in ascending order
4 if (q - p < 2) return; // If size of the sequence has fewer than 2 elements, return
5 int j = partition(a, p, q); // partition the subsequence a into three {X, Y, Z} where Y = a p

6 sort(a, p, j); // sort the subsequence X, where elements above ap

7 sort(a, j + 1, q); // sort the subsequence Y, where elements below ap

8}
73

Algorithm: Quick Sort


• The partition() Method {ap, …, aq-1} size = q - p
1 int partition(int[] a, int p, int q) { {ai, …, aj}
2 // RETURNS: index j of pivot element a[j];
3 // POSTCONDITION: a[i] <= a[j] <= a[k] for p <= i <= j <= k < q;
4 int pivot=a[p], i = p, j = q; // assign first element a[p] to pivot
5 while (i < j) {
// decrements j to the index of the next element that is
6 while (j > i && a[--j] >= pivot)
greater than the pivot
7 ; // empty loop
// copies a[j] into last vacant position
8 if (j > i) a[i] = a[j];
9 while (i < j && a[++i] <= pivot) // increments i to the index of the next element that is
less than the pivot
10 ; // empty loop
11 if (i < j) a[j] = a[i]; // copies a[i] into last vacant position

12 }
13 a[j] = pivot;
14 return j;
15 }
The separation is achieved by a rotation of elements that moves the small to the left and the
large to the right. The elements are located using 2 index i and j, initialized to p and q.
74

a[0]=Pivot=5
[0] [1] [2] [3] [4] [5] [6] [7] [8]
5 8 6 4 9 3 7 1 2
a[++i] ≤ pivot i j a[--j] ≥ pivot

1st iteration 2nd iteration 3rd iteration


5 while (i < j) { i=0; a[0]=5 i=1; i=2;
6 while (j > i && a[--j] >= pivot)
j=6; a[6]=7
7 ; // empty loop j=8; a[8]=2 j=7; a[7]=1 j=5; a[5]=3
8 if (j > i) a[i] = a[j]; a[0]=a[8]=2; a[1]=a[7]=1; a[2]=a[5]=3;
9 while (i < j && a[++i] <= pivot)
i=1; a[1]=8 i=2; a[2]=6 i=3; a[3]=4
10 ; // empty loop i=4;a[4]=9
11 if (i < j) a[j] = a[i]; a[8]=a[1]=8 a[7]=a[2]=6 a[5]=a[4]=9
12 }
13 a[j] = pivot; a[j]=a[4]=pivot=5
[0] [1] [2] [3] [4] [5] [6] [7] [8]
2 1 3 4 5 9 7 6 8
75

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


5 8 6 4 9 3 7 1 2
a[0]=Pivot=5
[0] [1] [2] [3] [4] [5] [6] [7] [8]
2 1 3 4 5 9 7 6 8

a[0]=Pivot=2
[0] [1] [2] [3] [4] [5] [6] [7] [8]

1 2 3 4 5 9 7 6 8

a[5]=Pivot=9
[0] [1] [2] [3] [4] [5] [6] [7] [8]

1 2 3 4 5 8 7 6 9

a[5]=Pivot=8
[0] [1] [2] [3] [4] [5] [6] [7] [8]

1 2 3 4 5 6 7 8 9
Algorithm: another example

A partition of an array during a quick sort


Copyright ©2012 by Pearson Education, Inc. All rights reserved
77

Useful links:

- quick sort animation


http://www.cs.auckland.ac.nz/~jmor159/PLDS210/
qsort.html
- example
http://faculty.simpson.edu/lydia.sinapova/www/cmsc
250/LN250_Tremblay/L06-QuickSortEX.htm
78

Exercise
• Based on the following contents of an array
{E, A, F, D, C, B}
▫ Draw diagrams to illustrate quick sorting
technique
▫ Show the contents of the array each time the
quick sort method change it while sorting the
array.
79

Divide And Conquer Method: Merge Sort


• Can be used as both an internal and an external sort; used
to sort large data.
• Method: (as an external sort since it is most often used)
i) Three files needed F1, F2, F3.
F1 and F2 are input data files.
F3 is output data file.
ii) Original data is divided into F1 and F2 alternatively such that the
first element in file F1, second in file F2, third back in file F1 and so
on.
iii) Sorting is done by sorting the first one-element subfile of F1 with
the first one-element subfile of F2 to give a sorted two-element subfile
of F3. After one cycle, repeat the process with size of subfiles is the
power of 2. (2, 4, 8, ....).
80

• Example : 75, 55, 15, 20, 85, 30, 35, 10, 60, 40, 50, 25, 45, 80, 70, 65

F : 75 55 15 20 85 30 35 10 60 40 50 25 45 80 70 65
Original data F1: 75 | 15 |85|35| 60| 50| 45| 70
F2: 55 | 20 |30|10| 40| 25| 80| 65

F3: 55 75 | 15 20 | 30 85 | 10 35 | 40 60 | 25 60 | 45 80 | 65 70

F1: 55 75 | 30 85 | 40 60 | 45 80 |
F2: 15 20 | 10 35 | 25 50 | 65 70 |
F3: 15 20 55 75 | 10 30 35 85 | 25 40 50 60 | 45 65 70 80 |

F1: 15 20 55 75 | 25 40 50 60
F2: 10 30 35 85 | 45 65 70 80

F3: 10 15 20 30 35 55 75 85 | 25 40 45 50 60 65 70 80

F1: 10 15 20 30 35 55 75 85
F2: 25 40 45 50 60 65 70 80
Sorted data
F3: 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85
Merging two sorted arrays into one sorted array
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by
Pearson Education, Inc.
All rights reserved

Merge Sort
• Divide array into two halves
▫ Sort the two halves
▫ Merge them into one sorted array
• Uses strategy of “divide and conquer”
▫ Divide problem up into two or more distinct,
smaller tasks
• Good application for recursion
The major steps in a merge sort

Copyright ©2012 by Pearson Education, Inc. All rights reserved


84

Algorithm: Merge Sort


1 void sort(int[] a, int p, int q) {
2 // PRECONDITION: 0 <= p < q <= a.length
3 // POSTCONDITION: a[p..q-1] is in ascending order
4 if (q-p < 2) return; //less than 2 elements, return
5 int m = (p+q)/2; //m position of the middle element, a[m]
6 sort(a, p, m); //sort the sublist of element that precede a[m]
7 sort(a, m, q); //sort the sublist of element that go after a[m]
8 merge(a, p, m, q); //merge the two sorted sublists
9 }

Steps 6-7 repeatedly divide the array into smaller subarrays until they get
down to size 0 or 1
85

The Merge() Method


1 void merge(int[] a, int p, int m, int q) {
2 // PRECONDITIONS: a[p..m-1] and a[m..q-1] are in ascending order;
3 // POSTCONDITION: a[p..q-1] is in ascending order;
4 if (a[m-1] <= a[m]) return; // a[p..q-1] is already sorted
5 int i = p, j = m, k = 0;
6 int[] aa = new int[q-p]; //a temporary array aa[]
7 while (i < m && j < q)
8 if (a[i]<a[j]) aa[k++] = a[i++];
9 else aa[k++] = a[j++];
10 if (i < m) System.arraycopy(a, i, a, p+k, m-i); // shift a[i..m-1]
11 System.arraycopy(aa, 0, a, p, k); // copy aa[0..k-1] to a[p..p+k-1];
12 }

Try with this a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
example: 66 33 99 55 88 22 44 77
Other example: Merge Sort Algorithm

Copyright ©2012 by Pearson Education, Inc. All rights reserved


87

Exercise
• Show the stages of merge sort for the following
numbers:
13, 57, 39, 85, 99, 70, 22, 48, 64
88

Diminishing Increment Sort Method


• Method :
The array is sorted in smaller groups. By using the
current value that gets larger, the groups will be sorted.
• Technique that used this method is Shell Sort.
89

Diminishing Increment Sort Method: Shell Sort

• Named after its discoverer – Donald L.Shell (1959)


• The technique is to sort items that are further apart
on the first pass, and then to sort items that are
closer and closer together on the later passes by
dividing the items into subfiles.
• Example:
27 80 02 46 16 12 54 64 22 17 66 37 35
90

• Subfiles: (5-increment)

Initial : 27 80 02 46 16 12 54 64 22 17 66 37 35

(27, 12, 66) ^ 12 80 02 46 16 27 54 64 22 17 66


37 35
(80, 54, 37) ^ 12 37 02 46 16 27 54 64 22 17 66
80 35
(02, 64, 35) ^ 12 37 02 46 16 27 54 35 22 17 66
80 64
(46, 22) ^ 12 37 02 22 16 27 54 35 46 17 66
80 64
(16, 17) ^ no changes

12 37 02 22 16 27 54 35 46 17 66 80 64
91

12 37 02 22 16 27 54 35 46 17 66 80 64

• Subfiles: (2-increment)
(12, 02, 16, 54, 46, 66, 64) ^ 02 37 12 22 16 27 46
35 54 17 64 80 66
(37, 22, 27, 35, 17, 80) ^ 02 17 12 22 16 27 46 35
54 37 64 80 66
• Subfiles: (1-increment)
02 17 12 22 16 27 46 35 54 37 64 80 66

02 12 16 17 22 27 35 37 46 54 64 66 80
92

Algorithm
The iSort() Method
1 void iSort(int[] a, int c, int d) {
2 for (int i = c+d; i < a.length; i+=d) {
3 int ai = a[i], j = i;
4 while (j > c && a[j-d] > ai) {
5 a[j] = a[j-d];
6 j -= d;
7 }
8 a[j] = ai;
9 }
10 }
93

Algorithm: Shell Sort


1 void sort(int[] a) {
2 for (int d = a.length/2; d > 0; d /= 2)
3 for (int c = 0; c < d; c++)
4 iSort(a, c, d); // applies insertion sort to the skip sequence
5}
An array and the subarrays formed by grouping entries whose
indices are 6 apart

Copyright ©2012 by Pearson Education, Inc. All rights reserved


The subarrays of Figure 8-12 after each is sorted, and the array
that contains them

Copyright ©2012 by Pearson Education, Inc. All rights reserved


The subarrays of the array of previous figure formed by
grouping entries whose indices are 3 apart

The subarrays of previous figure after each is sorted,


and the array that contains them

…and so on until 1 increment


Copyright ©2012 by Pearson Education, Inc. All rights reserved
97

Exercise
• Show the stages of Shell sort for the following
numbers (starting with 3 increment/apart) :
13, 57, 39, 85, 99, 70, 22, 48, 64
98

Useful link:

http://
www.youtube.com/watch?v=qzXAVXdd
cPU
99

Exercise:
1. Show which of the structures in below figure is a heap and
which is not.

23
23

12 15
12 15

11
(a) 11 (b)
100

23 23

12 15 13 15

13 11 12 11
(c) (d)
101

2. Given the following sequence of numbers, sort the data


element using heap technique.
27 7 92 6 12 14 40
102

Summary
• Sorting is one of the most common data-
processing applications
• Sorting Methods
▫ Priority Queue Sorting Method - Selection Sort and
Heap Sort
▫ Insert and Keep Method - Insertion Sort and Tree Sort
▫ Divide and Conquer Method - Quick Sort and Merge
Sort
▫ Diminishing Increment Sort Method - Shell Sort

You might also like