diff --git a/Vaibhav Jindal/Binary_Search.cpp b/Vaibhav Jindal/Binary_Search.cpp new file mode 100644 index 0000000..f2048a0 --- /dev/null +++ b/Vaibhav Jindal/Binary_Search.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +// A recursive binary search function. It returns +// location of x in given array arr[l..r] is present, +// otherwise -1 +int binarySearch(int arr[], int l, int r, int x) +{ + if (r >= l) { + int mid = l + (r - l) / 2; + + // If the element is present at the middle + // itself + if (arr[mid] == x) + return mid; + + // If element is smaller than mid, then + // it can only be present in left subarray + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + + // Else the element can only be present + // in right subarray + return binarySearch(arr, mid + 1, r, x); + } + + // We reach here when element is not + // present in array + return -1; +} + +int main(void) +{ + int len; + cout << "Enter Length of Array: "; + cin >> len; + int arr[len]; + cout << "Enter Elements of Array: "; + for(int i = 0; i < len; i++) + cin >> arr[i]; + int x; + cout << "Enter Element to Search: "; + cin >> x; + int result = binarySearch(arr, 0, len - 1, x); + (result == -1) ? cout << "Element is not present in array" << endl + : cout << "Element is present at index " << result << endl; + return 0; +} + diff --git a/Vaibhav Jindal/Insertion_Sort.cpp b/Vaibhav Jindal/Insertion_Sort.cpp new file mode 100644 index 0000000..caa8680 --- /dev/null +++ b/Vaibhav Jindal/Insertion_Sort.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +/* Function to sort an array using insertion sort*/ +void insertionSort(int arr[], int n) +{ + int i, key, j; + for (i = 1; i < n; i++) + { + key = arr[i]; + j = i - 1; + + /* Move elements of arr[0..i-1], that are + greater than key, to one position ahead + of their current position */ + while (j >= 0 && arr[j] > key) + { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } +} + +// A utility function to print an array of size n +void printArray(int arr[], int n) +{ + int i; + for (i = 0; i < n; i++) + cout << arr[i] << " "; + cout << endl; +} + +int main() +{ + int len; + cout << "Enter Length of Array: "; + cin >> len; + int arr[len]; + cout << "Enter Elements of Array: "; + for(int i = 0; i < len; i++) + cin >> arr[i]; + cout << "Sorted array: "; + insertionSort(arr, len); + printArray(arr, len); + + return 0; +} diff --git a/Vaibhav Jindal/Linear_Search.cpp b/Vaibhav Jindal/Linear_Search.cpp new file mode 100644 index 0000000..2dfecfd --- /dev/null +++ b/Vaibhav Jindal/Linear_Search.cpp @@ -0,0 +1,35 @@ +// C++ code to linearly search x in arr[]. If x +// is present then return its location, otherwise +// return -1 + +#include +using namespace std; + +//FUNCTION FOR LINEAR SEARCH +int search(int arr[], int n, int x) +{ + int i; + for (i = 0; i < n; i++) + if (arr[i] == x) + return i; + return -1; +} + +int main(void) +{ + int len; + cout << "Enter Length of Array: "; + cin >> len; + int arr[len]; + cout << "Enter Elements of Array: "; + for(int i = 0; i < len; i++) + cin >> arr[i]; + int x; + cout << "Enter Element to Search: "; + cin >> x; + int result = search(arr, len, x); + (result == -1)? cout<<"Element is not present in array" << endl + : cout<<"Element is present at index " << result << endl; + return 0; +} + diff --git a/Vaibhav Jindal/Selection_Sort.cpp b/Vaibhav Jindal/Selection_Sort.cpp new file mode 100644 index 0000000..2c7c904 --- /dev/null +++ b/Vaibhav Jindal/Selection_Sort.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +void selectionSort(int arr[], int n) +{ + int i, j, min_idx; + + // One by one move boundary of unsorted subarray + for (i = 0; i < n-1; i++) + { + // Find the minimum element in unsorted array + min_idx = i; + for (j = i+1; j < n; j++) + if (arr[j] < arr[min_idx]) + min_idx = j; + + // Swap the found minimum element with the first element + swap(&arr[min_idx], &arr[i]); + } +} + +/* Function to print an array */ +void printArray(int arr[], int size) +{ + int i; + for (i=0; i < size; i++) + cout << arr[i] << " "; + cout << endl; +} + +int main() +{ + int len; + cout << "Enter Length of Array: "; + cin >> len; + int arr[len]; + cout << "Enter Elements of Array: "; + for(int i = 0; i < len; i++) + cin >> arr[i]; + selectionSort(arr, len); + cout << "Sorted array: "; + printArray(arr, len); + return 0; +}