Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Vaibhav Jindal/Binary_Search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <bits/stdc++.h>
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;
}

48 changes: 48 additions & 0 deletions Vaibhav Jindal/Insertion_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <bits/stdc++.h>
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;
}
35 changes: 35 additions & 0 deletions Vaibhav Jindal/Linear_Search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// C++ code to linearly search x in arr[]. If x
// is present then return its location, otherwise
// return -1

#include <iostream>
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;
}

51 changes: 51 additions & 0 deletions Vaibhav Jindal/Selection_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <bits/stdc++.h>
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;
}