Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms and Data Structures Sorting and Selection.

Similar presentations


Presentation on theme: "Algorithms and Data Structures Sorting and Selection."— Presentation transcript:

1 Algorithms and Data Structures Sorting and Selection

2 Simple sorters Selection sort Insertion sort Exchange (bubble) sort 2

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 3

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--; endwhile a[j]=temp; endfor 4

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 5

6 Mergesort 6 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) 7

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. 8

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() 9

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() 10

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 11

12 อ้างอิง Kurt Mehlhorn and Peter Sanders, Algorithms and Data Structures: The Basic Toolbox, Springer 2008. Robert Lafore, Data Structures & Algorithms in JAVA, SAMS, 2002. 12


Download ppt "Algorithms and Data Structures Sorting and Selection."

Similar presentations


Ads by Google