Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views181 pages

CSE225Lecture18Sorting Part01

Uploaded by

COC Player
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views181 pages

CSE225Lecture18Sorting Part01

Uploaded by

COC Player
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 181

Lecture 18

Sorting
CSE225: Data Structures and Algorithms
Bubble Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Bubble Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Bubble Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted
Bubble Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted
Bubble Sort

5 1 3 2 4 6

Comparison

Data Movement

Sorted
Bubble Sort

5 1 3 2 4 6

Comparison

Data Movement

Sorted
Bubble Sort

5 1 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

5 1 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

5 1 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 5 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 5 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 5 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 5 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 5 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 5 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 2 5 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 2 5 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 2 5 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 2 5 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 2 5 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 2 3 5 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 2 3 5 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 2 3 5 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 2 3 5 4 6

Comparison

Data Movement

Sorted
Bubble Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted
Bubble Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted
Bubble Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted
Bubble Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted
Bubble Sort
template<class ItemType>
void Swap(ItemType& item1, ItemType& item2)
{
ItemType tempItem;

tempItem = item1;
item1 = item2;
item2 = tempItem;
}
Bubble Sort
template<class ItemType>
void BubbleUp(ItemType values[] , int startIndex, int endIndex)
{
for (int index = endIndex; index > startIndex; index--)
if (values[index-1] > values[index])
Swap(values[index-1], values[index]);
}

template<class ItemType>
void BubbleSort(ItemType values[], int numValues)
{
int current = 0;

while (current < numValues - 1)


{
BubbleUp(values, current, numValues-1);
current++;
}
}
Bubble Sort
template<class ItemType>
void BubbleUp(ItemType values[] , int startIndex, int endIndex)
{
for (int index = endIndex; index > startIndex; index--)
if (values[index-1] > values[index])
Swap(values[index-1], values[index]);
}

template<class ItemType>
void BubbleSort(ItemType values[], int numValues)
{
int current = 0;

while (current < numValues - 1) O(N)


{
BubbleUp(values, current, numValues-1);
current++;
O(N)
}
}
O(N2)
Bubble Sort (little improved)
template<class ItemType>
void BubbleUp2(ItemType values[], int startIndex, int endIndex, bool&
sorted)
{
sorted = true;
for (int index = endIndex; index > startIndex; index--)
if (values[index] < values[index-1])
{
Swap(values[index], values[index-1]);
sorted = false;
}
}
template<class ItemType>
void ShortBubble(ItemType values[], int numValues)
{
int current = 0;
bool sorted = false;
while (current < numValues - 1 && !sorted)
{
BubbleUp2(values, current, numValues-1, sorted);
current++;
}
}
Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort

5 1 3 4 6 2
 
Current Smallest

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2
 
Current Smallest

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort

1 5 3 4 6 2
 
Current Smallest

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5
 
Current Smallest

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Smallest

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Smallest

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Smallest

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Smallest

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 6 5
 
Current Smallest

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 5 6
 
Current Smallest

Comparison

Data Movement

Sorted
Selection Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted
Selection Sort
template<class ItemType>
int getMinIndex(ItemType values[], int startIndex, int endIndex)
{
int indexOfMin = startIndex;
for (int index = startIndex + 1; index <= endIndex; index++)
if (values[index] < values[indexOfMin])
indexOfMin = index;
return indexOfMin;
}
template<class ItemType>
void SelectionSort(ItemType values[], int numValues)
{
int endIndex = numValues-1;
int minIndex;
for (int current = 0; current < endIndex; current++)
{
minIndex = getMinIndex(values, current, endIndex);
Swap(values[current], values[minIndex]);
}
}
Selection Sort
template<class ItemType>
int getMinIndex(ItemType values[], int startIndex, int endIndex)
{
int indexOfMin = startIndex;
for (int index = startIndex + 1; index <= endIndex; index++)
if (values[index] < values[indexOfMin])
indexOfMin = index;
return indexOfMin;
}
template<class ItemType>
void SelectionSort(ItemType values[], int numValues)
{
int endIndex = numValues-1;
int minIndex;
for (int current = 0; current < endIndex; current++)O(N)
{
O(N)
minIndex = getMinIndex(values, current, endIndex);
Swap(values[current], values[minIndex]);
}
}
O(N2)
Insertion Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort

1 5 3 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort

1 3 5 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort

1 3 5 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort

1 3 5 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort

1 3 4 5 6 2

Comparison

Data Movement

Sorted
Insertion Sort

1 3 4 5 6 2

Comparison

Data Movement

Sorted
Insertion Sort

1 3 4 5 6 2

Comparison

Data Movement

Sorted
Insertion Sort

1 3 4 5 6 2

Comparison

Data Movement

Sorted
Insertion Sort

1 3 4 5 6 2

Comparison

Data Movement

Sorted
Insertion Sort

1 3 4 5 2 6

Comparison

Data Movement

Sorted
Insertion Sort

1 3 4 5 2 6

Comparison

Data Movement

Sorted
Insertion Sort

1 3 4 2 5 6

Comparison

Data Movement

Sorted
Insertion Sort

1 3 4 2 5 6

Comparison

Data Movement

Sorted
Insertion Sort

1 3 2 4 5 6

Comparison

Data Movement

Sorted
Insertion Sort

1 3 2 4 5 6

Comparison

Data Movement

Sorted
Insertion Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted
Insertion Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted
Insertion Sort
template<class ItemType>
void InsertItem(ItemType values [], int startIndex, int endIndex)
{
bool finished = false;
int current = endIndex;
bool moreToSearch = (current != startIndex);
while (moreToSearch && !finished)
{
if (values[current] < values[current -1] )
{
Swap(values[current], values[current-1]);
current--;
moreToSearch = (current != startIndex);
}
else finished = true;
}
)
template<class ItemType>
void InsertionSort(ItemType values[], int numValues)
{
for (int count = 0; count < numValues; count++)
InsertItem(values, 0, count);
}
Insertion Sort
template<class ItemType>
void InsertItem(ItemType values [], int startIndex, int endIndex)
{
bool finished = false;
int current = endIndex;
bool moreToSearch = (current != startIndex);
while (moreToSearch && !finished)
{
if (values[current] < values[current -1] )
{
Swap(values[current], values[current-1]);
current--;
moreToSearch = (current != startIndex);
}
else finished = true;
}
)
template<class ItemType>
void InsertionSort(ItemType values[], int numValues)
{
for (int count = 0; count < numValues; count++) 2
InsertItem(values, 0, count); O(N )
}
Merge Sort

6 18 56 62 1 9 15 43
Merge Sort
Unsorted

6 18 56 62 1 9 15 43
Sorted Sorted
Merge Sort

6 18 56 62 1 9 15 43
Merging
Merge Sort

6 18 56 62 1 9 15 43
Merging

Left half

Right half

Minimum between first elements in both halves


Merge Sort

6 18 56 62 1 9 15 43
Merging

1
Left half

Right half

Minimum between first elements in both halves


Merge Sort

6 18 56 62 1 9 15 43
Merging

1
Left half

Right half

Minimum between first elements in both halves


Merge Sort

6 18 56 62 1 9 15 43
Merging

1 6
Left half

Right half

Minimum between first elements in both halves


Merge Sort

6 18 56 62 1 9 15 43
Merging

1 6
Left half

Right half

Minimum between first elements in both halves


Merge Sort

6 18 56 62 1 9 15 43
Merging

1 6 9
Left half

Right half

Minimum between first elements in both halves


Merge Sort

6 18 56 62 1 9 15 43
Merging

1 6 9
Left half

Right half

Minimum between first elements in both halves


Merge Sort

6 18 56 62 1 9 15 43
Merging

1 6 9 15
Left half

Right half

Minimum between first elements in both halves


Merge Sort

6 18 56 62 1 9 15 43
Merging

1 6 9 15
Left half

Right half

Minimum between first elements in both halves


Merge Sort

6 18 56 62 1 9 15 43
Merging

1 6 9 15 18
Left half

Right half

Minimum between first elements in both halves


Merge Sort

6 18 56 62 1 9 15 43
Merging

1 6 9 15 18
Left half

Right half

Minimum between first elements in both halves


Merge Sort

6 18 56 62 1 9 15 43
Merging

1 6 9 15 18 43
Left half

Right half

Minimum between first elements in both halves


Merge Sort

6 18 56 62 1 9 15 43
Merging

1 6 9 15 18 43
Left half

Right half

Minimum between first elements in both halves


Merge Sort

6 18 56 62 1 9 15 43
Merging

1 6 9 15 18 43 56 62
Left half

Right half

Minimum between first elements in both halves


Merge Sort

1 6 9 15 18 43 56 62
Merging

1 6 9 15 18 43 56 62
Left half

Right half

Minimum between first elements in both halves


Merge Sort

1 6 9 15 18 43 56 62
Merge Sort

1 6 9 15 18 43 56 62

O(N)
Merge Sort
• Merging
template<class ItemType>
void Merge(ItemType values[], int leftFirst, int leftLast,
int rightFirst, int rightLast)
{
ItemType tempArray[MAX_ITEMS];
int index = leftFirst, saveFirst = leftFirst;

while ((leftFirst <= leftLast) && (rightFirst <= rightLast))


{
if (values[leftFirst] < values[rightFirst])
{
tempArray[index] = values[leftFirst];
leftFirst++;
}
else
{
tempArray[index] = values[rightFirst];
rightFirst++;
)
index++;
}
Merge Sort
• Merging

while (leftFirst <= leftLast)


{
tempArray[index] = values[leftFirst];
leftFirst++;
index++;
}

while (rightFirst <= rightLast)


{
tempArray[index] = values[rightFirst];
rightFirst++;
index++;
}

for (index = saveFirst; index <= rightLast; index++)


values[index] = tempArray[index];
}
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

O(logN) for dividing


Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 6 32 15 43 1 9 22 26 19 55 37 43 2 99

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

O(N) for merging


Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 6 32 15 43 1 9 22 26 19 55 37 43 2 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 6 32 15 43 1 9 22 26 19 55 37 43 2 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

6 18 26 32 1 9 15 43 19 22 26 55 2 37 43 99

18 26 6 32 15 43 1 9 22 26 19 55 37 43 2 99

O(N) for merging


Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

6 18 26 32 1 9 15 43 19 22 26 55 2 37 43 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

6 18 26 32 1 9 15 43 19 22 26 55 2 37 43 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

1 6 9 15 18 26 32 43 2 19 22 26 37 43 55 99

6 18 26 32 1 9 15 43 19 22 26 55 2 37 43 99

O(N) for merging


Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

1 6 9 15 18 26 32 43 2 19 22 26 37 43 55 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2

1 6 9 15 18 26 32 43 2 19 22 26 37 43 55 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 6 9 15 18 19 22 26 26 32 37 43 43 55 99

1 6 9 15 18 26 32 43 2 19 22 26 37 43 55 99

O(N) for merging


Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 6 9 15 18 19 22 26 26 32 37 43 43 55 99
Merge Sort

template<class ItemType>
void MergeSort(ItemType values[], int first, int last)
{
if (first < last)
{
int middle = (first + last) / 2;
MergeSort(values, first, middle);
MergeSort(values, middle + 1, last);
Merge(values, first, middle, middle + 1, last);
}
}
Merge Sort

template<class ItemType>
void MergeSort(ItemType values[], int first, int last)
{
if (first < last)
{
int middle = (first + last) / 2;
MergeSort(values, first, middle); O(NlogN)
MergeSort(values, middle + 1, last);
Merge(values, first, middle, middle + 1, last);
}
}
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 1 3 2 16 9 10 14 8 7
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 1 3 2 16 9 10 14 8 7

4
1 2

1 3
3 4 5 6

7
2 8 9
16 9 10
14 8 7
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 1 3 2 16 9 10 14 8 7

Alg: BUILD-MAX-HEAP(A) 0

4
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 1 3
3 4 5 6

3. do ReheapDown(A, i, n-1) 2 16 9 10
7 8 9

14 8 7
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 1 3 2 16 9 10 14 8 7

Alg: BUILD-MAX-HEAP(A) 0

4
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 1 3
3 4 5 6

3. do ReheapDown(A, i, n-1) 2 16 9 10
7 8 9

14 8 7
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 1 3 2 16 9 10 14 8 7

Alg: BUILD-MAX-HEAP(A) 0

4
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 1 3
3 4 5 6

3. do ReheapDown(A, i, n-1) 2 16 9 10
7 8 9

14 8 7
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 1 3 14 16 9 10 2 8 7

Alg: BUILD-MAX-HEAP(A) 0

4
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 1 3
3 4 5 6

3. do ReheapDown(A, i, n-1) 14 16 9 10
7 8 9

2 8 7
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 1 3 14 16 9 10 2 8 7

Alg: BUILD-MAX-HEAP(A) 0

4
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 1 3
3 4 5 6

3. do ReheapDown(A, i, n-1) 14 16 9 10
7 8 9

2 8 7
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 1 10 14 16 9 3 2 8 7

Alg: BUILD-MAX-HEAP(A) 0

4
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 1 10
3 4 5 6

3. do ReheapDown(A, i, n-1) 14 16 9 3
7 8 9

2 8 7
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 1 10 14 16 9 3 2 8 7

Alg: BUILD-MAX-HEAP(A) 0

4
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 1 10
3 4 5 6

3. do ReheapDown(A, i, n-1) 14 16 9 3
7 8 9

2 8 7
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 16 10 14 1 9 3 2 8 7

Alg: BUILD-MAX-HEAP(A) 0

4
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 16 10
3 4 5 6

3. do ReheapDown(A, i, n-1) 14 1 9 3
7 8 9

2 8 7
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 16 10 14 7 9 3 2 8 1

Alg: BUILD-MAX-HEAP(A) 0

4
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 16 10
3 4 5 6

3. do ReheapDown(A, i, n-1) 14 7 9 3
7 8 9

2 8 1
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 16 10 14 7 9 3 2 8 1

Alg: BUILD-MAX-HEAP(A) 0

4
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 16 10
3 4 5 6

3. do ReheapDown(A, i, n-1) 14 7 9 3
7 8 9

2 8 1
Heap Sort
0 1 2 3 4 5 6 7 8 9
16 4 10 14 7 9 3 2 8 1

Alg: BUILD-MAX-HEAP(A) 0

16
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 4 10
3 4 5 6

3. do ReheapDown(A, i, n-1) 14 7 9 3
7 8 9

2 8 1
Heap Sort
0 1 2 3 4 5 6 7 8 9
16 14 10 4 7 9 3 2 8 1

Alg: BUILD-MAX-HEAP(A) 0

16
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 14 10
3 4 5 6

3. do ReheapDown(A, i, n-1) 4 7 9 3
7 8 9

2 8 1
Heap Sort
0 1 2 3 4 5 6 7 8 9
16 14 10 8 7 9 3 2 4 1

Alg: BUILD-MAX-HEAP(A) 0

16
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 14 10
3 4 5 6

3. do ReheapDown(A, i, n-1) 8 7 9 3
7 8 9

2 4 1
Heap Sort
0 1 2 3 4 5 6 7 8 9
16 14 10 8 7 9 3 2 4 1

Alg: BUILD-MAX-HEAP(A) 0

16
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 14 10
3 4 5 6

3. do ReheapDown(A, i, n-1) 8 7 9 3
7 8 9

2 4 1
Heap Sort
0 1 2 3 4 5 6 7 8 9
16 14 10 8 7 9 3 2 4 1

Alg: BUILD-MAX-HEAP(A) O(N) 0

16
1. n = length[A] - 1
1 2
2. for i ← (n-1)/2 downto 0 14 10
3 4 5 6

3. do ReheapDown(A, i, n-1) 8 7 9 3
7 8 9

2 4 1

O(logN)

Time complexity: O(NlogN)


Heap Sort
0 1 2 3 4 5 6 7 8 9
4 1 3 2 16 9 10 14 8 7

4
1 2

1 3
3 4 5 6

7
2 8 9
16 9 10
14 8 7
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 1 3 2 16 9 10 14 8 7

0
Alg: HEAPSORT(A) 4
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 1 3
3 4 5 6
3. for i = n downto 1 2 16 9 10
7 8 9
4. swap A[0] and A[i]
14 8 7
5. ReheapDown(A, 0, i-1)
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 1 3 2 16 9 10 14 8 7

0
Alg: HEAPSORT(A) 4
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 1 3
3 4 5 6
3. for i = n downto 1 2 16 9 10
7 8 9
4. swap A[0] and A[i]
14 8 7
5. ReheapDown(A, 0, i-1)
Heap Sort
0 1 2 3 4 5 6 7 8 9
16 14 10 8 7 9 3 2 4 1

0
Alg: HEAPSORT(A) 16
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 14 10
3 4 5 6
3. for i = n downto 1 8 7 9 3
7 8 9
4. swap A[0] and A[i]
2 4 1
5. ReheapDown(A, 0, i-1)
Heap Sort
0 1 2 3 4 5 6 7 8 9
16 14 10 8 7 9 3 2 4 1

0
Alg: HEAPSORT(A) 16
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 14 10
3 4 5 6
3. for i = n downto 1 8 7 9 3
7 8 9
4. swap A[0] and A[i]
2 4 1
5. ReheapDown(A, 0, i-1)
Heap Sort
0 1 2 3 4 5 6 7 8 9
1 14 10 8 7 9 3 2 4 16

0
Alg: HEAPSORT(A) 1
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 14 10
3 4 5 6
3. for i = n downto 1 8 7 9 3
7 8 9
4. swap A[0] and A[i]
2 4 16
5. ReheapDown(A, 0, i-1)
Heap Sort
0 1 2 3 4 5 6 7 8 9
14 1 10 8 7 9 3 2 4 16

0
Alg: HEAPSORT(A) 14
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 1 10
3 4 5 6
3. for i = n downto 1 8 7 9 3
7 8 9
4. swap A[0] and A[i]
2 4 16
5. ReheapDown(A, 0, i-1)
Heap Sort
0 1 2 3 4 5 6 7 8 9
14 1 10 8 7 9 3 2 4 16

0
Alg: HEAPSORT(A) 14
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 1 10
3 4 5 6
3. for i = n downto 1 8 7 9 3
7 8 9
4. swap A[0] and A[i]
2 4 16
5. ReheapDown(A, 0, i-1)
Heap Sort
0 1 2 3 4 5 6 7 8 9
14 8 10 1 7 9 3 2 4 16

0
Alg: HEAPSORT(A) 14
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 8 10
3 4 5 6
3. for i = n downto 1 1 7 9 3
7 8 9
4. swap A[0] and A[i]
2 4 16
5. ReheapDown(A, 0, i-1)
Heap Sort
0 1 2 3 4 5 6 7 8 9
14 8 10 1 7 9 3 2 4 16

0
Alg: HEAPSORT(A) 14
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 8 10
3 4 5 6
3. for i = n downto 1 1 7 9 3
7 8 9
4. swap A[0] and A[i]
2 4 16
5. ReheapDown(A, 0, i-1)
Heap Sort
0 1 2 3 4 5 6 7 8 9
14 8 10 4 7 9 3 2 1 16

0
Alg: HEAPSORT(A) 14
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 8 10
3 4 5 6
3. for i = n downto 1 4 7 9 3
7 8 9
4. swap A[0] and A[i]
2 1 16
5. ReheapDown(A, 0, i-1)
Heap Sort
0 1 2 3 4 5 6 7 8 9
14 8 10 4 7 9 3 2 1 16

0
Alg: HEAPSORT(A) 14
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 8 10
3 4 5 6
3. for i = n downto 1 4 7 9 3
7 8 9
4. swap A[0] and A[i]
2 1 16
5. ReheapDown(A, 0, i-1)
Heap Sort
0 1 2 3 4 5 6 7 8 9
10 8 9 4 7 1 3 2 14 16

0
Alg: HEAPSORT(A) 10
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 8 9
3 4 5 6
3. for i = n downto 1 4 7 1 3
7 8 9
4. swap A[0] and A[i]
2 14 16
5. ReheapDown(A, 0, i-1)

Delete 14
Heap Sort
0 1 2 3 4 5 6 7 8 9
9 8 3 4 7 1 2 10 14 16

0
Alg: HEAPSORT(A) 9
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 8 3
3 4 5 6
3. for i = n downto 1 4 7 1 2
7 8 9
4. swap A[0] and A[i]
10 14 16
5. ReheapDown(A, 0, i-1)

Delete 10
Heap Sort
0 1 2 3 4 5 6 7 8 9
8 7 3 4 2 1 9 10 14 16

0
Alg: HEAPSORT(A) 8
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 7 3
3 4 5 6
3. for i = n downto 1 4 2 1 9
7 8 9
4. swap A[0] and A[i]
10 14 16
5. ReheapDown(A, 0, i-1)

Delete 9
Heap Sort
0 1 2 3 4 5 6 7 8 9
7 4 3 1 2 8 9 10 14 16

0
Alg: HEAPSORT(A) 7
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 4 3
3 4 5 6
3. for i = n downto 1 1 2 8 9
7 8 9
4. swap A[0] and A[i]
10 14 16
5. ReheapDown(A, 0, i-1)

Delete 8
Heap Sort
0 1 2 3 4 5 6 7 8 9
4 2 3 1 7 8 9 10 14 16

0
Alg: HEAPSORT(A) 4
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 2 3
3 4 5 6
3. for i = n downto 1 1 7 8 9
7 8 9
4. swap A[0] and A[i]
10 14 16
5. ReheapDown(A, 0, i-1)

Delete 7
Heap Sort
0 1 2 3 4 5 6 7 8 9
3 2 1 4 7 8 9 10 14 16

0
Alg: HEAPSORT(A) 3
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 2 1
3 4 5 6
3. for i = n downto 1 4 7 8 9
7 8 9
4. swap A[0] and A[i]
10 14 16
5. ReheapDown(A, 0, i-1)

Delete 4
Heap Sort
0 1 2 3 4 5 6 7 8 9
2 1 3 4 7 8 9 10 14 16

0
Alg: HEAPSORT(A) 2
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 1 3
3 4 5 6
3. for i = n downto 1 4 7 8 9
7 8 9
4. swap A[0] and A[i]
10 14 16
5. ReheapDown(A, 0, i-1)

Delete 3
Heap Sort
0 1 2 3 4 5 6 7 8 9
1 2 3 4 7 8 9 10 14 16

0
Alg: HEAPSORT(A) 1
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 2 3
3 4 5 6
3. for i = n downto 1 4 7 8 9
7 8 9
4. swap A[0] and A[i]
10 14 16
5. ReheapDown(A, 0, i-1)

Delete 2
Heap Sort
0 1 2 3 4 5 6 7 8 9
1 2 3 4 7 8 9 10 14 16

0
Alg: HEAPSORT(A) 1
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 2 3
3 4 5 6
3. for i = n downto 1 4 7 8 9
7 8 9
4. swap A[0] and A[i]
10 14 16
5. ReheapDown(A, 0, i-1)
Heap Sort
0 1 2 3 4 5 6 7 8 9
1 2 3 4 7 8 9 10 14 16

Alg: HEAPSORT(A)
O(NlogN) 0

1
1. BUILD-MAX-HEAP(A) 1 2

2. n = length(A) - 1 2 3
3 4 5 6
3. for i = n downto 1 4 7 8 9
7 8 9
4. swap A[0] and A[i]
10 14 16
5. ReheapDown(A, 0, i-1)

O(NlogN)
Time complexity: O(NlogN)

You might also like