-
Notifications
You must be signed in to change notification settings - Fork 56
Added binary search #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jayeshkumar09
wants to merge
3
commits into
anku580:master
Choose a base branch
from
jayeshkumar09:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,39 @@ | ||
| import java.util.*; | ||
| class Binarysearch { | ||
| public static void main(String[] args) | ||
| { | ||
| int[] arr = {16, 19, 20, 23, 45, 56, 78, 90, 96, 100}; | ||
| int item, location = -1; | ||
| System.out.println("Enter the item which you want to search"); | ||
| Scanner sc = new Scanner(System.in); | ||
| n = sc.nextInt(); | ||
| address = binarySearch(arr,0,9,item); | ||
| if(address != -1) | ||
| System.out.println("the location of the item is "+location); | ||
| else | ||
| System.out.println("Item not found"); | ||
| } | ||
|
|
||
| public static int binarySearch(int[] a, int beg, int end, int n) | ||
| { | ||
| int mid; | ||
| if(end >= beg) | ||
| { | ||
| mid = (beg + end)/2; | ||
| if(a[mid] == n) | ||
| { | ||
| return mid+1; | ||
| } | ||
| else if(a[mid] < n) | ||
| { | ||
| return binarySearch(a,mid+1,end,n); | ||
| } | ||
| else | ||
| { | ||
| return binarySearch(a,beg,mid-1,n); | ||
| } | ||
|
|
||
| } | ||
| return -1; | ||
| } | ||
| } |
This file contains hidden or 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,68 @@ | ||
| /*A heap is a tree with some special properties, so value of node should be greater | ||
| than or equal to(less than or equal to in case of min heap) children of the node and | ||
| tree should be complete binary tree */ | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class HeapSortMain { | ||
|
|
||
| public static void buildheap(int []arr) { | ||
|
|
||
| /* | ||
| * As last non leaf node will be at (arr.length-1)/2 | ||
| * so we will start from this location for heapifying the elements | ||
| * */ | ||
| for(int i=(arr.length-1)/2; i>=0; i--){ | ||
| heapify(arr,i,arr.length-1); | ||
| } | ||
| } | ||
|
|
||
| public static void heapify(int[] arr, int i,int size) { | ||
| int left = 2*i+1; | ||
| int right = 2*i+2; | ||
| int max; | ||
| if(left <= size && arr[left] > arr[i]){ | ||
| max=left; | ||
| } else { | ||
| max=i; | ||
| } | ||
|
|
||
| if(right <= size && arr[right] > arr[max]) { | ||
| max=right; | ||
| } | ||
| // If max is not current node, exchange it with max of left and right child | ||
| if(max!=i) { | ||
| exchange(arr,i, max); | ||
| heapify(arr, max,size); | ||
| } | ||
| } | ||
|
|
||
| public static void exchange(int[] arr,int i, int j) { | ||
| int t = arr[i]; | ||
| arr[i] = arr[j]; | ||
| arr[j] = t; | ||
| } | ||
|
|
||
| public static int[] heapSort(int[] arr) { | ||
|
|
||
| buildheap(arr); | ||
| int sizeOfHeap=arr.length-1; | ||
| for(int i=sizeOfHeap; i>0; i--) { | ||
| exchange(arr,0, i); | ||
| sizeOfHeap=sizeOfHeap-1; | ||
| heapify(arr, 0,sizeOfHeap); | ||
| } | ||
| return arr; | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| int[] arr={1,10,16,19,3,5}; | ||
| System.out.println("Before Heap Sort : "); | ||
| System.out.println(Arrays.toString(arr)); | ||
| arr=heapSort(arr); | ||
| System.out.println("====================="); | ||
| System.out.println("After Heap Sort : "); | ||
| System.out.println(Arrays.toString(arr)); | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a folder called
Sortand then add this file in it.