Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
560 B
1
Indexable
Never
//cau1
#include <iostream>

using namespace std;

int linear_search(int arr[], int n, int x) {
  for (int i = 0; i < n; i++) {
    if (arr[i] == x) {
      return i;
    }
  }
  return -1;
}

int main() {
  int arr[] = {5, 2, 4, 6, 1, 3};
  int n = sizeof(arr) / sizeof(arr[0]);
  int x = 4;

  int result = linear_search(arr, n, x);

  if (result == -1) {
    cout << "Khong tim thay " << x << " trong mang." << endl;
  } else {
    cout << "Tim thay " << x << " tai vi tri " << result << " trong mang." << endl;
  }

  return 0;
}
Leave a Comment