searching arrays. comp104 array sorting & searching / slide 2 unordered linear search * search...
Post on 22-Dec-2015
216 views
TRANSCRIPT
Searching
Arrays
COMP104 Array Sorting & Searching / Slide 2
Unordered Linear Search Search an unordered array of integers for a
value and save its index if the value is found. Otherwise, set index to -1.
Algorithm:Assume value not in array and set index = -1Start with the first array element (index 0) WHILE(more elements in array){
If value found at current index, save indexTry next element (increment index)
}
COMP104 Array Sorting & Searching / Slide 3
Unordered Linear Searchvoid main() {
int A[8] = {10, 7, 9, 1, 17, 30, 5, 6};int x, n, index;
cout << "Enter search element: ";cin >> x;index = -1;for(n=0; n<8; n++)
if(A[n]==x)index = n;
if(index==-1)cout << "Not found!!" << endl;
elsecout << "Found at: " << index << endl;
}
COMP104 Array Sorting & Searching / Slide 4
Ordered Linear Search Search an ordered array of integers for a value and save
its index if the value is found. Otherwise, set index to -1.
The key difference in design is that this array is ordered. If we perform a linear search, and find that we have
already passed where the element might be found, we can quit early.
COMP104 Array Sorting & Searching / Slide 5
Ordered Linear Search
void main() {
int A[8] = { 1, 5, 6, 7, 9, 10, 17, 30};int x, n, index;
cout << "Enter search element: ";cin >> x;index = -1;for(n=0; n<8 && A[n]<=x; n++)
if(A[n] == x)index = n;
if(index==-1)cout << "Not found!!" << endl;
elsecout << "Found at: " << index << endl;
}
COMP104 Array Sorting & Searching / Slide 6
Binary Search
Search an ordered array of integers for a value and save its index if the value is found. Otherwise, set index to -1.
Binary search takes advantage of the sorting, to search the array efficiently.
COMP104 Array Sorting & Searching / Slide 7
Binary Search Binary search is based on a divide-and-
conquer strategy which works as follows: Start by looking at the middle element of the
array 1. If the value it holds is lower than the search
element, eliminate the first half of the array. 2. If the value it holds is higher than the search
element, eliminate the second half of the array.
Repeat this process until the element is found, or until the entire array is eliminated.
COMP104 Array Sorting & Searching / Slide 8
Binary Searchvoid main() {
int A[8] = {1, 5, 6, 7, 9, 10, 17, 30};int x, lower, middle, upper;
cout << "Enter search element: ";cin >> x;lower = 0;upper = 7; // array size - 1middle = (lower + upper)/2;while(A[middle]!=x && lower<upper){
if(x<A[middle])upper = middle - 1;
elselower = middle + 1;
middle = (lower + upper)/2;}if(A[middle]!=x)
cout << "Not found!!" << endl;else
cout << "Found at: " << middle << endl;}
Sorting
Arrays
COMP104 Array Sorting & Searching / Slide 10
Sorting To arrange a set of items in sequence. About 25~50% of all computing power is
used in sorting. Possible reasons:
Many applications require sorting Many applications perform sorting when they
don't have to Many applications use inefficient sorting
algorithms
COMP104 Array Sorting & Searching / Slide 11
Sorting Applications List of student ID names and numbers in a table
(sorted by name) List of scores before letter grade
assignment (sorted by students' scores) List of horses after a race (sorted by the
finishing times) To prepare an originally unsorted array for
ordered binary searching
COMP104 Array Sorting & Searching / Slide 12
Some Sorting Methods Selection sort
Bubble sort
Shell sort (a simple but faster sorting method)
Quick sort (a much faster sorting method for most applications)
COMP104 Array Sorting & Searching / Slide 13
Selection Sort Selection sort performs sorting by
repeatedly putting the largest element to the end of the unsorted part of the array until the whole array is sorted.
It is similar to the way that many people do their sorting.
COMP104 Array Sorting & Searching / Slide 14
Selection Sort Algorithm
1. Define the unsorted part of the array as the entire array
2. While the unsorted part of the array has more than one element:
Find its largest element
Swap with last element
Reduce unsorted part of the array by 1
COMP104 Array Sorting & Searching / Slide 15
Selection Sort// Selection Sort of integers into ascending orderint main(){
const int size = 9;int A[size] = { 10, 7, 9, 1, 9, 17, 30, 5, 6};int i, rightmost, temp, max_index;
for(rightmost=size-1; rightmost>0; rightmost--){// find largest itemmax_index = 0;for(i=1; i<=rightmost; i++)
if(A[i] > A[max_index]) max_index = i; // swap largest item with last item temp = A[max_index];
A[max_index] = A[rightmost];A[rightmost] = temp;
}cout << "The sorted array: ";for(i=0; i<size; i++)
cout << A[i] << " ";cout << endl;return 0;
}
COMP104 Array Sorting & Searching / Slide 16
Bubble Sort Bubble sort examines the array from start
to finish, comparing elements as it goes. Any time it finds a larger element before a smaller
element, it swaps the two. In this way, the larger elements are passed
towards the end. The largest element of the array therefore
"bubbles" to the end of the array. It then repeats the process for the unsorted part of
the array until the whole array is sorted.
COMP104 Array Sorting & Searching / Slide 17
Bubble Sort
Bubble sort works on the same general principle as shaking a soft drink bottle.
Right after shaking, the contents are a mixture of bubbles and soft drink, distributed randomly.
Because bubbles are lighter than the soft drink, they rise to the surface, displacing the soft drink downwards.
This is how bubble sort got its name, because the smaller elements "float" to the top, while
the larger elements "sink" to the bottom.
COMP104 Array Sorting & Searching / Slide 18
Bubble Sort
Algorithm Define the unsorted part of the array to be
the entire array While the unsorted part of the array has
more than one element: 1. For every element in the unsorted part,
swap with the next neighbor if it is larger than the neighbor
2. Reduce the unprocessed part of the array by 1
COMP104 Array Sorting & Searching / Slide 19
Bubble Sort// Bubble Sort of integers into ascending orderint main(){
const int size = 9;int A[size] = { 10, 7, 9, 1, 9, 17, 30, 5, 6};int i, rightmost, temp;
for(rightmost=size-1; rightmost>0; rightmost--){for(i=0; i<rightmost; i++){
if(A[i] > A[i+1] ){ // swap if greatertemp = A[i];
A[i] = A[i+1]; A[i+1] = temp; }
}}cout << "The sorted array: ";for(i=0; i<size; i++)
cout << A[i] << " ";cout << endl;return 0;
}