8. Сортиране и търсене

** Сортиране чрез селекция (пряк избор)

// selsort.cpp
01: #include <iostream> 02: #include <vector> 03: #include <cstdlib> 04: #include <ctime> 06: using namespace std; 08: /** 09: Swaps two integers. 10: @param x the first integer to swap 11: @param y the second integer to swap 12: */ 13: void swap(int& x, int& y) 14: { int temp = x; 16: x = y; 17: y = temp; 18: } 20: /** 21: Gets the position of the smallest element in a vector range. 22: @param a the vector 23: @param from the beginning of the range 24: @param to the beginning of the range 25: @return the position of the smallest element in 26: the range a[from]...a[to] 27: */ 28: int min_position(vector<int>& a, int from, int to) 29: { int min_pos = from; 31: int i; 32: for (i = from + 1; i <= to; i++) 33: if (a[i] < a[min_pos]) min_pos = i; 34: return min_pos; 35: } 37: /** 38: Sorts a vector using the selection sort algorithm 39: @param a the vector to sort 40: */ 41: void selection_sort(vector<int>& a) 42: { int next; /* the next position to be set to the minimum */ 44: 45: for (next = 0; next < a.size() - 1; next++) 46: { /* find the position of the minimum */ 48: int min_pos = min_position(a, next, a.size() - 1); 49: if (min_pos != next) 50: swap(a[min_pos], a[next]); 51: } 52: } 54: /** 55: Prints all elements in a vector 56: @param a the vector to print 57: */ 58: void print(vector<int> a) 59: { for (int i = 0; i < a.size(); i++) 61: cout << a[i] << " "; 62: cout << "\n"; 63: } 65: /** 66: Sets the seed of the random number generator. 67: */ 68: void rand_seed() 69: { int seed = static_cast<int>(time(0)); 71: srand(seed); 72: } 74: /** 75: Computes a random integer in a range. 76: @param a the bottom of the range 77: @param b the top of the range 78: @return a random integer x, a <= x and x <= b 79: */ 80: int rand_int(int a, int b) 81: { return a + rand() % (b - a + 1); } 84: 85: int main() 86: { rand_seed(); 88: vector<int> v(20); 89: for (int i = 0; i < v.size(); i++) 90: v[i] = rand_int(1, 100); 91: print(v); 92: selection_sort(v); 93: print(v); 94: return 0; 95: }

** Оценка на сложността на алгоритъма за пряк избор

** Сортиране чрез сливане

// mergsort.cpp
001: #include <iostream> 002: #include <vector> 003: #include <cstdlib> 004: #include <ctime> 006: using namespace std; 008: /** 009: Merges two adjacent ranges in a vector 010: @param a the vector with the elements to merge 011: @param from the start of the first range 012: @param mid the end of the first range 013: @param to the end of the second range 014: */ 015: void merge(vector<int>& a, int from, int mid, int to) 016: { int n = to - from + 1; /* size of the range to be merged */ 018: /* merge both halves into a temporary vector b */ 019: vector<int> b(n); 020: 021: int i1 = from; 022: /* next element to consider in the first half */ 023: int i2 = mid + 1; 024: /* next element to consider in the second half */ 025: int j = 0; /* next open position in b */ 027: /* 028: As long as neither i1 nor i2 past the end, move the smaller 029: element into b 030: */ 031: while (i1 <= mid && i2 <= to) 032: { if (a[i1] < a[i2]) 034: { b[j] = a[i1]; 036: i1++; 037: } 038: else 039: { b[j] = a[i2]; 041: i2++; 042: } 043: j++; 044: } 046: /* 047: Note that only one of the two while loops below is executed 048: */ 050: /* Copy any remaining entries of the first half */ 051: while (i1 <= mid) 052: { b[j] = a[i1]; 054: i1++; 055: j++; 056: } 057: /* Copy any remaining entries of the second half */ 058: while (i2 <= to) 059: { b[j] = a[i2]; 061: i2++; 062: j++; 063: } 065: /* Copy back from the temporary vector */ 066: for (j = 0; j < n; j++) a[from + j] = b[j]; 068: } 070: /** 071: Sorts the elements in a range of a vector. 072: @param a the vector with the elements to sort 073: @param from start of the range to sort 074: @param to end of the range to sort 075: */ 076: void merge_sort(vector<int>& a, int from, int to) 077: { if (from == to) return; 079: int mid = (from + to) / 2; 080: /* sort the first and the second half */ 081: merge_sort(a, from, mid); 082: merge_sort(a, mid + 1, to); 083: merge(a, from, mid, to); 084: } 086: /** 087: Prints all elements in a vector 088: @param a the vector to print 089: */ 090: void print(vector<int> a) 091: { for (int i = 0; i < a.size(); i++) cout << a[i] << " "; 094: cout << "\n"; 095: } 097: /** 098: Sets the seed of the random number generator. 099: */ 100: void rand_seed() 101: { int seed = static_cast<int>(time(0)); 103: srand(seed); 104: } 106: /** 107: Computes a random integer in a range. 108: @param a the bottom of the range 109: @param b the top of the range 110: @return a random integer x, a <= x and x <= b 111: */ 112: int rand_int(int a, int b) 113: { return a + rand() % (b - a + 1); } 116: 117: int main() 118: { rand_seed(); 120: vector<int> v(20); 121: for (int i = 0; i < v.size(); i++) 122: v[i] = rand_int(1, 100); 123: print(v); 124: merge_sort(v, 0, v.size() - 1); 125: print(v); 126: return 0; 127: }

** Анализ на ефективността на алгоритъма за сортиране чрез сливане

** Линейно търсене
// lsearch.cpp  
01: #include <iostream>
02: #include <vector>
03: #include <cstdlib>
04: #include <ctime>
06: using namespace std;
08: /**
09: Finds an element in a vector
10: @param v the vector with the elements to search
11: @param a the value to search for
12: @return the index of the first match, or -1 if not found
13: */
14: int linear_search(vector<int> v, int a)
15: { for (int i = 0; i < v.size(); i++)
17: if (v[i] == a) return i;
21: return -1;
22: }
24: /**
25: Prints all elements in a vector
26: @param a the vector to print
27: */
28: void print(vector<int> a)
29: { for (int i = 0; i < a.size(); i++)
31: cout << a[i] << " ";
32: cout << "\n";
33: }
35: /**
36: Sets the seed of the random number generator.
37: */
38: void rand_seed()
39: { int seed = static_cast<int>(time(0));
41: srand(seed);
42: }
44: /**
45: Computes a random integer in a range.
46: @param a the bottom of the range
47: @param b the top of the range
48: @return a random integer x, a <= x and x <= b
49: */
50: int rand_int(int a, int b)
51: { return a + rand() % (b - a + 1); }
54:
55: int main()
56: { rand_seed();
58: vector<int> v(20);
59: for (int i = 0; i < v.size(); i++)
60: v[i] = rand_int(1, 100);
61: print(v);
62: cout << "Enter number to search for: ";
63: int n;
64: cin >> n;
65: int j = linear_search(v, n);
66: cout << "Found in position " << j << "\n";
67: return 0;
68: }

** Двоично търсене
v[0]
v[1]
v[2]
v[3]
v[4]
v[5]
v[6]
v[7]
14
43
76
100
115
290
400
511
v[4]
v[5]
v[6]
v[7]
115
290
400
511
v[4]
115
// bsearch.cpp
01: #include <iostream> 02: #include <vector> 03: #include <cstdlib> 04: #include <ctime> 06: using namespace std; 08: /** 09: Finds an element in a sorted vector. 10: @param v the sorted vector with the elements to search 11: @param from the start of the range to search 12: @param to the end of the range to search 13: @param a the value to search for 14: @return the index of the first match, or -1 if not found 15: */ 16: int binary_search(vector<int> v, int from, int to, int a) 17: { if (from > to) return -1; 20: int mid = (from + to) / 2; 21: int diff = v[mid] - a; 22: if (diff == 0) /* v[mid] == a */ 23: return mid; 24: else if (diff < 0) /* v[mid] < a */ 25: return binary_search(v, mid + 1, to, a); 26: else 27: return binary_search(v, from, mid - 1, a); 28: } 30: /** 31: Prints all elements in a vector 32: @param a the vector to print 33: */ 34: void print(vector<int> a) 35: { for (int i = 0; i < a.size(); i++) 37: cout << a[i] << " "; 38: cout << "\n"; 39: } 41: /** 42: Sets the seed of the random number generator. 43: */ 44: void rand_seed() 45: { int seed = static_cast<int>(time(0)); 47: srand(seed); 48: } 50: /** 51: Computes a random integer in a range. 52: @param a the bottom of the range 53: @param b the top of the range 54: @return a random integer x, a <= x and x <= b 55: */ 56: int rand_int(int a, int b) 57: { return a + rand() % (b - a + 1); } 60: 61: int main() 62: { rand_seed(); 64: vector<int> v(20); 65: v[0] = 1; 66: for (int i = 1; i < v.size(); i++) 67: v[i] = v[i - 1] + rand_int(1, 10); 68: 69: print(v); 70: cout << "Enter number to search for: "; 71: int n; 72: cin >> n; 73: int j = binary_search(v, 0, v.size() - 1, n); 74: cout << "Found in position " << j << "\n"; 75: return 0; 76: }