Anda di halaman 1dari 18

Data Structures

ShellSort
 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

Advanced Sorting

*Property of STI
Page 1 of 18

Data Structures

ShellSort
 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

Advanced Sorting

*Property of STI
Page 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++)
{
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;
}
}

Advanced Sorting

*Property of STI
Page 3 of 18

Data Structures

Exercise
 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

Advanced Sorting

*Property of STI
Page 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

To get the 3-sorted gap, Sort the 3-sorted gap


repeat first step, but now,
in 3-column

Advanced Sorting

*Property of STI
Page 5 of 18

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
{
For each segment, we do an Insertion
Sort. (think of how to write the loops
here efficiently... )
Divide the number of segments by two.

Advanced Sorting

*Property of STI
Page 6 of 18

Data Structures

Partitioning
 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

Advanced Sorting

*Property of STI
Page 7 of 18

Data Structures

Partitioning

Advanced Sorting

*Property of STI
Page 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 &&
theArray[++leftPtr]
< pivot)
;
while(rightPtr > left &&
theArray[--rightPtr]
> pivot)
;
if(leftPtr >= rightPtr)
break;
else
swap(leftPtr, rightPtr);
}
return leftPtr;
}
Advanced Sorting

*Property of STI
Page 9 of 18

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;
else
{
int partition =
partitionIt(left,
right, pivot);
recQuickSort(left,
partition-1);
recQuickSort(partition+1,
right);
}
}

Advanced Sorting

*Property of STI
Page 10 of 18

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 STI
Page 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
the boundary between left and right
subarrays, it will be in its final sorted
position.

Advanced Sorting

*Property of STI
Page 12 of 18

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)
;
while(rightPtr > 0 &&
theArray[--rightPtr]
> pivot)
;
if(leftPtr >= rightPtr)
break;
else
swap(leftPtr,
rightPtr);
}
swap(leftPtr, right);
return leftPtr;
}
Advanced Sorting

*Property of STI
Page 13 of 18

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 STI
Page 14 of 18

Data Structures

Median-of-Three

 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

Advanced Sorting

*Property of STI
Page 15 of 18

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);
if( theArray[left] >
theArray[right] )
swap(left, right);
if( theArray[center] >
theArray[right] )
swap(center, right);
swap(center, right-1);
return theArray[right-1];
}

Advanced Sorting

*Property of STI
Page 16 of 18

Data Structures

Median-of-Three
public int partitionIt(int left, int
right, double pivot)
{
int leftPtr = left;
int rightPtr = right - 1;
while(true)
{
while(theArray[++leftPtr]
< pivot)
;
while(theArray[--rightPtr]
> pivot)
;
if(leftPtr >= rightPtr)
break;
else
swap(leftPtr, rightPtr);
}
swap(leftPtr, right-1);
return leftPtr;
}
Advanced Sorting

*Property of STI
Page 17 of 18

Data Structures

Median-of-Three
public 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;
}
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);
}
}
Advanced Sorting

*Property of STI
Page 18 of 18