Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental in Computer Science Sorting. Sorting Arrays (Basic sorting algorithms) Use available main memory Analyzed by counting #comparisons and #moves.

Similar presentations


Presentation on theme: "Fundamental in Computer Science Sorting. Sorting Arrays (Basic sorting algorithms) Use available main memory Analyzed by counting #comparisons and #moves."— Presentation transcript:

1 Fundamental in Computer Science Sorting

2 Sorting Arrays (Basic sorting algorithms) Use available main memory Analyzed by counting #comparisons and #moves Easy to understand and code Codes are short Examples – Straight insertion – Straight selection – Straight exchange (bubble) 2

3 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 3

4 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

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 Advanced sorting methods Improvement of straight methods – Insertion sort by diminishing increment (Shell) – Tree sort (Heap) – Partition sort (Quick) – Finding the median 6

7 Shell sort algorithm Define a gap h (start with h high) Sort all elements having distance h among those elements Diminish the gap h – May use h= 3h+1 (Knuth) to create series of gaps When h = 1 or low do insertion sort Gap could be any interval series which ends with h=1 no matter what N is. The original paper used N/2 Flamig’s h=(5h-1)/11 Gap must be easily calculated 7

8 Shell sort sample code h=1 while (h<=n/3) h=h*3+1; while(h>0) for(i=h; i<n; i++) temp = array[i]; j=i; while(j>h-1 && array[j-h] >= temp) array[j] = array[j-h]; j -=h; array[j]=temp; h=(h-1)/3; 8

9 Heap is a binary tree and – The value of each node is not less than the values stored in each of its children – The tree is perfectly balanced and the leaves in the last level are all in the left most positions Tree representation – Nodes and edges – Arrays 9

10 heapsort(data[]) transform data into a heap; for i = data.length-1 downto 2 swap the root with the element in position i; restore the heap property for the tree data[0], …, data[i- 1]; Heap Sort Algorithm 10

11 Partition sort algorithm (Quick) 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. 11

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

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

14 Finding the median Median of three Middle of list At some position 14

15 Comparison to Sorting arrays 256 elements 2048 elements 15

16 Sorting sequences Main memory is not fitted the size of data to be sorted Examples – Straight merging – Natural merging – Balanced Multi-way merging 16

17 Merge sort algorithm (straight) mergesort(data[], first, last) if first < last mid = (first + last) / 2; mergesort(data[], first, mid); mergesort(data[], mid+1, last); merge(data[], first, last); 17 //partitioning //merging

18 References N.Wirth, Algorithms and Data Structures, 1985. Robert Lafore, Data Structures & Algorithms in JAVA, SAMS, 2002. Data Structures and Algorithms in Java, A. Drozdek, 2008 18


Download ppt "Fundamental in Computer Science Sorting. Sorting Arrays (Basic sorting algorithms) Use available main memory Analyzed by counting #comparisons and #moves."

Similar presentations


Ads by Google