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
59 changes: 59 additions & 0 deletions Binary Search/iterative binary search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <stdio.h>
int iterative_binary(int array[], int start, int end, int search);
int main()
{
int array[50], n, i, s;
int idx;
printf("How many elements do you want to enter? ");
scanf("%d",&n);

printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
{
scanf("\t%d",&array[i]);
}

printf("Enter the element to search: ");
scanf("%d",&s);

idx = iterative_binary(array, 0, n-1, s);
if(idx == NULL)
{
printf("Element is not present in the array.\n");
}
else
{
printf("The element occurs in index: %d\n",idx);
}
return 0;
}

int iterative_binary(int array[], int start, int end, int search)
{
while (start <= end)
{
int mid = start + (end - start)/2;
printf("Middle element: %d\n",mid);
if (array[mid] == search)
return mid;
if (array[mid] < search)
start = mid + 1;
else
end = mid - 1;
}
return;
}














45 changes: 45 additions & 0 deletions Sorting/selection-sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include<iostream>
using namespace std;
void swapping(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void display(int *array, int size)
{
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void selectionSort(int *array, int size)
{
int i, j, imin;
for(i = 0; i<size-1; i++)
{
imin = i; //get index of minimum data
for(j = i+1; j<size; j++)
if(array[j] < array[imin])
imin = j;
//placing in correct position
swap(array[i], array[imin]);
}
}
int main()
{
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++)
{
cin >> arr[i];
}
cout << "Array before Sorting: ";
display(arr, n);
selectionSort(arr, n);
cout << "Array after Sorting: ";
display(arr, n);
}