insertion sort void inssort (elem * array, int n) { for (int i=1; i0) && (key(array[j])

16
Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I<n; I++) for (int j=I; (j>0) && (key(array[j])<key(array[j- 1])); j--) swap (array[j], array[j-1]); } init I=1 I=2 I=3 I=4 I=5 I=6 I=7 42 20 17 13 13 13 13 13 20 42 20 17 17 14 14 14 17 17 42 20 20 17 17 15 13 13 13 42 28 20 20 17 28 28 28 28 42 28 23 20 14 14 14 14 14 42 28 23 23 23 23 23 23 23 42 28 15 15 15 15 15 15 15 42

Upload: bryce-oneal

Post on 18-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Insertion Sortvoid inssort (ELEM * array, int n) {

for (int I=1; I<n; I++)for (int j=I; (j>0) && (key(array[j])<key(array[j-1]));

j--)swap (array[j], array[j-1]);

}

init I=1 I=2 I=3 I=4 I=5 I=6 I=742 20 17 13 13 13 13 1320 42 20 17 17 14 14 1417 17 42 20 20 17 17 1513 13 13 42 28 20 20 1728 28 28 28 42 28 23 2014 14 14 14 14 42 28 2323 23 23 23 23 23 42 2815 15 15 15 15 15 15 42

Page 2: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Speed

CASE Comparisons SwappingBEST n-1 0WORST O(n2) O(n2) AVERAGE O(n2) O(n2)

Page 3: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Bubble Sort

void bubsort(ELEM * array, int n) {for (int I=0; I<n-1; I++)

for (int j=n-1; j>I; j--)if (key(array[j]) < key(array[j-1]))

swap (array[j], array[j-1]);}

Example: smallest goes to top with each pass.

Speed Analysis:

CASE Comparisons SwapsBest O(n2) 0Worst O(n2) O(n2) Average O(n2) O(n2)

Page 4: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Selection Sort

void selsort (ELEM * array, int n) {for (int I=0; I<n-1; I++){

int lowindex=I;for (int j=n-1; j>I; j--)

if (key(array[j])<key(array[lowindex]))lowindex=j;

swap(array[I], array[lowindex]);}

}EXAMPLE: find smallest and put at top of rest of list.

Speed Analysis:CASE Comparisons SwapsBest O(n2) 0Worst O(n2) O(n) Average O(n2) O(n)

Page 5: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Pointer Swapping

• Instead of swapping records, swap pointers in an array.• Start array with 0, 1, 2, …., n-1.• Just needs extra level of subscripting in algorithms.

void selsort (ELEM * array, int n) {for (int I=0; I<n-1; I++){

int lowindex= I;for (int j=n-1; j>I; j--)

if (key(array[point[j]]) <key(array[point[lowindex]]))

lowindex=j;swap(point[I], point[lowindex]);

}}

Page 6: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Quicksort

• Divide and Conquer technique• Split the problem into 2 subproblems so that when

each is solved independently, their solutions will be the solution to the whole problem.

• Need to divide the problem such that all of one subproblem has all values less that all the values of the other subproblem. This way when each subproblem is sorted, the entire array will be sorted.

• Now to sort each subset, we divide and conquer again.

• Repeat until we have subproblems of size 0 or 1.

Page 7: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Quicksort Algorithm

void qsort (ELEM * array, int I, int j) {int pivotindex = findpivot(array,I,j);swap (array[pivotindex], array[j]);int k=partition(array, I-1,j, key(array[j]));swap (array[k], array[j]);if ((k-I)>1) qsort(array,I,k-1);if ((j-k)>1) qsort(array,k+1,j);

}

int findpivot (ELEM * array, int I, int j){return (I+j)/2;}

Page 8: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Quicksort Partition

int partition (ELEM * array, int l, int r, KEY pivot){do {

while (key(array[++l]) < pivot);while (r && (key(array[--r]) >pivot));swap (array[l], array[r]);} while (l<r);

swap (array[l], array[r]);return l;}

Page 9: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Partition Exampleinitial 72 6 57 88 85 42 83 73 48 60 l rPass 1 72 6 57 88 85 42 83 73 48 60 l rSwap 1 48 6 57 88 85 42 83 73 72 60 l rPass 2 48 6 57 88 85 42 83 73 72 60 l rSwap 2 48 6 57 42 85 88 83 73 72 60 l rPass 3 48 6 57 42 85 88 83 73 72 60 r lSwap 3 48 6 57 85 42 88 83 73 72 60 r lRev. 48 6 57 42 85 88 83 73 72 60SwapCost: O(n)

Page 10: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Quicksort Example

72 6 57 88 60 42 83 73 48 85

48 6 57 42 60 88 83 73 72 85

Pivot = 60

6 42 57 48

Pivot = 6 Pivot = 73

72 73 83 88 85

Pivot = 57 Pivot = 88

42 48 57 83 85 88

Pivot = 83Pivot = 42

42 48 83 85

Page 11: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Cost for Quicksort

Best Case : Always partition in half O(n log n)Worst case : Bad partition - have a subproblem of size 1 each

time. O(n2).Average case:

T(n)= n+1+ 1/(n-1) k=1 (T(k)+T(n-k))

= O(n log n)

Optimizations for Quicksort

• Better pivot• Use better algorithm for small sublists• Eliminate recursion.

n-1

Page 12: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Heapsort

• Use a min heap to get items in ascending order.

• Create the heap – O(n).

• Remove an item – O(log n).

• Remove n items – O(n log n).

Page 13: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

MergesortGood for both internal and especially external

sorting.The function merge merges 2 sorted lists into

one sorted list. More easily done when sorted.

List mergesort(list inlist){if (length(inlist)== 1) return inlist;list l1=half of items from inlist;list l2=other half of items from inlist;return merge(mergesort(l1), mergesort(l2));}

Page 14: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Mergesort Example

36 20 17 13 28 14 23 15

13 17 20 36 14 15 23 28

36 20 17 13 28 14 23 15

13 14 15 17 20 23 28 36

20 36 13 17 14 28 15 23

36 20 17 13 28 14 23 15

Page 15: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Mergesort Analysis• T(1)=1• T(n)=2T(n/2)+n• Solve• T(n)/n=T(n/2)/(n/2)+1• T(n/2)/(n/2)=T(n/4)/(n/4)+1• T(n/4)/(n/4)=T(n/8)/(n/8)+1 …• T(2)/2=T(1)/1+1 add all equations• T(n)/n+T(n/2)/(n/2)…+T(2)/2=T(n/2)/(n/2)+…+T(1)/1

+ log n• T(n)/n=T(1)+log n• T(n)=1+n log n=O(n log n)

Page 16: Insertion Sort void inssort (ELEM * array, int n) { for (int I=1; I0) && (key(array[j])

Optimized Mergesortvoid mergesort(ELEM * array, ELEM * temp, int left, int

right) {int I, j, k, mid=(left+right)/2;if (left == right) return;mergesort(array, temp, left, mid);mergesort(array, temp, mid+1, right);for (I=left; I<=mid; I++) temp[I] = array[I];for (j=1; j<=right-mid; j++) temp[right-j+1] =

array[j+mid];I=left;j=right;for (k=left; k<=right; k++)

if (temp[I]<temp[j]) array[k]=temp[I++];else array[k]=temp[j--];

}