sequential and parallel sorting algorithms

Upload: kesharwanidurgesh

Post on 16-Jul-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Algorithms

Sequential and parallel sorting algorithmsContents

Insertion sort Quicksort Heapsort Mergesort Shellsort Lower boundsExternal sorting o

Merging sorted files Introduction 0-1-principle Odd-even transposition sort Odd-even merge sort Bitonic Sort Introduction LS3 sort 4-way mergesort Rotatesort 3n sort

Sorting networkso o o o o

Sorting on mesh-connected processor arrayso o o o o o o o

s2-way mergesortShearsort 2D odd-even transposition sort

Randomsort ;-) Sorting contest

Sorting algorithms

Insertion sortInsertion sort is an elementary sorting algorithm. It has a time complexity of (n ), thus being slower than heapsort, merge sort and also shellsort. Insertion sort is well suited for sorting small data sets or for the insertion of new elements into a sorted sequence.2

IdeaLet a0, ..., an-1 be the sequence to be sorted. At the beginning and after each iteration of the algorithm the sequence consists of two parts: the first part is still unsorted (i 0, ..., n).

a0, ..., ai-1 is already sorted, the second part ai, ..., an-1

In order to insert element ai into the sorted part, it is compared with ai-1, ai-2 etc. When an element aj with aj ai is found, ai is inserted behind it. If no such element is found, then ai is inserted at the beginning of the sequence. After inserting element ai the length of the sorted part has increased by one. In the next iteration, inserted into the sorted part etc. While at the beginning the sorted part consists of element end it consists of all elements a0, ..., an-1. Example: The following table shows the steps for sorting the sequence 5 7 0 3 4 2 6 1. On the left side the sorted part of the sequence is shown in red. For each iteration, the number of positions the inserted element has moved is shown in brackets. Altogether this amounts to 17 steps.5 5 0 0 0 0 0 0 7 7 5 3 3 2 2 1 0 0 7 5 4 3 3 2 3 3 3 7 5 4 4 3 4 4 4 4 7 5 5 4 2 2 2 2 2 7 6 5 6 6 6 6 6 6 7 6 1 1 1 1 1 1 1 7 (0) (0) (2) (2) (2) (4) (1) (6)

ai+1 is

a0 only, at the

ImplementationThe following simulation illustrates the realization of the algorithm. The corresponding program is given subsequently.

Simulation of insertion sort (Java applet for simulation of insertion sort) The following function insertionsort sorts an integer array a[0], ..., a[n-1]. The sorting function is encapsulated in a class InsertionSorter. The method sort passes the array to be sorted to an array a, sets n to its length and calls insertionsort. The statement to sort an array b with insertion sort is

InsertionSorter.sort(b); The program follows. public class InsertionSorter { private static int[] a; private static int n; public static void sort(int[] a0) { a=a0; n=a.length; insertionsort(); } private static void insertionsort() { int i, j, t; for (i=1; i0 && a[j-1]>t) { a[j]=a[j-1]; j--; } a[j]=t; } } } // end class InsertionSorter

AnalysisThe worst case occurs when in every step the proper position for the element that is inserted is found at the beginning of the sorted part of the sequence. I.e. in the while-loop sequences of length 1, 2, 3, ..., n1 are scanned. Altogether, these are (n-1)n / 2 sequence is sorted in decreasing order. (n ) operations. This case occurs when the original2

It is possible to find the inserting position of element ai faster, namely by binary search. However, moving the elements to the right in order to make room for the element to be inserted takes linear time anyway. The exact number of steps required by insertion sort is given by the number of inversions of the sequence. Definition: Let a = a0, ..., an-1 be a finite sequence. An inversion is a pair of index positions, where the elements of the sequence are out of order. Formally, an inversion is a pair ( i, j), where i < j and ai >

aj.1)Example: Let a = 5, 7, 4, 9, 7. Then, (0, 2) is an inversion, since a0 > a2, namely 5 > 4. Also, (1, 2) is an inversion, since 7 > 4, and (3, 4) is an inversion, since 9 > 7. There are no other inversions in this sequence.

We now count the inversions (i, j) of a sequence a separately for each position j. The resulting value of

vj gives the number of elements ai to the left of aj which are greater than aj.For instance, for the sequence a = 5, 7, 4, 9, 7 we have v2 = 2, since the two inversions (0, 2) and (1, 2) have 2 as their second component. Here, 5 and 7 are greater than 4. Definition: The inversion sequence v = v0, ..., vn-1 of a sequence a = a0, ..., an-1 is defined by

vj = |{ (i, j) | i < j ai > aj }|for j = 0, ..., n-1. Example: The above sequence a = 5, 7, 4, 9, 7 has the inversion sequence v = 0, 0, 2, 0, 1. Obviously, we have vi

i for all i = 0, ..., n-1. If all vi are 0, then and only then the sequence a is

sorted. If the sequence a is a permutation, then it is uniquely determined by its inversion sequence v. The permutation n-1, ..., 0 has inversion sequence 0, ..., n-1. Theorem: Let a = a0, ..., an-1 be a sequence and v = v0, ..., vn-1 be its inversion sequence. Then the number of steps T(a) required by insertion sort to sort sequence

a is

T(a) =the sum of all vi.

i = 0, ..., n-1 vi

Proof: Obviously, insertion sort takes vi steps in each iteration i. Thus, the overall number of steps is equal to

Example: The following table shows the sequence a of the first example and its corresponding inversion sequence. For instance, v5 = 4 since 4 elements to the left of and 4). The total number of inversions is 17. i 0 1 2 3 4 5 6 7 ai 5 7 0 3 4 2 6 1 vi 0 0 2 2 2 4 1 6 Thus, insertion sort requires 17 steps to sort this sequence (see first example).Sorting algorithms

a5 = 2 are greater than 2 (namely 5, 7, 3

QuicksortQuicksort is one of the fastest and simplest sorting algorithms [Hoa 62]. It works recursively by a divideand-conquer strategy .

IdeaFirst, the sequence to be sorted a is partitioned into two parts, such that all elements of the first part b are less than or equal to all elements of the second part c (divide). Then the two parts are sorted separately by recursive application of the same procedure (conquer). Recombination of the two parts yields the sorted sequence (combine). Figure 1 illustrates this approach.

Figure 1: Quicksort(n)

The first step of the partition procedure is choosing a comparison element x. All elements of the sequence that are less than x are placed in the first part, all elements greater than x are placed in the second part. For elements equal to x it does not matter into which part they come. In the following algorithm it may also happen that an element equal to x remains between the two parts.Algorithm Input: Output:

Partitionsequence

a , ..., an0

-1

with

n elements a , ..., aj are less than or equal to all elements ai,0

permutation of the sequence such that all elements ...,

an (i > j)-1

Method: 1. choose the element in the middle of the sequence as comparison element let

x

i = 0 and j = n-1 i jsearch for the first element search for the last element if 1.

while

ai which is greater than or equal to x aj which is less than or equal to x

i j1. exchange let

ai and aj

i = i+1 and j = j-1

After partitioning the sequence, quicksort treats the two parts recursively by the same procedure. The recursion ends whenever a part consists of one element only.

SimulationThe following simulation illustrates the partitioning procedure. (Java applet for the simulation of the partitioning procedure of quicksort)

ProgramThe following Java program implements quicksort. void quicksort (int[] a, int lo, int hi) { // lo is the lower index, hi is the upper index // of the region of array a that is to be sorted int i=lo, j=hi, h; // comparison element x int x=a[(lo+hi)/2]; // do { partition

while (a[i]x) j--; if (i