Documentos de Académico
Documentos de Profesional
Documentos de Cultura
STIA2024
Data Structures & Algorithm
Analysis
1
Chapter Contents
Insertion Sort
Introduction
Insertion Sort of a Chain of Linked Nodes
Java Code for Insertion Sort
The Efficiency of Insertion Sort
Selection Sort
Introduction
The Efficiency of Selection Sort
Shell Sort
Introduction
The Efficiency of Shell Sort
2
Chapter Contents
Merge Sort
- Introduction
- Recursive Merge Sort
- Java Code for Merge Sort
- The Efficiency of Merge Sort
Quick Sort
- Introduction
- Creating the Partition
- Java Code for Quick Sort
- The Efficiency of Quick Sort
3
Chapter Contents
Heap Sort
- Reprise: The ADT Heap
- Using an Array to Represent a Heap
- Adding an Entry
- Removing the Root
- Creating a Heap
- Heap Sort
Comparing the Algorithms
4
Learning Objective
5
Introduction
Sorting – rearranging data in an array or list
so that it is in ascending or descending
order.
Sorting can be done to any collection of
items that can be compared with one
another.
Eg: arrange a row of books on bookshelf by
title.
The efficiency of a sorting algorithm is
significant, particularly when large amounts
of data are involved.
6
INSERTION SORT
7
Insertion Sort
If first two books are out of order
Remove second book
Slide first book to right
Insert removed book into first slot
9
Insertion Sort
10
Insertion Sort
Sort an array partitions – that is, divides the
array into two 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
the unsorted part and inserts it into its proper
sorted position within the sorted part.
An insertion sort is more efficient when an
array is sorted.
11
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++) Java Code for
{
int temp=ts[x];
Insertion Sort
for (int y=x;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();
}
}
12
Insertion Sort
int temp=ts[x];
3 1 2 7 5 int y=x;
[0] [1] [2] [3] [4] for (; y>0 && temp < ts[y-1];y--)
ts[y]=ts[y-1];
ts[y]=temp
x=1 x=3
y=1 1 2 3 7 5 y=3
3 1 2 7 5 y=0 [0] [1] [2] [3] [4]
[0] [1] [2] [3] [4]
X=1 1 2 3 7 5
temp=1 temp=7 [0] [1] [2] [3] [4]
1<3 1 3 2 7 5 X=3 7<3
[0] [1] [2] [3] [4]
1 2 3 7 5 x=4
y=4
X=2 [0] [1] [2] [3] [4]
1 3 2 7 5 y=2
y=3
[0] [1] [2] [3] [4] y=1
temp=5 1 2 3 5 7
1 3 3 7 5 5<7 [0] [1] [2] [3] [4]
X=2 X=4 5<3
temp=2 [0] [1] [2] [3] [4]
2<3
2<1 1 2 3 7 5
1 2 3 5 7
[0] [1] [2] [3] [4]
[0] [1] [2] [3] [4] 13
Insertion Sort
int temp=ts[x];
int y=x;
for (; y>0 && temp < ts[y-1];y--)
ts[y]=ts[y-1];
ts[y]=temp
3 1 2 7 5 1 2 3 7 5
[0] [1] [2] [3] [4] [0] [1] [2] [3] [4]
X=1 1 3 2 7 5
temp=1 temp=7
1<3 [0] [1] [2] [3] [4] x=3
temp=7
7<3
1 3 2 7 5
[0] [1] [2] [3] [4]
1 2 3 7 5
temp=2
[0] [1] [2] [3] [4]
x=2 1 2 3 7 5
temp=2 [0] [1] [2] [3] [4] temp=5
2<3 x=4
2<1 temp=5 1 2 3 5 7
5<7 [0] [1] [2] [3] [4]
5<3
14
Insertion Sort of Chain of Linked Nodes
15
Insertion Sort of Chain of Linked Nodes
Efficiency of
insertion sort of
a chain is O(n2)
Breaking a chain of nodes into two pieces as
the first step in an insertion sort:
(a) the original chain; (b) the two pieces 17
Efficiency of Insertion Sort
19
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 … 20
Selection Sort
22
Before Sort L1 L2 L3 L4 Sorted List
12 12 12 12 12 12
19 19 19 19 19 19
33 33 22 22 22 22
26 26 26 26 26 26
22 22 33 33 33 33
25
Efficiency of Selection Sort
26
SHELL SORT
27
Shell Sort
Step 1:
Divide list – distance of 5 horizontal
81 94 11 96 12
35 17 95 28 58
41 75 15
Sort using insertion sort vertical
35 17 11 28 12
41 75 15 96 58
81 94 95
35 17 11 28 12 41 75 15 96 58 81 94 95
29
Step 2:
Shell Sort Divide list – distance of 3
35 17 11
28 12 41
75 15 96
58 81 94
95
28 12 11
35 15 41
58 17 94
75 81 96
95
Step 3:
Divide list – distance of 1
28 12 11 35 15 41 58 17 94 75 81 96 95
31
Efficiency of Shell Sort
32
MERGE SORT
33
Merge Sort
34
Merge Sort
// Sorts the array elements a[first] through a[last] recursively.
Algorithm mergeSort(a, first, last)
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]
}
35
Merge Sort
37
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)
{ Java Code for Merge
int [] tsSem=new int[a.length]; Sort …… continued
mergeSorting(a,tsSem,0,a.length-1);
} next slide
38
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;
} // class MergeSort
39
Merge Sort
Merge Sort has two level:-
a) Divide the list into halves
L=0
R=6 2 5 1 9 3 8 7
mid=(0+6)/2 [0] [1] [2] [3] [4] [5] [6]
=3 L=4
R=6
L=0 mid=(4+6)/2
R=3 =5
mid=(0+3)/2
2 5 1 9 3 8 7
[0] [1] [2] [3] L=2 L=4 [4] [5] [6]
=1
R=3 R=5
mid=(2+3)/2 mid=(4+5)/2
L=0 =2 =4
R=1
3 8 7
mid=(0+1)/2 2 5 1 9 [6]
=0 [0] [1] [2] [3] [4] [5]
L=6
R=6
8
5 1 9 3 [5]
2 [4]
[0] [1] [2] [3]
L=5
L=4
L=0 L=1 L=2 L=3
R=4
R=5 First example
R=1 R=2 R=3
R=0 40
Merge Sort
1 2 3 5 7 8 9
[0] [1] [2] [3] [4] [5] [6]
1 2 5 9 3 8 7
[0] [1] [2] [3] [4] [5] [6]
3 8 7
2 5 1 9 [6]
[0] [1] [2] [3] [4] [5]
8
5 1 9 3 [5]
2 [4]
[0] [1] [2] [3]
First example
41
Merge Sort
L=0
R=6 34 8 64 51 32 21 30
mid=(0+6)/2 [0] [1] [2] [3] [4] [5] [6]
=3
L=4
R=6
L=0 mid =(4+6)/2
R=3 =5
mid =(0+3)/2
34 8 64 51 32 21 30
[0] [1] [2] [3] Ki=4 [4] [5] [6]
=1
Ki=2 Ka=5
Ka=3 Tgh=(4+5)/2
L=0 Tgh=(2+3)/2 =4
R=1 =2 32 21 30
mid =(0+1)/2
34 8 64 51 [6]
[0] [1] [2] [3] [4] [5]
=0
L=6
R=6
21
L=0 8 64 51 32 [5]
R=0 34 [4]
[0] [1] [2] [3]
L=5 Second
L=4
L=1 L=2 L=3 R=5
R=1 R=2 R=3 R=4 example42
Merge Sort
L=0
R=6
mid=(0+6)/2
8 21 30 32 34 51 64
=3 [0] [1] [2] [3] [4] [5] [6]
L=4
R=6
L=0 mid=(4+6)/2
R=3 =5
mid=(0+3)/2
8 34 51 64 21 30 32
[0] [1] [2] [3] Ki=4 [4] [5] [6]
=1 Ki=2
Ka=5
Ka=3
Tgh=(2+3)/2 Tgh=(4+5)/2
L=0 =4
=2
R=1 21 32 30
mid=(0+1)/2
8 34 51 64 [6]
[0] [1] [2] [3] [4] [5]
=0
L=6
R=6
21
L=0 8 64 51 32 [5]
R=0 34 [4]
[0] [1] [2] [3] Second
L=5
L=4 example
L=1 L=2 L=3 R=5
R=4 43
R=1 R=2 R=3
Efficiency of Merge Sort
44
QUICK SORT
45
Quick Sort
47
Quick Sort
48
Quick Sort
49
Quick Sort
2 1 3 9 8 7
1 3 8 7
51
Quick Sort
Quick Sort
b) Merge/combine the list from bottom
1 2 3 5 7 8 9
1 2 3 7 8 9
1 3 7 8
52
public class QuickSort
{
public static void main(String [] args)
{
int [] tsnom = {33,17,6,21,56,29};
53
//choosePivot method for chosing the first number and sort
//for sort, left is smaller then pivot and right for greater then pivot
for (int i=first + 1; i <=last; i++)
{
if (a[i] < pivot)
{
a[p]=a[i];
a[i]=a[p+1];
a[p+1]=pivot;
p++;
}
}
return p;
}//choosePivot
Java Code for
public static void print(int[]a) Quick Sort
{
for (int i=0;i<a.length;i++)
System.out.print("\t"+ a[i]);
System.out.println("\n");
}//print
}
54
Quick Sort
5 6 7 8 9
List of Number
pivot unknown
5 6 7 8 9
pivot unknown
5 6 7 8 9
pivot unknown
5 6 7 8 9
pivot
After first partition 5 6 7 8 9
5 6 7 8 9
[0] [1] [2] [3] [4]
<5 >5
6 7 8 9
<6 >6
7 8 9
<7 >7
8 9
<8 >8
56
Efficiency of Quick Sort
57
HEAP SORT
58
Reprise: The ADT Heap
60
Using an Array to Represent a Heap
64
Adding an Entry
71
Creating a Heap
74
Heapsort
75
Heapsort
77
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--) Efficiency is O(n log n).
{ reheap(array, 0, last); However, quicksort is
swap(array, 0, last); usually a better choice.
} // end for
} // end heapSort
1 5
5 2 1 9 3 8 7 2 2 3 1
4 6 7
9 5 3 8 7
reheap(2)
1 5
2 9 3 1
5 9 1 2 3 8 7
4 6 7
2 5 3 8 7
reheap(3) 80
Heap Sort
• Creating a heap
1 5
2 9 3 8
5 9 8 2 3 1 7
4 6 7
2 5 3 1 7
reheap(1)
1 9
2 5 3 8
9 5 8 2 3 1 7
4 6 7
2 5 3 1 7 81
Heap Sort
• Creating a heap
1 9
2 5 3 8
9 5 8 2 3 1 7
4 6 7
2 5 3 1 7
remove
1 7
2 5 3 8
7 5 8 2 3 1 9
4 6
2 5 3 1
82
Heap Sort
• Creating a heap
1 7
2 5 3 8
7 5 8 2 3 1 9
4 6
2 5 3 1
reheap(1)
1 8
2 5 3 7
8 5 7 2 3 1 9
4 6
2 5 3 1
remove
83
Heap Sort
Creating a heap
1 1
2 5 3 7
1 5 7 2 3 8 9
4
2 5 3
reheap(1)
84
Heap Sort
• Creating a heap
1 7
2 5 3 1
7 5 1 2 3 8 9
4
2 5 3
remove
1 3
2 5 3 1
3 5 1 2 7 8 9
4
2
reheap(1) 85
Heap Sort
• Creating a heap
1 5
2 3 3 1
5 3 1 2 7 8 9
4
2
remove
1 2
2 3 3 1
2 3 1 5 7 8 9
reheap(1)
86
Heap Sort
• Creating a heap
1 3
2 2 3 1
3 2 1 5 7 8 9
remove
1 1
2 2
1 2 3 5 7 8 9
reheap(1)
87
Heap Sort
• Creating a heap
1
2
2 1
2 1 3 5 7 8 9
remove
1
1
1 2 3 5 7 8 9
remove
1 2 3 5 7 8 9
88
Efficiency of Heap Sort
89
Comparing the Algorithms
Best Average Worst
Case Case Case
91