Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
for-Alfabank
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jose Ernesto Suarez
for-Alfabank
Commits
264a7935
Unverified
Commit
264a7935
authored
Oct 02, 2019
by
Margarita
Committed by
GitHub
Oct 02, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Last realize
just made nicer code)
parent
f9b0dbdf
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
0 deletions
+61
-0
plus array
plus array
+61
-0
No files found.
plus array
0 → 100644
View file @
264a7935
import java.util.Arrays;
public class test {
public static int binarySearch(int ar[], int first, int last, int X){
int mid = (first + last)/2;
while( first <= last ){
if ( (ar[mid] < X) & ((ar[mid-1]) >= X)){
return mid;
}
else if (ar[mid] < X){
last = mid - 1;
}
else {
first = mid + 1;
}
mid = (first + last)/2;
}
return -1;
}
public static void main(String[] args) {
//create random array
System.out.print("\nRandom Array: \n");
int ar[] = new int[20];
for (int i = 0; i < ar.length; i++) {
ar[i] = (int)(Math.random()*100);
System.out.print(ar[i] + " ");
}
//My trick for simple sort
System.out.print("\nNegative array: \n");
for (int i = 0; i < ar.length; i++) {
ar[i] = ar[i]*(-1);
System.out.print(ar[i] + " ");
}
//Sort array and return positive numbers)
System.out.print("\nSorted Array: \n");
Arrays.sort(ar);
for(int i = 0; i < ar.length; i++) {
ar[i] = ar[i]*(-1);
System.out.print(ar[i] + " ");
}
//I just choose this number)
int X = 17;
System.out.print("\nX = " + X);
//call my binary search
int last = ar.length-1;
int first = 0;
int index = binarySearch(ar,first,last,X);
if (index != -1) {
System.out.println("\n" + X + " more than first_index_number: " + index);
} else {
System.out.println("\nsomething wrong");
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment