algorithms and data structures sorting and selection

12
Algorithms and Data Structures Sorting and Selection

Upload: buddy-long

Post on 22-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Algorithms and Data Structures Sorting and Selection

Algorithms and Data Structures

Sorting and Selection

Page 2: Algorithms and Data Structures Sorting and Selection

2

Simple sorters

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

Page 3: Algorithms and Data Structures Sorting and Selection

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

Page 4: Algorithms and Data Structures Sorting and Selection

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

Page 5: Algorithms and Data Structures Sorting and Selection

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

Page 6: Algorithms and Data Structures Sorting and Selection

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

Page 7: Algorithms and Data Structures Sorting and Selection

7

A lower bound

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

Page 8: Algorithms and Data Structures Sorting and Selection

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.

Page 9: Algorithms and Data Structures Sorting and Selection

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

Page 10: Algorithms and Data Structures Sorting and Selection

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

Page 11: Algorithms and Data Structures Sorting and Selection

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

Page 12: Algorithms and Data Structures Sorting and Selection

12

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

and Data Structures: The Basic Toolbox, Springer 2008.

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