28
anestis.toptsis.cse.1030 1 Sorting

Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 1

Sorting

Page 2: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 2

Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small)

Why do we want to do this? It is easier to search in a sorted list. (e.g. lg N, if do Binary search)

There are lots of sorting algorithms (e.g. D.Knuth Vol.3) (those that we’ll do here, plus many many more.)

Page 3: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 3

Typical complexity of sorting algorithms:

O(N²) → Easy to write; Slow when run.O(N lg N) → harder to write; faster when run

Page 4: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 4

Some Sorting algorithms:Selection sort – O(N^2)Merge sort – O(N lg N)Quick sort -- O(N lg N)

Page 5: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 5

Selection sort

One of the easiest sorting algorithms (may be the easiest?) Complexity: O(N^2)

Page 6: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 6

Selection sort:3,9,7,13,8,1,2 (unsorted list)Assuming that we want to sort in

ascending order (descending is the same idea)

Algorithm:1. find smallest item X in the list2. output X3. “delete” X from list4. while (list != empty){

do 1.2.3.}

Page 7: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 7

Example: (list as before: 3,9,7,13,8,1,2)

Iteration Smallest

Item List Output

0 3,9,7,13,8,1,2

1 1 3,9,7,13,8,2 1

2 2 3,9,7,13,8 1,2

3 3 9,7,13,8 1,2,3

4 7 9,13,8 1,2,3,7

5 8 9,13 1,2,3,7,8

6 9 13 1,2,3,7,8,9,

7 13 1,2,3,7,8,9,13

final output

Page 8: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 8

∑=

N

ii

0∑=

N

ii

0

Complexity:Iteration 1: Scan list of N items (N comparisons) and find smallest.Iteration 2: Scan list of N-1 items (N-1 comparisons) and find smallest.Iteration 3: Scan list of N-2 items (N-2 comparisons) and find smallest.etc. total number of steps:N + (N-1) + (N-2) + (N-3) + ……+3+2+1 → O (N²)

Page 9: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 9

How to implement Selection sort:With array(s):Store initial list into an array A [ ]Then, as we find the smallest item during each iteration, we accumulate the sorted growing list on one side of the array.

Page 10: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 10

public void sort( ){int min = giveMeMinLocation(0, theList.length–1);swap(0, min);min = giveMeMinLoc(1, theList.length-1);swap(1, min);min = giveMeMinLoc(2, theList.length-1);swap(2, min);

…Min = giveMeMinLoc(N-2, theList.length-1);Swap(N-2, min); //N-2 is theList.lengthint min;for (int i = 0; i < = theList.length–2; i++){

min = giveMeMinLocation(i, theList.length–1);swap(i, min);

}}

Page 11: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 11

Private void swap (int i; int j){int temp;temp = theList[i];theList[i] = theList(i);theList[j] = temp;

}

Page 12: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 12

private int giveMeMinLocation(int start, int end){int cm = theList[start];int minLoc = start;for (int i = start; i < = end; i ++){

if (theList[i] < cm){ minLoc = i;cm = theList[i];}

}return minLoc;

}

Page 13: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 13

Quicksort

One of the most popular sorting algorithms (may be the most popular?)Belongs to the category of algorithms known as “divide and conquer”.“Not as easy”.Complexity: O(N lgN)

Page 14: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 14

Quicksort

I. Start with unsorted list of items, L.II. Chose an element from L, as the pivot. (can

select any element, typical choices are the last, or the middle, but does not really matter.)

III. Create 2 sub-lists L1 and L2 : a. L1: all elements of L < pivotb. L2: all elements of L > pivotc. (If L has elements = = pivot, then put those

together, next to the pivot)IV. Repeat II & III for each of the sub-lists L1 &

L2.

Page 15: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 15

Example: (L: unsorted list)85, 24, 63, 45, 17, 31, 96, 50

Chose pivot: 50 (last of L)L1: 24, 45, 17, 31L2: 85, 63, 96

Page 16: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 16

L1: 24, 45, 17, 31L2: 85, 63, 96

Now repeat process for L1 & L2L1: pivot 31

L11: 24, 17L12: 45

L2: pivot 96L21: 85, 63L22: - (Empty)

Page 17: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 17

By now, list L looks like this:

24,17, 31, 45, 50, 85,63, 96

L11 L12 L21

pivot pivot

pivot

Page 18: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 18

… to continue

Lists with more than 1 item: continue sorting.Lists that contain 1 item or are empty: already sorted (nothing else to be done).

Repeat process for L11 & L21

Page 19: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 19

L11: 24, 17

L11: pivot 17

Breaks into:L111: -- (empty and thus sorted).L112: 24 (one item and thus sorted).

Therefore, L11 is now sorted

Page 20: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 20

... And by now, list L looks like this:

17, 24, 31, 45, 50, 85,63, 96

L112 (already sorted

since has 1 item)

L12 (already sorted since has 1 item)

L21

pivotpivot

pivot

pivot

Page 21: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 21

L21: 85, 63

L21: pivot 63

Breaks into:L211: -- (one item and thus sorted).L212: 85 (empty and thus sorted).

Therefore, L21 is now sorted

Page 22: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 22

... And by now, list L looks like this:

17, 24, 31, 45, 50, 63, 85, 96

L112 (already

sorted since has 1 item)

L12 (already sorted since has 1 item)

pivotpivot

pivotpivot

pivot

L212 (already

sorted since has 1 item)

Page 23: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 23

Complexity of Quicksort

First, selecting the pivot (for list of size N), and splitting the list into 2 lists (> pivot, < pivot), needs N-1 comparisons.This 1st iteration, involves

1 pivot andN-1 comparisons.

Page 24: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 24

... 2nd iteration

Select 2 more pivots and split the 2 lists, resulting into 4 lists.This 2nd iteration involves

3 pivots (the original pivot + the 2 new pivots) – note, 3 = 2^2 - 1, and N-3 comparisons

Page 25: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 25

... 3rd iteration

Select 4 more pivots and split the 4 lists, resulting into 8 lists.This 3rd iteration involves

7 pivots, note 7 = 2^3 – 1, and N -7 comparisons

Likewise, the 4th iteration involves 2^4 –1 = 15 pivots and N – 15 comparisons, etc.

Page 26: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 26

… The “last” iteration

The last iteration involves2^last – 1 pivots, and N – (2^last – 1 ) comparisons

Note, it has to be N – (2^last – 1 ) > 0N > (2^last – 1 ) lg ( N+1 ) > lastlast = O(lgN)

Page 27: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 27

That is, during the last iteration, weperform

N– (2^lgN - 1) = N– (N - 1) = 1

comparison only.

Page 28: Sorting - yorku.ca -- selection and quicksort.pdf · Sorting: Put a list of items in order; either ascending (small to big) or descending (big to small) Why do we want to do this?

anestis.toptsis.cse.1030 28

Overall, we perform O(lgN) iterations.Therefore, the total number of comparisons is:

(N – 1) + (N – 3) + (N – 7) + … + 1 which turns out to be

O(N∙lgN )[the calculation is somewhat complicatedand outside the scope of this course].