Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void swap (int &a, int &b) {
- int c = 0;
- c = a;
- a = b;
- b = c;
- }
- void qs (int *arr, int low, int high){
- int a = high;
- int i = low - 1;
- if (low < high) {
- for (int j = low; j < high; j++){
- if (arr[a] > arr[j]) {
- i++;
- swap (arr[i], arr[j]);
- }
- }
- swap(arr[a], arr[i+1]);
- qs (*&arr, low, i);
- qs (*&arr, i+2, high);
- }
- }
- void bs (int arr[], int k, int low, int high){
- int x = high/2;
- if (low < high){
- if (arr[x] < k){
- bs(arr, k, x+1, high);
- } else if (arr[x] > k) {
- bs(arr, k, low, x-1);
- } else if (arr[x] == k) {
- cout << " Found " << k << " at " << x+1;
- }
- } else if (low == high) {
- if (arr[high] == k){
- cout << " Found " << k << " at " << high+1;
- } else {
- cout << "Not Found";
- }
- }
- }
- int main() {
- int n, s;
- cout << "How many integers you want to input : ";
- cin >> n;
- int array[n];
- for (int x = 0; x < n; x++){
- cin >> array[x];
- }
- qs(array, 0, n-1);
- cout << "Ouput of sorted array : ";
- for (int x = 0; x < n; x++){
- cout << array[x] << " ";
- }
- cout << endl << "Which element do you want to search for? ";
- cin >> s;
- bs (array, s, 0, n-1);
- return 0;
- }
Add Comment
Please, Sign In to add comment