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
35 changes: 35 additions & 0 deletions BinarySearch/New Text Document.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
// 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 arr[] = {2, 3, 4, 10, 40};
���int n = sizeof(arr)/ sizeof(arr[0]);
���int x = 10;
���int result = binarySearch(arr, 0, n-1, x);
���(result == -1)? printf("Element is not present in array")
�����������������: printf("Element is present at index %d", result);
���return 0;
}
46 changes: 46 additions & 0 deletions BubbleSort/Bubble Sort in C++
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>

using namespace std;

// Sort arr[] of size n using Bubble Sort.
void BubbleSort (int arr[], int n)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n-i-1; ++j)
{
// Comparing consecutive data and switching values if value at j > j+1.
if (arr[j] > arr[j+1])
{
arr[j] = arr[j]+arr[j+1];
arr[j+1] = arr[j]-arr[j + 1];
arr[j] = arr[j]-arr[j + 1];
}
}
// Value at n-i-1 will be maximum of all the values below this index.
}
}

int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;

int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}

BubbleSort(arr, n);

// Display the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];

return 0;
}
34 changes: 34 additions & 0 deletions BubbleSort/New Text Document.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");
scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)
scanf("%d", &array[c]);

for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);

return 0;
}
47 changes: 47 additions & 0 deletions InsertionSort/insertion sort in c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// C program for insertion sort
#include <stdio.h>
#include <math.h>

/* 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 ot print an array of size n
void printArray(int arr[], int n)
{
   int i;
   for (i=0; i < n; i++)
       printf("%d ", arr[i]);
   printf("\n");
}



/* Driver program to test insertion sort */
int main()
{
    int arr[] = {12, 11, 13, 5, 6};
    int n = sizeof(arr)/sizeof(arr[0]);

    insertionSort(arr, n);
    printArray(arr, n);

    return 0;
}
30 changes: 30 additions & 0 deletions LinearSearch/New Text Document.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);
return 0;
}