Fundamental in Computer Science Sorting. Sorting Arrays (Basic sorting algorithms) Use available...

18
Fundamental in Computer Science Sorting

Transcript of Fundamental in Computer Science Sorting. Sorting Arrays (Basic sorting algorithms) Use available...

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

Fundamental in Computer Science

Sorting

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

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)

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

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

endwhilea[j]=temp;

• endfor

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

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

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

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: Fundamental in Computer Science Sorting. Sorting Arrays (Basic sorting algorithms) Use available main memory Analyzed by counting #comparisons and #moves.

6

Advanced sorting methods

• Improvement of straight methods– Insertion sort by diminishing increment (Shell)– Tree sort (Heap)– Partition sort (Quick)– Finding the median

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

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

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

Shell sort sample codeh=1while (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

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

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

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

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

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

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.

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

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

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

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

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

14

Finding the median

• Median of three• Middle of list• At some position

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

15

Comparison to Sorting arrays

256 elements

2048 elements

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

16

Sorting sequences

• Main memory is not fitted the size of data to be sorted

• Examples – Straight merging– Natural merging– Balanced Multi-way merging

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

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

//partitioning

//merging

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

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