Docsity

Algorithms and Data Structures - Lecture 4 | CSE 331, Study notes of Computer Science

Toggle sidebar
bg1
CSE 331, Summer 2002, Lecture #4, (5/20/02)
1. Sorting
(a) Internal vs. External; most algorithms will be internal
(b) Array vs. Linked List; we will assume use of arrays
(c) Records and Keys
i. The record is the actual data; generally (much) larger than the key.
ii. The key is what is used to perform the comparison.
i. Pros: aids comprehension, simplifies analysis
ii. Cons: hidden memory fetch factor
(d) Type: elements may be of any comparable type (names, numbers, etc.); we’ll generally
assume integers.
(e) Code:
i. C-like
ii. Array to be sorted will be a[]
iii. Function swap( a, i, j ) simply swaps positions iand jin array a
2. Bubble Sort
(a) Likely simplest of all (why “bubble” I don’t exactly know).
(b) Algorithm:
i. Progress through the array comparing each two elements encountered.
ii. If larger one is left of smaller, elements are swapped.
iii. Repeat this array scan until no more element swaps are needed.
while ( !done )
{
i=0;
done=1;
while ( i<size-1 )
{
if ( a[i] > a[i+1] )
{
done = 0;
swap( a, i, i+1 );
}
i++;
}
}
(c) Correctness: Does this work? Why?
(d) Worst case analysis: Worst case occurs when? How many iterations?
(e) Comparison: When is this worse than bubble sort? Better?
pf2

Partial preview of the text

Download Algorithms and Data Structures - Lecture 4 | CSE 331 and more Study notes Computer Science in PDF only on Docsity!

CSE 331, Summer 2002, Lecture #4, (5/20/02)

  1. Sorting

(a) Internal vs. External; most algorithms will be internal (b) Array vs. Linked List; we will assume use of arrays (c) Records and Keys i. The record is the actual data; generally (much) larger than the key. ii. The key is what is used to perform the comparison. i. Pros: aids comprehension, simplifies analysis ii. Cons: hidden memory fetch factor (d) Type: elements may be of any comparable type (names, numbers, etc.); we’ll generally assume integers. (e) Code: i. C-like ii. Array to be sorted will be a[] iii. Function swap( a, i, j ) simply swaps positions i and j in array a

  1. Bubble Sort

(a) Likely simplest of all (why “bubble” I don’t exactly know). (b) Algorithm: i. Progress through the array comparing each two elements encountered. ii. If larger one is left of smaller, elements are swapped. iii. Repeat this array scan until no more element swaps are needed.

while ( !done ) { i=0; done=1; while ( i<size-1 ) { if ( a[i] > a[i+1] ) { done = 0; swap( a, i, i+1 ); } i++; } }

(c) Correctness: Does this work? Why? (d) Worst case analysis: Worst case occurs when? How many iterations? (e) Comparison: When is this worse than bubble sort? Better?

  1. Selection Sort

(a) “Selection” because smallest element is repeatedly selected. (b) Algorithm: i. Scan the array to find the ith^ smallest element. ii. Swap that element with the one in the ith^ position. iii. Repeating this for i=0...size-1 sorts the array.

for ( i=0; i<size; i++ ) { lowest = i; for ( j=i; j<size; j++ ) { if ( a[lowest] > a[j] ) lowest = j; } swap( a, i, lowest ); }

(c) Correctness: Does this work? Why? (d) Worst case analysis: Worst case occurs when? How many iterations? (e) Comparison: When is this worse than bubble sort? Better?

  1. Insertion Sort

(a) Simple sorting method often used on playing cards (e.g., bridge hands). (b) Algorithm: i. Begin at i=j=1 (the second element). ii. Scan backwards from j: iii. For j>0, if there exists a smaller element at position j-1, swap positions j and j-1, decrement j and repeat. iv. Otherwise, if i<size, increment i, set j=i and repeat again.

for ( i=0; i<size-1; i++ ) { for ( j=i+1, t=a[j]; j>0; j-- ) { if ( a[j-1] > t ) a[j] = a[j-1]; else last; } a[j] = t; }

(c) Correctness: Does this work? Why? (d) Worst case analysis: Worst case occurs when? How many iterations? (e) Comparison: When is this worse than bubble sort? Better?