Lecture 12 Sorting Complete
Lecture 12 Sorting Complete
2
Selection sort
To Sort an Array into Ascending Order, First Search for the Largest
Element
Because You Want the Largest Element to be in the Last Position of the
Array, You Swap the Last Item With the Largest Item to be in the Last
Position of the Array, You Swap the Last Item with the Largest Item,
Even if These Items Appear to be Identical
Now, Ignoring the Last (Largest) Item of the Array, search Rest of the
Array For Its Largest Item and Swap it With Its Last Item, Which is the
Next-to-Last Item in the original Array
You Continue Until You Have Selected and Swapped N-1 of the N Items
in the Array
The Remaining Item, Which is Now in the First Position of the Array, is in
its Proper Order
3
Selection sort
Shaded Elements are Selected
Elements in Order are Coloured Red
Initial Array 29 10 14 37 13
After1st Swap 29 10 14 13 37
After2nd Swap 13 10 14 29 37
After3rd Swap 13 10 14 29 37
After4th Swap 10 13 14 29 37
4
Algorithm Analysis
Last= index of the last item of subarray
L= index of largest item found
Swap Operation
swap(a[max_pos],a[last]); Swap operation requires 3
} moves
So this will require 3 *(N-1) 5
6
Insertion sort
Divide Array Into Sorted Section At Front (Initially
Empty), Unsorted Section At End
Step Iteratively Through Array, Moving Each Element
To Proper Place In Sorted Section
Sorted Unsorted
….. …..
0 i N-1
After i Iterations
7
Insertion sort
Initial Array 29 10 14 37 13 Copy 10
29 29 14 37 13 Shift 29
10 29 29 37 13 Shift 29
10 14 29 37 13 Copy 13
8
Implementation
void insertionSort(int numbers[], int N)
{
int i, j, index;
9
Discussion
In worst case of insertion sort requires N*(N-1)/2
comparison and data moves (in the inner loop)
11
Mergesort
The Recursive calls Continue to divide the Array into
Pieces Until Each Piece Contains Only One Item
An Array of One Item is Obviously Sorted
The Algorithm Then Merges These small Pieces Until
One Sorted Array Results
12
Merge sort Example
38 16 27 39 12 27
38 16 27 39 12 27 Recursive
Calls
38 16 27 39 12 27 to
Mergesort
38 16 39 12
16 38 12 39
Merge
16 27 38 12 27 39
Steps
12 16 27 27 38 39
13
Merge the halves
14
Pseudocode of Merge sort
15
Void Merge(Datatype A[], int F, int Mid, Int L)
{
Int First1=F; int Last1= Mid; //F to mid is first subaaray
Int First2=Mid+1; int Last2=L; //Mid +1 to last i second subarray
Datatype TempArray[MAX_size];
For(; (First1<=last1) && (First2<=Last2); index++) // while both subarrays are not empty copy
the { smaller item into the temporary
array
if (A[First1] <A[First2])
{TempArray[index]=A[First1]
++First1;}
Else
{ TempArray[index]=A[First2]
++First2;}
}
For(index=F; Index<=L; ++Index) //Copy the result Back into the original array
A[index]=TempArray[Index];
16
Algorithm analysis
Merge function called once
Merges all N items and require 3* N-1
18
Quicksort
Divide and Conquer algorithm
19
Quicksort
Partition (Divide)
Choose a pivot
Find the position for the pivot so that
all elements to the left are less
all elements to the right are greater
20
Quicksort
Conquer
Apply the same algorithm to each half
21
Partition Algorithm
Must Arrange Items Into Two regions S1, the Set of
Items Less Than the Pivot, and S2, the Set of Items
Greater Than or Equal to Pivot
Different algorithms for Choice of a Pivot
Retain Pivot in A[R] position
The Items That await Placement are in Another Region ,
Called the Unknown Region
S1 S2 Unknown
<P >=P ? P
LastS1 FirstUnknown L R
S1 S2 Unknown
<P >=P ? P
LastS1 FirstUnknown L R
24
Example: Quicksort (Only one step shown)
Pivot
Choose Pivot, keep it in A[R]
38 12 39 27 16 27
Unknown Pivot FirstUnknown = 0(Points to 38
38 Belongs in S2
38 12 39 27 16 27
S2 Unknown Pivot
S1 is Empty
38 12 39 27 16 27 12 Belongs in S1, swap38 & 12
S1 S2 Unknown Pivot
12 38 39 27 16 27 39 Belongs in S2
S1 S2 Unknown Pivot
12 38 39 27 16 27 27 Belongs in S2
S1 S2 UnknownPivot
12 38 39 27 16 27 16 Belongs in S1, Swap 38 & 16
S1 S2 Pivot
12 16 39 27 38 27 No more Unknown
S1 Pivot S2
16 12 27 39 27 38 Place Pivot between S1 and S2
25
Quicksort Code
P: first element
r: last element
Quicksort(A, p, r)
{
if (p < r)
{
q = Partition(A, p, r)
Quicksort(A, p , q-1)
Quicksort(A, q+1 , r)
}
}
30
Comparison of Sorting
Algorithms
Worst Case
Selection Sort N2
Bubble Sort N2
Insertion Sort N2
Mergesort N * log N
Quicksort N2
31
Online References
http://www.bluffton.edu/~nesterd/java/
SortingDemo.html
http://www.youtube.com/watch?v=y_G9BkAm6B8
http://www.sorting-algorithms.com/quick-sort
http://www.cs.auckland.ac.nz/~jmor159/PLDS210/
ds_ToC.html
http://www.youtube.com/watch?v=vxENKlcs2Tw
32