Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream.h>
- int A[2][20];
- int B[40];
- void CreateAndPrintA()
- {
- srand(time(NULL));
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < 20; j++)
- {
- A[i][j] = rand() % 100;
- cout << A[i][j] << " ";
- }
- cout << endl;
- }
- }
- void BubbleSort()
- {
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < 19; j++)
- {
- for (int k = 0; k < 19; k++)
- {
- if (A[i][k] > A[i][k + 1])
- {
- int initialValue = A[i][k];
- A[i][k] = A[i][k + 1];
- A[i][k + 1] = initialValue;
- }
- }
- }
- }
- }
- void CreateAndPrintB()
- {
- int index = 0;
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < 20; j++)
- {
- B[index] = A[i][j];
- cout << B[index] << " ";
- index++;
- }
- }
- cout << endl;
- }
- void PrqkaSelekciq()
- {
- for (int i = 0; i < 39; i++)
- {
- int min = i;
- for (int j = i + 1; j < 40; j++)
- {
- if (B[min] > B[j])
- {
- min = j;
- }
- }
- int initialValue = B[min];
- B[min] = B[i];
- B[i] = initialValue;
- }
- }
- bool BinarySearch(int n)
- {
- int left = 0;
- int mid = 0;
- int right = 39;
- while (left <= right)
- {
- mid = (left + right) / 2;
- if (B[mid] == n)
- {
- return true;
- }
- else
- {
- if (B[mid] > n)
- {
- right = mid - 1;
- }
- else
- {
- left = mid + 1;
- }
- }
- }
- return false;
- }
- int main() {
- CreateAndPrintA();
- BubbleSort();
- cout << "-------------------------------" << endl;
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < 20; j++)
- {
- cout << A[i][j] << " ";
- }
- cout << endl;
- }
- cout << "-------------------------------" << endl;
- CreateAndPrintB();
- PrqkaSelekciq();
- cout << "-------------------------------" << endl;
- for (int i = 0; i < 40; i++)
- {
- cout << B[i] << " ";
- }
- int n;
- cout <<endl<<"n = ";
- cin >> n;
- if (BinarySearch(n))
- {
- cout << "The number was found" << endl;
- }
- else
- {
- cout << "The number was not found" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment