0% found this document useful (0 votes)
31 views44 pages

13 Sorting2

Uploaded by

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

13 Sorting2

Uploaded by

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

1

2
3
97 53 59 26 41 58 31
After buildHeap
1 2 3 4 5 6 7

After swap 31 53 59 26 41 58 97

1 2 3 4 5 6 7

After deleteMax is 59 53 58 26 41 31 97
completed
1 2 3 4 5 6 7

One deleteMax Step

4
After swap 31 53 58 26 41 59 97

1 2 3 4 5 6 7

After deleteMax is 58 53 31 26 41 59 97
completed
1 2 3 4 5 6 7

5
After swap 41 53 31 26 58 59 97

1 2 3 4 5 6 7

After deleteMax is 53 41 31 26 58 59 97
completed
1 2 3 4 5 6 7

6
After swap 26 41 31 53 58 59 97

1 2 3 4 5 6 7

After deleteMax is 41 26 31 53 58 59 97
completed
1 2 3 4 5 6 7

7
31 26 41 53 58 59 97
After swap
1 2 3 4 5 6 7

After deleteMax is 31 26 41 53 58 59 97
completed
1 2 3 4 5 6 7

8
26 31 41 53 58 59 97
After swap
1 2 3 4 5 6 7

After deleteMax is 26 31 41 53 58 59 97
completed
1 2 3 4 5 6 7

9
template <class Comparable>
void heapsort(vector<Comparable> & a)
{
// buildHeap
for (int i = a.size()/2; i >=0; i––)
percDown(a, i, a.size());

// sort
for (int j = a.size() –1; j >0; j––)
{
swap(a[0], a[j]); // swap max to the last pos.
percDown(a, 0, j); // re-form the heap
}
}
10
template <class Comparable> // for deleteMax
// a is the array, i is the position to percolate down from
// n is the logical size of the array
void percDown( vector<Comparable> & a, int i, int n )
{
int child;
Comparable tmp;

for (tmp=a[i] ; leftChild(i) < n; i = child )


{
child = leftChild(i);
if ( child != n-1 && a[ child ] < a[ child+1 ] )
child++;
if ( a[child ] > tmp )
a[i] = a[ child ];
else inline int leftChild( int i )
break; {
} return 2*i+1;
a[ i ] = tmp; }
} 11
12
13
14
Initially

1 5 12 23 3 4 11 34

Actr Bctr Cctr

15
Initially

1 5 12 23 3 4 11 34

Actr Bctr Cctr

Compare 1 and 3, 1 is smaller, move that to C and increment Actr and Cctr

1 5 12 23 3 4 11 34 1

Actr Bctr Cctr

16
Compare 5 and 3, 3 is smaller, move that to C and increment Bctr and Cctr

1 5 12 23 3 4 11 34 1 3

Actr Bctr Cctr

17
Compare 5 and 3, 3 is smaller, move that to C and increment Bctr and Cctr

1 5 12 23 3 4 11 34 1 3

Actr Bctr Cctr

Compare 5 and 4, 4 is smaller, move that to C and increment Bctr and Cctr

1 5 12 23 3 4 11 34 1 3 4

Actr Bctr Cctr

18
Compare 5 and 11, 5 is smaller, move that to C and increment Actr and Cctr

1 5 12 23 3 4 11 34 1 3 4 5

Actr Bctr Cctr

19
Compare 5 and 11, 5 is smaller, move that to C and increment Actr and Cctr

1 5 12 23 3 4 11 34 1 3 4 5

Actr Bctr Cctr

Compare 12 and 11, 11 is smaller, move that to C and increment Bctr and Cctr

1 5 12 23 3 4 11 34 1 3 4 5 11

Actr Bctr Cctr

20
Compare 12 and 34, 12 is smaller, move that to C and increment Actr and Cctr

1 5 12 23 3 4 11 34 1 3 4 5 11 12

Actr Bctr Cctr

21
Compare 12 and 34, 12 is smaller, move that to C and increment Actr and Cctr

1 5 12 23 3 4 11 34 1 3 4 5 11 12

Actr Bctr Cctr

Compare 23 and 34, 23 is smaller, move that to C and increment Actr and Cctr

1 5 12 23 3 4 11 34 1 3 4 5 11 12 23

Actr Bctr Cctr

22
Move 34 to C

1 5 12 23 3 4 11 34 1 3 4 5 11 12 23 34

Actr Bctr Cctr

23
24
25
26
/**
* Mergesort algorithm (driver).
*/
template <class Comparable>
void mergeSort( vector<Comparable> & a )
{
vector<Comparable> tmpArray( a.size( ) );

mergeSort( a, tmpArray, 0, a.size( ) - 1 );


}

27
/**
* Internal method that makes recursive calls.
* a is an array of Comparable items.
* tmpArray is an array to place the merged result.
* left is the left-most index of the subarray.
* right is the right-most index of the subarray.
*/
template <class Comparable>
void mergeSort( vector<Comparable> & a,
vector<Comparable> & tmpArray, int left, int right )
{
if ( left < right )
{
int center = ( left + right ) / 2;
mergeSort( a, tmpArray, left, center );
mergeSort( a, tmpArray, center + 1, right );
merge( a, tmpArray, left, center + 1, right );
}
} 28
/**
* Internal method that merges two sorted halves of a subarray.
* a is an array of Comparable items.
* tmpArray is an array to place the merged result.
* leftPos is the left-most index of the subarray.
* rightPos is the index of the start of the second half.
* rightEnd is the right-most index of the subarray.
*/
template <class Comparable>
void merge( vector<Comparable> & a,
vector<Comparable> & tmpArray,
int leftPos, int rightPos, int rightEnd )
{
int leftEnd = rightPos - 1;
int tmpPos = leftPos;
int numElements = rightEnd - leftPos + 1;

29
// Main loop
while ( leftPos <= leftEnd && rightPos <= rightEnd )
if ( a[ leftPos ] <= a[ rightPos ] )
tmpArray[ tmpPos++ ] = a[ leftPos++ ];
else
tmpArray[ tmpPos++ ] = a[ rightPos++ ];

while ( leftPos <= leftEnd ) // Copy rest of first half


tmpArray[ tmpPos++ ] = a[ leftPos++ ];

while ( rightPos <= rightEnd ) // Copy rest of right half


tmpArray[ tmpPos++ ] = a[ rightPos++ ];

// Copy tmpArray back


for ( int i = 0; i < numElements; i++, rightEnd-- )
a[ rightEnd ] = tmpArray[ rightEnd ];
}
30
// Main loop
while ( leftPos <= leftEnd && rightPos <= rightEnd )
if ( a[ leftPos ] <= a[ rightPos ] )
tmpArray[ tmpPos++ ] = a[ leftPos++ ];
else
tmpArray[ tmpPos++ ] = a[ rightPos++ ];

while ( leftPos <= leftEnd ) // Copy rest of first half


tmpArray[ tmpPos++ ] = a[ leftPos++ ];

while ( rightPos <= rightEnd ) // Copy rest of right half


tmpArray[ tmpPos++ ] = a[ rightPos++ ];

// Copy tmpArray back


for ( int i = 0; i < numElements; i++, rightEnd-- )
a[ rightEnd ] = tmpArray[ rightEnd ];
} 31
// Main loop
while( leftPos <= leftEnd && rightPos <= rightEnd )
if( a[ leftPos ] <= a[ rightPos ] )
tmpArray[ tmpPos++ ] = a[ leftPos++ ];
else
tmpArray[ tmpPos++ ] = a[ rightPos++ ];

while( leftPos <= leftEnd ) // Copy rest of first half


tmpArray[ tmpPos++ ] = a[ leftPos++ ];

while( rightPos <= rightEnd ) // Copy rest of right half


tmpArray[ tmpPos++ ] = a[ rightPos++ ];

// Copy tmpArray back


for( int i = 0; i < numElements; i++, rightEnd-- )
a[ rightEnd ] = tmpArray[ rightEnd ];
}
Note that exactly one of these loops is actually executed! 32
// Main loop
while ( leftPos <= leftEnd && rightPos <= rightEnd )
if ( a[ leftPos ] <= a[ rightPos ] )
tmpArray[ tmpPos++ ] = a[ leftPos++ ];
else
tmpArray[ tmpPos++ ] = a[ rightPos++ ];

while ( leftPos <= leftEnd ) // Copy rest of first half


tmpArray[ tmpPos++ ] = a[ leftPos++ ];

while ( rightPos <= rightEnd ) // Copy rest of right half


tmpArray[ tmpPos++ ] = a[ rightPos++ ];

// Copy tmpArray back


for ( int i = 0; i < numElements; i++, rightEnd-- )
a[ rightEnd ] = tmpArray[ rightEnd ];
}
33
34
35
So the recurrence looks like this

T ( 1)  0
N
T ( N )  2T ( )  ( N  1)
2

Cost of the merge


Costs of the subproblems

36
This recurrence can be solved by (repeated)
expansion into
k 1
T ( N )  2 T ( 1) 
k

i
(N
0
2)i

37
This recurrence can be solved by (repeated)
expansion into
k 1
T ( N )  2 T ( 1) 
k

i
(N
0
2)i

T ( N )  2k  0  Nk  ( N  1)

38
This recurrence can be solved by (repeated)
expansion into
k 1
T ( N )  2 T ( 1) 
k

i
(N
0
2)
i

T ( N )  2  0  Nk  ( N  1)
k

Since k = log2 N
T ( N )  N  log N  ( N  1)  O ( N log N )
39
40
A Decision Tree to sort 3 numbers 41
42
43
44

You might also like