Algorithms Search Sort
Algorithms Search Sort
False
Searching - Sequential
A K 10 2 J 2 2 Q 9
9
Bubble Sorting
• Bubble, Selection, Insertion Sorts
• Uses the same strategy that you use for sorting your
bridge hand. You pick up a card, start at the beginning
of your hand and find the place to insert the new card,
insert it and move all the others up one place.
• From the first element
• Exchange pairs if they’re out of order
• Last one must now be the largest
• Repeat from the first to n-1
• Stop when you have only one element to check
2 8 5 7 6 1 3 4
Bubble Sorting
http://math.hws.edu/eck/js/sorting/xSortLab.html
/* Bubble sort for integers */
For i=0 to n
for j=1 to n-i
if( a[j-1]>a[j] ) then
temp=a[j-1]
a[j-1]=a[j]
a[j]=temp
End if
Next j
Next I
(n-1)+..7+6+5+4+3+2+1=
n( n 1)
O(n 2 )
2
Bubble Sorting
GIS Bank Bank Bank Bank
Bank GIS GIS GIS GIS
Hospital Hospital Hospital Hospital Hospital
Tire Tire Tire Tire Automobile
Automobile Automobile Automobile Automobile Tire
Surveying Surveying Surveying Surveying Surveying
Real Real Real Real Real
Bank Bank
GIS GIS
Hospital Hospital
Automobile Automobile
Surveying Surveying
Tire Real
Real Tire
http://math.hws.edu/TMCM/java/xSortLab/
Quicksort
• Efficient sorting algorithm
• Discovered by C.A.R. Hoare
• C. A. R. Hoare, Quicksort, Comp. J.
5(1) p10-15 1962.
http://cg.scs.carleton.ca/~morin/misc/sortalg/
Quicksort
• Partition
• Choose a pivot
• Find the position for the pivot so that
• all elements to the left are less
• all elements to the right are greater
http://www.cs.auckland.ac.nz/software/AlgAnim/qsort.html
Quicksort
• Conquer
• Apply the same algorithm to each half
110 A-6
Exam review - Binary numbers questions
• (101101)2 = = (45)10
• (75)10 = = (1001011)2
• (13)8 = =(11)
• (4D3)16 =3+208+1024=1235
Exam review DBMS – Example
CREATE TABLE Projects ([J#] TEXT, [JNAME] TEXT, PRIMARY KEY ([J#]) );
ALTER TABLE Projects ADD CITY TEXT
INSERT INTO Projects VALUES("J1", "SERVCO" , "Big Rapids" );
DBMS – Example
The three tables Part, Suppliers, and Projects have many to many
relationships through table SPJ (the Bridges table)
DBMS – Example