algorithms and data structures sorting and selection

Post on 22-Dec-2015

217 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Algorithms and Data Structures

Sorting and Selection

2

Simple sorters

• Selection sort• Insertion sort• Exchange (bubble) sort

3

Sample code – selection

• for(i=0;i<n-1;i++)min=i;for(j=i+1;j<n;j++)

If(a[j] < a[min])min=j;

endif

endfor temp=a[i];a[i]=a[min]a[min]=temp;

• endfor

4

Sample code – insertion

• for(i=1;i<n;i++)temp=a[i];j=i;while(j>0 and a[j-1] >= temp)

a[j] = a[j-1];j--;

endwhilea[j]=temp;

• endfor

5

Sample code - bubble

• for (i=n-1;i>1;i--)for (j=0;j<i;j++)

If (a[j]>a[j+1])temp=a[j];a[j]=a[j+1];a[j+1]=temp;

endif

endfor• endfor

6

Mergesort

mergesort(data[], first, last) if first < last mid = (first + last) / 2; mergesort(data[], first, mid); mergesort(data[], mid+1, last); merge(data[], first, last);

//partitioning

//merging

7

A lower bound

• Any comparison-based sorting algorithm is O(nlogn)

8

Quick sort

• Define the pivot value (right most)• Scan from left (Leftscan) and right (Rightscan)• If the Leftscan found the value which is larger than the

pivot value, stops.• If the Rightscan found the value which is smaller that the

pivot value, stops.• Swap those values and continue.• After partitioned, inserting the pivot value at the

boundary of left and right partitions, all values of left partition must be smaller than the pivot and all values of right partition must larger than the pivot.

9

Sample code• //--------------------------------------------------------------• public void recQuickSort(int left, int right)• {• if(right-left <= 0) // if size <= 1,• return; // already sorted• else // size is 2 or larger• {• long pivot = theArray[right]; // rightmost item• // partition range• int partition = partitionIt(left, right, pivot);• recQuickSort(left, partition-1); // sort left side• recQuickSort(partition+1, right); // sort right side• }• } // end recQuickSort()

10

Sample code (cont.)• //--------------------------------------------------------------• public int partitionIt(int left, int right, long pivot)• {• int leftPtr = left-1; // left (after ++)• int rightPtr = right; // right-1 (after --)• while(true)• { // find bigger item• while( theArray[++leftPtr] < pivot )• ; // (nop)• // find smaller item• while(rightPtr > 0 && theArray[--rightPtr] > pivot)• ; // (nop)

• if(leftPtr >= rightPtr) // if pointers cross,• break; // partition done• else // not crossed, so• swap(leftPtr, rightPtr); // swap elements• } // end while(true)• swap(leftPtr, right); // restore pivot• return leftPtr; // return pivot location• } // end partitionIt()

11

Selection (Searching)

• Do not use a full power of sorting• Find an element whose value is maximum or

minimum• Find an element with rank k– Use a partition part of quick sort

12

อ้�างอ้�ง• Kurt Mehlhorn and Peter Sanders, Algorithms

and Data Structures: The Basic Toolbox, Springer 2008.

• Robert Lafore, Data Structures & Algorithms in JAVA, SAMS, 2002.

top related