-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
1,821 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include<iostream> | ||
using namespace std; | ||
void swap1(int *a,int *b) | ||
{ | ||
|
||
int temp=*a; | ||
*a=*b; | ||
*b=temp; | ||
} | ||
|
||
int partition1(int arr[],int start,int last) | ||
{ | ||
|
||
int pivot=arr[last]; | ||
int current=start; | ||
int i; | ||
for( i=start;i<last;i++) | ||
{ | ||
if(arr[i]<=pivot) | ||
{ | ||
|
||
swap1(&arr[i],&arr[current]); | ||
current++; | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
swap1(&arr[last],&arr[current]); | ||
|
||
return current; | ||
} | ||
|
||
|
||
void quicksort(int arr[],int start,int last) | ||
{ | ||
if(start<last) | ||
{ | ||
int p_index=partition1(arr,start,last); | ||
quicksort(arr,start,p_index-1); | ||
quicksort(arr,p_index+1,last); | ||
|
||
|
||
} | ||
|
||
|
||
|
||
} | ||
|
||
int main() | ||
{ | ||
cout<<"ENTER SIZE"<<endl; | ||
int n; | ||
cin>>n; | ||
int *arr; | ||
arr=new int[n]; | ||
for(int i=0;i<n;i++) | ||
{ | ||
cin>>arr[i]; | ||
|
||
} | ||
int start=0; | ||
int last=n-1; | ||
quicksort(arr,start,last); | ||
cout<<endl; | ||
for(int i=0;i<n;i++) | ||
{ | ||
|
||
cout<<arr[i]<<" "; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
class Stack | ||
{ | ||
static final int MAX = 1000; | ||
int top; | ||
int a[] = new int[MAX]; | ||
|
||
boolean isEmpty() | ||
{ | ||
return (top < 0); | ||
} | ||
Stack() | ||
{ | ||
top = -1; | ||
} | ||
|
||
boolean push(int x) | ||
{ | ||
if (top >= (MAX-1)) | ||
{ | ||
System.out.println("Stack Overflow"); | ||
return false; | ||
} | ||
else | ||
{ | ||
a[++top] = x; | ||
System.out.println(x + " pushed into stack"); | ||
return true; | ||
} | ||
} | ||
|
||
int pop() | ||
{ | ||
if (top < 0) | ||
{ | ||
System.out.println("Stack Underflow"); | ||
return 0; | ||
} | ||
else | ||
{ | ||
int x = a[top--]; | ||
return x; | ||
} | ||
} | ||
} | ||
class Main | ||
{ | ||
public static void main(String args[]) | ||
{ | ||
Stack s = new Stack(); | ||
s.push(10); | ||
s.push(20); | ||
s.push(30); | ||
System.out.println(s.pop() + " Popped from stack"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include<iostream> | ||
using namespace std; | ||
///elements in array should be sorted | ||
bool binarysearch(int arr[],int num,int low,int high) | ||
{ | ||
int flag=0; | ||
int mid; | ||
if(low<=high) | ||
{ | ||
|
||
mid=(low+high)/2; | ||
if(arr[mid]<num) | ||
{ | ||
low=mid+1; | ||
|
||
} | ||
else | ||
{ | ||
high=mid-1; | ||
} | ||
if(num==arr[mid]) | ||
{ | ||
flag=1; | ||
return true; | ||
|
||
} | ||
binarysearch(arr,num,low,high); | ||
} | ||
else | ||
{ | ||
|
||
return false; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cout<<"Enter size"<<endl; | ||
cin>>n; | ||
int *arr; | ||
arr=new int[n]; | ||
for(int i=0;i<n;i++) | ||
{ | ||
|
||
cin>>arr[i]; | ||
} | ||
|
||
for(int i=0;i<n;i++) | ||
{ | ||
for(int j=i+1;j<n;j++) | ||
{ | ||
|
||
if(arr[i]>arr[j]) | ||
{ | ||
int temp=arr[j]; | ||
arr[j]=arr[i]; | ||
arr[i]=temp; | ||
|
||
|
||
} | ||
} | ||
|
||
} | ||
int num; | ||
cout<<"Enter num you want to search in an array"<<endl; | ||
cin>>num; | ||
int low=0; | ||
int high=n-1; | ||
if(binarysearch(arr,num,low,high)==true) | ||
{ | ||
cout<<"ELEMNET FOUND"<<endl; | ||
|
||
} | ||
else | ||
{ | ||
|
||
cout<<"ELEMNET NOT FOUND"<<endl; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include<iostream> | ||
#include<conio.h> | ||
#include<algorithm> | ||
|
||
int binary_search(int A[],int f,int l,int num) | ||
{int mid; | ||
mid=(f+l)/2; | ||
if(num==A[mid]) | ||
return mid; | ||
|
||
elseif(num>A[mid]) | ||
{binary_search(A,mid+1,l,num);} | ||
|
||
elseif(num<A[mid]) | ||
{binary_search(A,f,mid-1,num); | ||
|
||
else | ||
return(-1); | ||
} | ||
|
||
int main() | ||
{int a[100],n,d,x; | ||
cout<<"how many numbers?"; | ||
cin>>n; | ||
|
||
cout<<"enter numbers"; | ||
for(int i=0;i<n;i++) | ||
cin>>a[i]; | ||
|
||
sort(a,a+n); | ||
|
||
cout<<"Enter the number you wish to search"; | ||
cin>>d; | ||
|
||
x=binary_search(a,0,n-1,d); | ||
|
||
if(x!=-1) | ||
cout<<"number is at "<<x+1<<"position"; | ||
else | ||
cout<<"number not found"; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
void sort(int *data, int n) { | ||
int i, j, temp; | ||
for (i = 1; i < n; i++) { | ||
temp = data[i]; | ||
for (j = i; j > 0 && data[j-1] > temp; j--) { | ||
data[j] = data[j-1]; | ||
} | ||
data[j] = temp; | ||
} | ||
} | ||
|
||
int main() { | ||
int i, high, low, mid, n, *data, element, ch = 1; | ||
printf("Binary Search Example:\n"); | ||
printf("Enter the number of entries:"); | ||
scanf("%d", &n); | ||
|
||
data = (int *)malloc(sizeof (int) * n); | ||
for (i = 0; i < n; i++) { | ||
scanf("%d", &data[i]); | ||
} | ||
sort(data, n); | ||
printf("After sorting:"); | ||
for (i = 0; i < n; i++) | ||
printf("%3d ", data[i]); | ||
printf("\n"); | ||
while (ch) { | ||
printf("Enter the data to be searched:"); | ||
scanf("%d", &element); | ||
low = 0, high = n-1; | ||
while (low <= high) { | ||
mid = (high + low) / 2; | ||
if (data[mid] < element) | ||
low = mid + 1; | ||
else if (data[mid] > element) | ||
high = mid - 1; | ||
else { | ||
printf("Search element %d found " | ||
"at index %d\n", element, mid); | ||
break; | ||
} | ||
} | ||
printf("Searched element is not available\n"); | ||
printf("Do you wanna continue search(1/0):"); | ||
scanf("%d", &ch); | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.