meljun cortes data structures advanced sorting
DESCRIPTION
Meljun Cortes Data Structures Advanced SortingTRANSCRIPT
Data Structures
� does not sort the element itself but increases
the efficiency of other sorting algorithms
� invented by Donald Shell in 1959
� extension of insertion sort
� good for medium-sized arrays
� space between elements is known as increment
and represented by letter h
ShellSort
Advanced Sorting *Property of STIPage 1 of 18
Data Structures
� N-sorting
� n can be less than half the number of
element
� n is reduced and the elements are sorted
again until n is equal to 1
� avoid the sequence constructed with the powers
of 2
ShellSort
Advanced Sorting *Property of STIPage 2 of 18
Data Structures
ShellSort
public void shellSort()
{
int inner, outer;
double temp;
int h = 1;
while(h <= nElems/3)
h = h*3 + 1;
while(h>0)
{
for(outer=h; outer<nElems; outer++)
{
Advanced Sorting *Property of STIPage 3 of 18
{
temp = theArray[outer];
inner = outer;
while(inner > h-1 &&
theArray[inner-h] >= temp)
{
theArray[inner]=theArray
[inner-h];
inner-=h;
}
theArray[inner] = temp;
}
h = (h-1) / 3;
}
}
Data Structures
� Given a series of elements below, generate its
5-sorted then 3-sorted
� 13, 14, 94, 33, 82, 25, 59, 94, 65, 23, 45,
27, 73, 25, 39, 10
Exercise
Advanced Sorting *Property of STIPage 4 of 18
Data Structures
Solution
Have the numbers
written in 5 column table
to get the 5-sorted gap
Still in 5-column gap,
sort each column, giving
you sorted numbers
below
Advanced Sorting *Property of STIPage 5 of 18
To get the 3-sorted gap,
repeat first step, but now,
in 3-column
Sort the 3-sorted gap
Data Structures
Shellsort
� Shellsort pseudocode
Shell Sort (Sorting the array A[size])
Determine the number of segments by dividing
the number of cells by two.
While the number of segments are greater than
zero
Advanced Sorting *Property of STIPage 6 of 18
{
For each segment, we do an Insertion
Sort. (think of how to write the loops
here efficiently... )
Divide the number of segments by two. }
Data Structures
� divided into two groups: all items with a key
value higher than the specified amount and all
items lower than the key value
� horizontal line represents the pivot value; this
determines which of the two groups an item is
placed
Partitioning
Advanced Sorting *Property of STIPage 7 of 18
Data Structures
Partitioning
Advanced Sorting *Property of STIPage 8 of 18
Data Structures
Partitioning
public int partitionIt(int left, int
right, double pivot)
{
int leftPtr = left - 1;
int rightPtr = right + 1;
while(true)
{
while(leftPtr < right &&
Advanced Sorting *Property of STIPage 9 of 18
while(leftPtr < right &&
theArray[++leftPtr]
< pivot)
;
while(rightPtr > left &&
theArray[--rightPtr]
> pivot)
;
if(leftPtr >= rightPtr)
break;
else
swap(leftPtr, rightPtr);
}
return leftPtr;
}
Data Structures
QuickSort
� operates by partitioning an array into two
subarrays and then calling itself to quicksort
each of these subarrays
public void recQuickSort(int left,
int right)
{
if(right - left <= 0)
return;
Advanced Sorting *Property of STIPage 10 of 18
return;
else
{
int partition =
partitionIt(left,
right, pivot);
recQuickSort(left,
partition-1);
recQuickSort(partition+1,
right);
}
}
Data Structures
QuickSort
� Basic steps
� Partition the array or subarray into left
(smaller keys) and right (larger keys)
groups.
� Call ourselves to sort the left group.
� Call ourselves again to sort the right group
Advanced Sorting *Property of STIPage 11 of 18
Data Structures
QuickSort
� pivot value to use
� Your pivot value should be the key value of
an actual data item, which you can call as
pivot.
� You can pick a data item more or less at
random. For simplicity, you can always pick
the one on the right end of the subarray
being partitioned.
� After the partition, if the pivot is inserted at
Advanced Sorting *Property of STIPage 12 of 18
� After the partition, if the pivot is inserted at
the boundary between left and right
subarrays, it will be in its final sorted
position.
Data Structures
QuickSort
public int partitionIt(int left, int
right, double pivot)
{
int leftPtr = left-1;
int rightPtr = right;
while(true)
{
while(theArray[++leftPtr]
< pivot)
;
Advanced Sorting *Property of STIPage 13 of 18
;
while(rightPtr > 0 &&
theArray[--rightPtr]
> pivot)
;
if(leftPtr >= rightPtr)
break;
else
swap(leftPtr,
rightPtr);
}
swap(leftPtr, right);
return leftPtr;
}
Data Structures
Median-of-Three
� ideal pivot choice but the process that it
requires is not practical as it would take more
time than the sort itself
� find the median of the first, last, and middle
elements of the array and use this as the pivot
Advanced Sorting *Property of STIPage 14 of 18
Data Structures
Median-of-Three
Advanced Sorting *Property of STIPage 15 of 18
� guaranteed that the element at the left end of
the subarray is less than or equal to the pivot,
and the element at the right end is greater than
or equal to the pivot
� process of partition need not to examine these
elements again
Data Structures
Median-of-Three
public double median0f3(int left,
int right)
{
int center = (left+right)/2;
if( theArray[left] >
theArray[center] )
swap(left, center);
Advanced Sorting *Property of STIPage 16 of 18
if( theArray[left] >
theArray[right] )
swap(left, right);
if( theArray[center] >
theArray[right] )
swap(center, right);
swap(center, right-1);
return theArray[right-1];
}
Data Structures
Median-of-Three
public int partitionIt(int left, int
right, double pivot)
{
int leftPtr = left;
int rightPtr = right - 1;
while(true)
{
Advanced Sorting *Property of STIPage 17 of 18
while(theArray[++leftPtr]
< pivot)
;
while(theArray[--rightPtr]
> pivot)
;
if(leftPtr >= rightPtr)
break;
else
swap(leftPtr, rightPtr);
}
swap(leftPtr, right-1);
return leftPtr;
}
Data Structures
Median-of-Threepublic void manualSort(int left, int
right)
{
int size = right-left+1;
if(size <= 1)
return;
if(size == 2)
{
if( theArray[left]
> theArray[right] )
swap(left, right);
return;
Advanced Sorting *Property of STIPage 18 of 18
return;
}
else
{
if( theArray[left]
> theArray[right-1] )
swap(left, right-1);
if( theArray[left]
> theArray[right] )
swap(left, right);
if( theArray[left]
> theArray[right] )
swap(right-1, right);
}
}