Sei sulla pagina 1di 16

slide 1

Lecture 17

Sorting
z
z

Arranging data into a certain order


Input:
an array (sequence) A of sortable data elements
A[start] A[start + 1] ... A[end]
Output:
array A satisfying the property
A[start] <= A[start + 1] <= ... <= A[end]
There are many sorting algorithms: SelectionSort, QuickSort,
MergeSort, InsertionSort, BubbleSort, ShellSort, etc.

slide 2

Lecture 17

SelectionSort
z

Basic idea:
for (i = start; i <= end - 1; i++)
select and put the i-th smallest element in A[i]

slide 3

Lecture 17

SelectionSort Example
//This class sorts an array, using the selection sort algorithm
public class SelectionSorter {
private int[] a; //reference to array to be sorted
//Constructs a selection sorter.
public SelectionSorter(int[] anArray) {
a = anArray;
}
//Sorts the array managed by this selection sorter.
public void sort( ) {
for (int i = 0; i < a.length - 1; i++) {
int minPos = minimumPosition(i);
swap(minPos, i);
}
}

slide 4

Lecture 17

SelectionSort Example (cont.)


//Finds the smallest element in a tail range of the array
private int minimumPosition(int from) {
int minPos = from; //first index in array a to compare
for (int i = from + 1; i < a.length; i++)
if (a[i] < a[minPos]) minPos = i;
return minPos; //index of the smallest element in the
//range a[from]...a[a.length - 1]
}
//Swaps two entries of the array with index i and j
private void swap(int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
} //End class SelectionSorter

slide 5

Lecture 17

SelectionSort Example (cont.)


/**This program tests the selection sort algorithm by
sorting an array that is filled with random numbers.
*/
public class SelectionSortTest {
public static void main(String[] args) {
int[] a = ArrayUtil.randomIntArray(20, 100);
System.out.println("Before sorting: ");
ArrayUtil.print(a);
SelectionSorter sorter = new SelectionSorter(a);
sorter.sort();
System.out.println("After sorting: ");
ArrayUtil.print(a);
}
}

Lecture 17

slide 6

SelectionSort Example (cont.)


import java.util.Random;
//This class contains utility methods for array manipulation.
public class ArrayUtil {
//Creates an array filled with random values between 0 and n-1
public static int[] randomIntArray(int length, int n) {
int[] a = new int[length];
Random generator = new Random();
for (int i = 0; i < a.length; i++) a[i] = generator.nextInt(n);
return a;
}
//Prints all elements in an array.
public static void print(int[] a) {
for (int i = 0; i < a.length; i++) System.out.print(a[i] + " ");
System.out.println();
}
}

Lecture 17

Sample program output


Before sorting:
15 7 71 7 41 56 39 58 46 88 50 72 45 63 53 90 39 50 78 59
After sorting:
7 7 15 39 39 41 45 46 50 50 53 56 58 59 63 71 72 78 88 90

slide 7

Lecture 17

QuickSort
Basic idea:
1) Choose an element as a pivot.
2) Partition the input array into two subarrays so that
elements of first subarray <= pivot <= elements of second subarray
3) QuickSort the first subarray and QuickSort the second subarray.
z

slide 8

Lecture 17

QuickSort Example
public class QuickSorter {
private int[] a;
public QuickSorter(int[] anArray) {
a = anArray;
}
public void sort( ) {
sort(0, a.length - 1);
}
public void sort(int from, int to)
{
if (from >= to) return;
int p = partition(from, to);
sort(from, p);
sort(p + 1, to);
}

slide 9

Lecture 17

QuickSort Example (cont.)


private int partition(int from, int to) {
int pivot = a[from];
int i = from - 1;
int j = to + 1;
while (i < j) {
i++; while (a[i] < pivot) i++;
j--; while (a[j] > pivot) j--;
if (i < j) swap(i, j);
}
return j; // index of last element in first subarray
}
private void swap(int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

slide 10

slide 11

Lecture 17

QuickSort Example (cont.)


public class QuickSortTest
{
public static void main(String[] args)
{
int[] a = ArrayUtil.randomIntArray(20, 100);
System.out.println("Before sorting:");
ArrayUtil.print(a);
QuickSorter sorter = new QuickSorter(a);
sorter.sort();
System.out.println("After sorting:");
ArrayUtil.print(a);
}
}

Lecture 17

Lomuto's Algorithm for Partition


//A simple partition algorithm: the Lomuto algorithm
//Rearranges the subarray A[b .. r] in place.
PARTITION(A, b, r)
1. x A[r]
//pivot element
2. i b - 1
3. for j b to r - 1
4.
do if A[j] x
5.
then i i + 1
6.
exchange A[i] A[j]
7. exchange A[i+1] A[r]
8. return i+1

slide 12

slide 13

Lecture 17

While the partition in progress ...

At the beginning of each iteration of the for loop of


lines 3 - 6, for any array index k,
1. If b k i, then A[k] x.
2. If i+1 k j-1, then A[k] > x.
3. If k = r, then A[k] = x.
x
b

>x
i

?
j

x
r

Lecture 17

Pseudocode for QuickSort with


Lomuto's Partition
QUICKSORT(A, b, r)
1. if b < r
2.
then p PARTITION(A, b, r)
3.
QUICKSORT(A, b, p -1)
4.
QUICKSORT(A, p+1, r)

To sort an entire array A, the initial call is


QUICKSORT(A, 0, n-1), where n is the length of A.

slide 14

Lecture 17

QuickSort with Lomuto's partition algorithm


public class QuickSorterLomuto {
private int[] a;
public QuickSorterLomuto(int[] anArray) {
a = anArray;
}
public void sort( ) {
sort(0, a.length - 1);
}
public void sort(int from, int to) {
if (from < to) {
int p = partition(from, to); //position of pivot
sort(from, p - 1);
sort(p + 1, to);
}
}

slide 15

slide 16

Lecture 17

QuickSort with Lomuto's partition algorithm (cont.)


private int partition(int from, int to) {
int pivot = a[to];
int i = from - 1;
for(int j = from; j < to; j++) {
if (a[j] <= pivot) {
i++; swap(i,j);
}
}
swap(i+1,to);
return i+1; //final position of pivot
}
private void swap(int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

Potrebbero piacerti anche