عملي2

advertisement
Introduction to Search Algorithms
The Linear Search:Program 9-1
1 // This program demonstrates the searchList function,
2 // which performs a linear search on an integer array.
3 #include <iostream>
4 using namespace std;
5
6 // Function prototype
7 int searchList(int [], int, int);
8
9 const int SIZE = 5;
10
11 int main()
12 {
13 int tests[SIZE] = {87, 75, 98, 100, 82};
14 int results; // Holds the search results
15
16 // Search the array for the value 100
17 results = searchList(tests, SIZE, 100);
18
19 // If searchList returned -1, 100 was not found
20 if (results == -1)
21 cout << "You did not earn 100 points on any test.\n";
22 else
23 { // Otherwise results contains the subscript of
24 // the first 100 found in the array
25 cout << "You earned 100 points on test ";
26 cout << (results + 1) << ".\n";
27 }
28 return 0;
29 }
30
31 /*****************************************************************
32 * searchList *
33 * This function performs a linear search on an integer array. *
34 * The list array, which has size elements, is searched for *
35 * the number stored in value. If the number is found, its array *
36 * subscript is returned. Otherwise, -1 is returned. *
37 *****************************************************************/
38 int searchList(int list[], int size, int value)
39 {
40 int index = 0; // Used as a subscript to search array
41 int position = -1; // Used to record position of search value
42 bool found = false; // Flag to indicate if the value was found
43
44 while (index < size && !found)
45 {
46 if (list[index] == value) // If the value is found
47 {
48 found = true; // Set the flag
49 position = index; // Record the value's subscript
50 }
51 index++; // Go to the next element
52 }
53 return position; // Return the position, or -1
54 }
The Binary Search:Program 9-2
1 // This program performs a binary search on an integer
2 // array whose elements are in ascending order.
3 #include <iostream>
4 using namespace std;
5
6 // Function prototype
7 int binarySearch(int [], int, int);
8
9 const int SIZE = 20;
10
11 int main()
12 {
13 // Create an array of ID numbers sorted in ascending order
14 int IDnums[SIZE] = {101, 142, 147, 189, 199, 207, 222,
15 234, 289, 296, 310, 319, 388, 394,
16 417, 429, 447, 521, 536, 600 };
17
18 int empID, // Holds the ID to search for
19 results; // Holds the search results
20
21 // Get an employee ID to search for
22 cout << "Enter the employee ID you wish to search for: ";
23 cin >> empID;
24
25 // Search for the ID
26 results = binarySearch(IDnums, SIZE, empID);
27
28 // If binarySearch returned -1, the ID was not found
29 if (results == -1)
30 cout << "That number does not exist in the array.\n";
31 else
32 { // Otherwise results contains the subscript of
33 // the specified employee ID in the array
34 cout << "ID " << empID << " was found in element "
35 << results << " of the array.\n";
36 }
37 return 0;
38 }
39
40 /*****************************************************************
41 * binarySearch *
42 * This function performs a binary search on an integer array *
43 * with size elements whose values are stored in ascending *
44 * order. The array is searched for the number stored in the *
45 * value parameter. If the number is found, its array subscript *
46 * is returned. Otherwise, -1 is returned. *
47 *****************************************************************/
48 int binarySearch(int array[], int size, int value)
49 {
50 int first = 0, // First array element
51 last = size - 1, // Last array element
52 middle, // Midpoint of search
53 position = -1; // Position of search value
54 bool found = false; // Flag
55
56 while (!found && first <= last)
57 {
58 middle = (first + last) / 2; // Calculate midpoint
59 if (array[middle] == value) // If value is found at mid
60 {
61 found = true;
62 position = middle;
63 }
64 else if (array[middle] > value) // If value is in lower half
65 last = middle - 1;
66 else
67 first = middle + 1; // If value is in upper half
68 }
69 return position;
70 }
Introduction to Sorting Algorithms
The Bubble Sort:Program 9-4
1 // This program uses the bubble sort algorithm to sort an array
2 // of integers in ascending order.
3 #include <iostream>
4 using namespace std;
5
6 // Function prototypes
7 void sortArray(int [], int);
8 void showArray(int [], int);
9
10 int main()
11 {
12 const int SIZE = 6;
13
14 // Array of unsorted values
15 int values[SIZE] = {7, 2, 3, 8, 9, 1};
16
17 // Display the values
18 cout << "The unsorted values are:\n";
19 showArray(values, SIZE);
20
21 // Sort the values
22 sortArray(values, SIZE);
23
24 // Display them again
25 cout << "The sorted values are:\n";
26 showArray(values, SIZE);
27 return 0;
28 }
29
30 /************************************************************
31 * sortArray *
32 * This function performs an ascending-order bubble sort on *
33 * array. The parameter size holds the number of elements *
34 * in the array. *
35 ************************************************************/
36 void sortArray(int array[], int size)
37 {
38 int temp;
39 bool swap;
40
41 do
42 { swap = false;
43 for (int count = 0; count < (size - 1); count++)
44 {
45 if (array[count] > array[count + 1])
46 {
47 temp = array[count];
48 array[count] = array[count + 1];
49 array[count + 1] = temp;
50 swap = true;
51 }
52 }
53 } while (swap);
54 }
55
56 /*************************************************************
57 * showArray *
58 * This function displays the contents of array. The *
59 * parameter size holds the number of elements in the array. *
60 *************************************************************/
61 void showArray(int array[], int size)
62 {
63 for (int count = 0; count < size; count++)
64 cout << array[count] << " ";
65 cout << endl;
66 }
The Selection Sort:Program 9-5
1 // This program uses the selection sort algorithm to sort
2 // an array in ascending order.
3 #include <iostream>
4 using namespace std;
5
6 // Function prototypes
7 void selectionSort(int [], int);
8 void showArray(int [], int);
9
10 int main()
11 {
12 const int SIZE = 6;
13
14 // Array of unsorted values
15 int values[SIZE] = {5, 7, 2, 8, 9, 1};
16
17 // Display the values
18 cout << "The unsorted values are\n";
19 showArray(values, SIZE);
20
21 // Sort the array
22 selectionSort(values, SIZE);
23
24 // Display the values again
25 cout << "The sorted values are\n";
26 showArray(values, SIZE);
27 return 0;
28 }
29
30 /**************************************************************
31 * selectionSort *
32 * This function performs an ascending-order selection sort *
33 * on array. The parameter size holds the number of elements *
34 * in the array. *
35 **************************************************************/
36 void selectionSort(int array[], int size)
37 {
38 int startScan, minIndex, minValue;
39
40 for (startScan = 0; startScan < (size - 1); startScan++)
41 {
42 minIndex = startScan;
43 minValue = array[startScan];
44 for(int index = startScan + 1; index < size; index++)
45 {
46 if (array[index] < minValue)
47 {
48 minValue = array[index];
49 minIndex = index;
50 }
51 }
52 array[minIndex] = array[startScan];
53 array[startScan] = minValue;
54 }
55 }
56
57 /*************************************************************
58 * showArray *
59 * This function displays the contents of array. The *
60 * parameter size holds the number of elements in the array. *
61 *************************************************************/
62 void showArray(int array[], int size)
63 {
64 for (int count = 0; count < size; count++)
65 cout << array[count] << " ";
66 cout << endl;
67 }
Download