Chapter 8 Searching and Sorting Arrays
Big O notation ised to characterize the
Const algorithm
Binary search: Power of 2 are used to calculate the maximum number of comparison the binary search will make on an array of any size
50000 Elements closest 2^16 == 65536 10000000 Elements closest 2^20
Sorting algorithms are used to arrange data into some order. To sort the data in an array, the programmer must use an appropriate sorting algorithm.
A sorting algorithm is a technique for scanning through an array and rearranging its contents in some specific order. This section will introduce two simple sorting algorithm: The bubble sort and the selection sort
Inefficiency of the linear search -- if the array contains 20000 elements, the algorithm will have to look at all 20000 elements in order to find a value stored in the last elements
The advantage of the linear search is its simplicity. It is very easy to understand and implement.
The bubble sort is an easy way to arrange data in ascending or descending order
The bubble sort starts by comparing the first two elements in the array. If element 0 is greater than element 1, they are exchanged.
#include <iostream> using namespace std; int seachList (const int [], int, int); const int SIZE = 5; int main() { int tests [SIZE] = {87, 75, 98, 100, 82}; int results; // Passing tests, SIZE, 100(number you want to serch) results = searchList (tests, SIZE, 100); if (result == -1) cout << "You did not earn 100 points on any test\n." else { cout << "You earned 100 points on test "; cout << (result + 1) << endl; } return 0; }
int searchList (const int list[], int numElements, int value) { int index = 0; int position = -1; bool found = false; while (index < numElems && ! found) { if (list[index] == value) { found = true; position = index; } index++ } return position; }
Here is the pseudocode for a function that performs binary search on an array: Set first index to 0 Set last index to the last subscript in the array Set found to false Set position to -1
While found is not true and first is less than or equal to last Set middle to subscript halfway btwn array[first] & [last] If array [middle] = desired value Set found to true Set position to middle Else if array [middle] is greater than the desired value Set last to middle - 1 Else Set first to middle + 1 End If End While
Here is the Pseudocode For startscan is set to subscript in array from o to sbr -1 Set index var to startScan Set minIndex var to startScan Set minValue var to array [startScan] For index is set to each subcr in array from (startScan + 1) through the last subsript If array [index] is less than minVal Set minValue to array [index] Set minIndex to index End if End For Set array [minIndex] = array [index] Set array [startScan] = minValue End for
void selectionSort(int array[], int size) { int startScan, minIndex, minValue; for (startScan = 0; startScan < (size − 1); startScan++) { minIndex = startScan; minValue = array[startScan]; for(int index = startScan + 1; index < size; index++) { if (array[index] < minValue) { minValue = array[index]; minIndex = index; } } array[minIndex] = array[startScan]; array[startScan] = minValue; } }
Here is the bubble sort in pseudocode Do Set swap flag to false For count is set to each subscript in array fr 0 to last if array [count] > array [count+1] Set the content of array [count] than array [count+1] Set swap flag to true End if End For While any Element is swap
void sortArray (int array[], int size) { bool swap; int temp; do { swap = false; for (int count = 0; count < (size -1); count++) { if (array[count] > array[count + 1]) { temp = array[count]; array[count] = array[count + 1]; array[count + 1] = temp; swap = true; } } } while (swap); }
The binary search is a clever algorithm that is much more efficient than the linear search. Its only requirement is that the values of the array be sorted in order.
Instead of testing the array first elements, this algorithm starts with the element in the middle.
The linear search is a very simple algorithm. Sometimes called a sequential search, it uses a loop to sequentially step through an array, starting with the first element.
It compares each element with the values being searched for and stops when either the value is found of the array is encountered.
Obviously the binary search is much more efficient than the linear search. Every time it makes a comparison and fails to find the desired item
It eliminates half of the remaining portion of the array that must be search
The selection sort. The bubble sort is inefficient for large a arrays because items only move by one element at a time. The selection sort however usually performs fewer exchange because it moves items immediately to their final position in the array
It works like this. The smallest value is located and moved to element 0. Then the next smallest value is located and moved to element 1.
A search algorithm is a method of locating a specific item in a larger collection of data
The section discusses two algorithms for searching the contents of array
