Beruflich Dokumente
Kultur Dokumente
EN47 / COMP09-01
Exploring Computer Science
Spring 2009
Alexandre R.J. Franois
Computer Science Department
Tufts University
This course makes extensive use of material developed (and graciously provided) by Soha Assoun.
Today's Outline
Linear search
Complexity
Sorting
Bubble sort
Efficiency
Searching
Searching = looking for something
Searching an array is particularly common
Goal: determine whether a particular value is in the array
12
-5
A[0]
A[1]
A[2]
A[3]
142
21
-17
...
45
A[N-1]
Function prototype:
int LinearSearch(int A[], int size, int target){
}
Algorithm
start at the beginning of the array and examine each element
until x is found, or until all elements have been examined
Examples:
LinearSearch(A,8,6)
returns 3
LinearSearch(A,8,15)
returns -1
3
12
-5
A[0]
A[1]
A[2]
A[3]
142
21
-17
...
45
A[N-1]
Loop
The loop condition is written so that A[index] is not
accessed if index >= size:
while ((index < size) && (A[index] != x))
What does && mean?
Why do we need index < size?
Why do we need A[index] != x
Complexity
Number of elements to consider in order to give an answer
Worst case?
Average?
8.6104
7.31015
1.11017
1.51017
1.51017
31017
3.81017
4.11017
Examples:
LinearSearch(A,8,6)
LinearSearch(A,8,15)
from http://physics.ucr.edu/~wudka/Physics7/Notes_www/node16.html
12
-5
A[0]
A[1]
A[2]
A[3]
142
21
-17
...
45
A[N-1]
Sorting
The problem: put things in order
Usually smallest to largest: ascending
Could also be largest to smallest: descending
Lots of applications:
ordering hits in web search engine
preparing lists of output
merging data from multiple sources
faster search (e.g. binary search)
Sorting Algorithms
Sorting has been intensely studied for decades
Efficiency is important
Sorting Problem
Initial conditions: unsorted array
Goal: data sorted in increasing order:
A[0] <= A[1] <= ... <= A[N-1]
Bubble Sort
Compare first two elements:
If out of order, exchange them so that they are in order
End of pass 1
12
-5
142
21
-17
45
12
-5
142
21
-17
45
-5
12
142
21
-17
45
-5
12
142
21
-17
45
-5
12
142
21
-17
45
-5
12
21
142
-17
45
-5
12
21
-17
142
45
-5
12
21
-17
45
142
12
-5
142
21
-17
45
End of pass 1
-5
12
21
-17
45
142
End of pass 2
-5
12
-17
21
45
142
End of pass 3
-5
-17
12
21
45
142
End of pass 4
-5
-17
12
21
45
142
End of pass 5
-5
-17
12
21
45
142
End of pass 6
-17
-5
12
21
45
142
No more exchanges
EN47/COMP09-01 Spring 2009 - ARJF
Swap
When do we swap?
Swapping two values:
Int temp;
if (A[count] > A[count+1]) {
temp = A[count];
A[count] = A[count+1];
A[count+1] = temp;
}
}
EN47/COMP09-01 Spring 2009 - ARJF
1 comparison and 3
assignments each time
through
N times through outer loop
and a total of N/2 times
through inner loop
Efficiency: O(N2)
Summary
Linear search
Complexity
Sorting
Bubble sort
Efficiency
Lab Prep
Write up!
Your program will then sort the integers into ascending order
using the bubble sort algorithm and print the result.
2. Linear search
After each search the program will ask the user if there is
another number to search for
EN47/COMP09-01 Spring 2009 - ARJF
Parameters: